diff options
author | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
---|---|---|
committer | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
commit | b643ca0bd58188da3dc98193f97fb220ab008983 (patch) | |
tree | f445722a242dd9e201a14143aa930938520b638a /dpi | |
parent | 13e0d2aabf7d06c3034b878e0cea861e4db1ddce (diff) |
Convert dpid, file dpi and cookies dpi to dsh API
cookies: convert to dsh API (with auth)
file dpi: convert to dsh API (with auth)
dpid: convert to dsh API (with auth)
handle EINTR in connect(), authenticated DpiBye cmd
Diffstat (limited to 'dpi')
-rw-r--r-- | dpi/cookies.c | 82 | ||||
-rw-r--r-- | dpi/file.c | 110 |
2 files changed, 104 insertions, 88 deletions
diff --git a/dpi/cookies.c b/dpi/cookies.c index f5cfc188..34cb222b 100644 --- a/dpi/cookies.c +++ b/dpi/cookies.c @@ -42,6 +42,7 @@ int main(void) #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> +#include <netinet/in.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> @@ -114,6 +115,11 @@ typedef struct { Dlist *ports; } CookieData_t; +typedef struct { + Dsh *sh; + int status; +} ClientInfo; + /* * Local data */ @@ -1343,22 +1349,25 @@ static CookieControlAction Cookies_control_check_domain(const char *domain) * Note: Buf is a zero terminated string * Return code: { 0:OK, 1:Abort, 2:Close } */ -static int srv_parse_buf(SockHandler *sh, char *Buf, size_t BufSize) +static int srv_parse_tok(Dsh *sh, ClientInfo *client, char *Buf) { char *p, *cmd, *cookie, *host, *path, *scheme; - int port, ret; - - if (!(p = strchr(Buf, '>'))) { - /* Haven't got a full tag */ - MSG("Haven't got a full tag!\n"); - return 1; - } + int port, ret = 1; + size_t BufSize = strlen(Buf); cmd = a_Dpip_get_attr_l(Buf, BufSize, "cmd"); - if (cmd && strcmp(cmd, "DpiBye") == 0) { + if (!cmd) { + /* abort */ + } else if (client->status == 0) { + /* authenticate */ + if (a_Dpip_check_auth(Buf) == 1) { + client->status = 1; + ret = 0; + } + } else if (strcmp(cmd, "DpiBye") == 0) { dFree(cmd); - MSG("Cookies dpi (pid %d): Got DpiBye.\n", (int)getpid()); + MSG("(pid %d): Got DpiBye.\n", (int)getpid()); exit(0); } else if (cmd && strcmp(cmd, "set_cookie") == 0) { @@ -1375,7 +1384,7 @@ static int srv_parse_buf(SockHandler *sh, char *Buf, size_t BufSize) dFree(path); dFree(host); dFree(cookie); - return 2; + ret = 2; } else if (cmd && strcmp(cmd, "get_cookie") == 0) { dFree(cmd); @@ -1393,19 +1402,17 @@ static int srv_parse_buf(SockHandler *sh, char *Buf, size_t BufSize) cmd = a_Dpip_build_cmd("cmd=%s cookie=%s", "get_cookie_answer", cookie); - if (sock_handler_write_str(sh, 1, cmd)) { + if (a_Dpip_dsh_write_str(sh, 1, cmd)) { ret = 1; } else { - _MSG("sock_handler_write_str: SUCCESS cmd={%s}\n", cmd); + _MSG("a_Dpip_dsh_write_str: SUCCESS cmd={%s}\n", cmd); ret = 2; } dFree(cookie); dFree(cmd); - - return ret; } - return 0; + return ret; } /* -- Termination handlers ----------------------------------------------- */ @@ -1433,12 +1440,12 @@ static void termination_handler(int signum) * -- MAIN ------------------------------------------------------------------- */ int main(void) { - struct sockaddr_un spun; - int temp_sock_descriptor; + struct sockaddr_in sin; socklen_t address_size; + ClientInfo *client; + int tmp_fd,code; char *buf; - int code; - SockHandler *sh; + Dsh *sh; /* Arrange the cleanup function for terminations via exit() */ atexit(cleanup); @@ -1458,36 +1465,45 @@ int main(void) { exit(1); /* some OSes may need this... */ - address_size = sizeof(struct sockaddr_un); + address_size = sizeof(struct sockaddr_in); while (1) { - temp_sock_descriptor = - accept(STDIN_FILENO, (struct sockaddr *)&spun, &address_size); - if (temp_sock_descriptor == -1) { + tmp_fd = accept(STDIN_FILENO, (struct sockaddr *)&sin, &address_size); + if (tmp_fd == -1) { perror("[accept]"); exit(1); } - /* create the SockHandler structure */ - sh = sock_handler_new(temp_sock_descriptor,temp_sock_descriptor,8*1024); + /* create the Dsh structure */ + sh = a_Dpip_dsh_new(tmp_fd, tmp_fd, 8*1024); + client = dNew(ClientInfo,1); + client->sh = sh; + client->status = 0; while (1) { code = 1; - if ((buf = sock_handler_read(sh)) != NULL) { + if ((buf = a_Dpip_dsh_read_token(sh)) != NULL) { /* Let's see what we fished... */ _MSG(" buf = {%s}\n", buf); - code = srv_parse_buf(sh, buf, strlen(buf)); + code = srv_parse_tok(sh, client, buf); + dFree(buf); + } else if (sh->status == DPIP_EAGAIN) { + /* may reach here when the tag size is larger than kernel buffer */ + continue; } + _MSG(" code = %d %s\n", code, code == 1 ? "EXIT" : "BREAK"); - if (code == 1) + if (code == 1) { exit(1); - else if (code == 2) + } else if (code == 2) { break; + } } - _MSG("Closing SockHandler\n"); - sock_handler_close(sh); - sock_handler_free(sh); + _MSG("Closing Dsh\n"); + a_Dpip_dsh_close(sh); + a_Dpip_dsh_free(sh); + dFree(client); }/*while*/ @@ -61,7 +61,7 @@ typedef struct { } DilloDir; typedef struct { - SockHandler *sh; + Dsh *sh; int status; int old_style; pthread_t thrID; @@ -278,7 +278,7 @@ static void File_print_parent_dir(ClientInfo *Client, const char *dirname) Uparent = Escape_uri_str(parent, NULL); HUparent = Escape_html_str(Uparent); - sock_handler_printf(Client->sh, 0, + a_Dpip_dsh_printf(Client->sh, 0, "<a href='file:%s'>Parent directory</a>", HUparent); dFree(HUparent); dFree(Uparent); @@ -295,14 +295,14 @@ static void File_print_mtime(ClientInfo *Client, time_t mtime) /* Month, day and {hour or year} */ if (Client->old_style) { - sock_handler_printf(Client->sh, 0, " %.3s %.2s", ds + 4, ds + 8); + a_Dpip_dsh_printf(Client->sh, 0, " %.3s %.2s", ds + 4, ds + 8); if (time(NULL) - mtime > 15811200) { - sock_handler_printf(Client->sh, 0, " %.4s", ds + 20); + a_Dpip_dsh_printf(Client->sh, 0, " %.4s", ds + 20); } else { - sock_handler_printf(Client->sh, 0, " %.5s", ds + 11); + a_Dpip_dsh_printf(Client->sh, 0, " %.5s", ds + 11); } } else { - sock_handler_printf(Client->sh, 0, + a_Dpip_dsh_printf(Client->sh, 0, "<td>%.3s %.2s %.5s", ds + 4, ds + 8, /* (more than 6 months old) ? year : hour; */ (time(NULL) - mtime > 15811200) ? ds + 20 : ds + 11); @@ -358,7 +358,7 @@ static void File_info2html(ClientInfo *Client, FileInfo *finfo, int n) if (Client->old_style) { char *dots = ".. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .."; int ndots = MAXNAMESIZE - strlen(name); - sock_handler_printf(Client->sh, 0, + a_Dpip_dsh_printf(Client->sh, 0, "%s<a href='%s'>%s</a>" " %s" " %-11s%4d %-5s", @@ -367,7 +367,7 @@ static void File_info2html(ClientInfo *Client, FileInfo *finfo, int n) filecont, size, sizeunits); } else { - sock_handler_printf(Client->sh, 0, + a_Dpip_dsh_printf(Client->sh, 0, "<tr align=center %s><td>%s<td align=left><a href='%s'>%s</a>" "<td>%s<td>%d %s", (n & 1) ? "bgcolor=#dcdcdc" : "", @@ -375,7 +375,7 @@ static void File_info2html(ClientInfo *Client, FileInfo *finfo, int n) filecont, size, sizeunits); } File_print_mtime(Client, finfo->mtime); - sock_handler_write_str(Client->sh, 0, "\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "\n"); dFree(Hname); dFree(HUref); @@ -393,7 +393,7 @@ static void File_transfer_dir(ClientInfo *Client, /* Send DPI header */ d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url); - sock_handler_write_str(Client->sh, 1, d_cmd); + a_Dpip_dsh_write_str(Client->sh, 1, d_cmd); dFree(d_cmd); /* Send page title */ @@ -401,7 +401,7 @@ static void File_transfer_dir(ClientInfo *Client, HUdirname = Escape_html_str(Udirname); Hdirname = Escape_html_str(Ddir->dirname); - sock_handler_printf(Client->sh, 0, + a_Dpip_dsh_printf(Client->sh, 0, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "\r\n" @@ -415,21 +415,21 @@ static void File_transfer_dir(ClientInfo *Client, dFree(Udirname); if (Client->old_style) { - sock_handler_write_str(Client->sh, 0, "<pre>\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "<pre>\n"); } /* Output the parent directory */ File_print_parent_dir(Client, Ddir->dirname); /* HTML style toggle */ - sock_handler_write_str(Client->sh, 0, + a_Dpip_dsh_write_str(Client->sh, 0, " <a href='dpi:/file/toggle'>%</a>\n"); if (dList_length(Ddir->flist)) { if (Client->old_style) { - sock_handler_write_str(Client->sh, 0, "\n\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "\n\n"); } else { - sock_handler_write_str(Client->sh, 0, + a_Dpip_dsh_write_str(Client->sh, 0, "<br><br>\n" "<table border=0 cellpadding=1 cellspacing=0" " bgcolor=#E0E0E0 width=100%>\n" @@ -441,7 +441,7 @@ static void File_transfer_dir(ClientInfo *Client, "<td><b>Modified at</b>\n"); } } else { - sock_handler_write_str(Client->sh, 0, "<br><br>Directory is empty..."); + a_Dpip_dsh_write_str(Client->sh, 0, "<br><br>Directory is empty..."); } /* Output entries */ @@ -451,13 +451,13 @@ static void File_transfer_dir(ClientInfo *Client, if (dList_length(Ddir->flist)) { if (Client->old_style) { - sock_handler_write_str(Client->sh, 0, "</pre>\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "</pre>\n"); } else { - sock_handler_write_str(Client->sh, 0, "</table>\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "</table>\n"); } } - sock_handler_write_str(Client->sh, 0, "</BODY></HTML>\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "</BODY></HTML>\n"); } /* @@ -545,15 +545,16 @@ static void File_send_error_page(ClientInfo *Client, int res, /* Send DPI command */ d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url); - sock_handler_write_str(Client->sh, 1, d_cmd); + a_Dpip_dsh_write_str(Client->sh, 1, d_cmd); dFree(d_cmd); - sock_handler_printf(Client->sh, 0, "HTTP/1.1 %s\r\n" - "Content-Type: text/plain\r\n" - "Content-Length: %ld\r\n" - "\r\n" - "%s", - status, strlen(body), body); + a_Dpip_dsh_printf(Client->sh, 0, + "HTTP/1.1 %s\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: %ld\r\n" + "\r\n" + "%s", + status, strlen(body), body); } /* @@ -626,7 +627,7 @@ static int File_get_file(ClientInfo *Client, /* Send DPI command */ d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url); - sock_handler_write_str(Client->sh, 1, d_cmd); + a_Dpip_dsh_write_str(Client->sh, 1, d_cmd); dFree(d_cmd); /* Check for gzipped file */ @@ -646,23 +647,24 @@ static int File_get_file(ClientInfo *Client, dFree(name); /* Send HTTP headers */ - sock_handler_write_str(Client->sh, 0, "HTTP/1.1 200 OK\r\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "HTTP/1.1 200 OK\r\n"); if (gzipped) { - sock_handler_write_str(Client->sh, 0, "Content-Encoding: gzip\r\n"); + a_Dpip_dsh_write_str(Client->sh, 0, "Content-Encoding: gzip\r\n"); } if (!gzipped || strcmp(ct, unknown_type)) { - sock_handler_printf(Client->sh, 0, "Content-Type: %s\r\n", ct); + a_Dpip_dsh_printf(Client->sh, 0, "Content-Type: %s\r\n", ct); } else { /* If we don't know type for gzipped data, let dillo figure it out. */ } - sock_handler_printf(Client->sh, 0, "Content-Length: %ld\r\n" - "\r\n", - sb->st_size); + a_Dpip_dsh_printf(Client->sh, 0, + "Content-Length: %ld\r\n" + "\r\n", + sb->st_size); /* Send body -- raw file contents */ do { if ((st = read(fd, buf, LBUF)) > 0) { - if (sock_handler_write(Client->sh, 0, buf, (size_t)st) != 0) + if (a_Dpip_dsh_write(Client->sh, 0, buf, (size_t)st) != 0) break; } else if (st < 0) { perror("[read]"); @@ -761,7 +763,7 @@ static void File_toggle_html_style(ClientInfo *Client) OLD_STYLE = !OLD_STYLE; d_cmd = a_Dpip_build_cmd("cmd=%s", "reload_request"); - sock_handler_write_str(Client->sh, 1, d_cmd); + a_Dpip_dsh_write_str(Client->sh, 1, d_cmd); dFree(d_cmd); } @@ -784,7 +786,7 @@ static ClientInfo *File_add_client(int sock_fd) ClientInfo *NewClient; NewClient = dNew(ClientInfo, 1); - NewClient->sh = sock_handler_new(sock_fd, sock_fd, 8*1024); + NewClient->sh = a_Dpip_dsh_new(sock_fd, sock_fd, 8*1024); NewClient->status = 0; NewClient->done = 0; NewClient->old_style = OLD_STYLE; @@ -821,8 +823,8 @@ static void File_remove_client_n(uint_t n) pthread_mutex_unlock(&ClMut); _MSG("Closing Socket Handler\n"); - sock_handler_close(Client->sh); - sock_handler_free(Client->sh); + a_Dpip_dsh_close(Client->sh); + a_Dpip_dsh_free(Client->sh); dFree(Client); } @@ -846,34 +848,32 @@ static int File_num_clients(void) */ static void File_serve_client(void *data) { - char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *path, *auth, *p; + char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *path; ClientInfo *Client = data; + int st; /* Authenticate our client... */ - auth = sock_handler_read(Client->sh); - if ((p = strchr(auth, '<')) != NULL) { - /* auth and dpip's tag are in one chunk, separate them */ - dpip_tag = dStrdup(p); - *p = 0; - } - MSG("auth={%s}\n", auth); - if (a_Dpip_check_auth(auth) == -1) { - dFree(dpip_tag); - dFree(auth); - return; - } - dFree(auth); + do { + dpip_tag = a_Dpip_dsh_read_token(Client->sh); + } while (!dpip_tag && Client->sh->status == DPIP_EAGAIN); + _MSG("auth={%s}\n", dpip_tag); + st = a_Dpip_check_auth(dpip_tag); + dFree(dpip_tag); + _MSG("a_Dpip_check_auth returned %d\n", st); + dReturn_if (st == -1); /* Read the dpi command */ - if (!dpip_tag) - dpip_tag = sock_handler_read(Client->sh); - MSG("dpip_tag={%s}\n", dpip_tag); + do { + dpip_tag = a_Dpip_dsh_read_token(Client->sh); + } while (!dpip_tag && Client->sh->status == DPIP_EAGAIN); + _MSG("dpip_tag={%s}\n", dpip_tag); if (dpip_tag) { cmd = a_Dpip_get_attr(dpip_tag, "cmd"); if (cmd) { if (strcmp(cmd, "DpiBye") == 0) { DPIBYE = 1; + MSG("file.dpi:: (pid %d): Got DpiBye.\n", (int)getpid()); } else { url = a_Dpip_get_attr(dpip_tag, "url"); if (!url) |