diff --git a/src/elogd.c b/src/elogd.c index e8d8237c..7a6fe0cd 100755 --- a/src/elogd.c +++ b/src/elogd.c @@ -6,6 +6,9 @@ Contents: Web server program for Electronic Logbook ELOG $Log$ + Revision 1.8 2003/02/14 15:35:38 midas + Revised cookie handling and absolute paths + Revision 1.7 2003/02/11 15:43:15 midas Revised attachment upload @@ -3439,7 +3442,149 @@ int i; /*------------------------------------------------------------------*/ -void redirect(char *path) +void extract_path(char *str) +{ +char *p, str2[256]; + + if (strstr(str, "http://")) + { + p = str+7; + while (*p && *p != '/') + p++; + if (*p == '/') + p++; + + strcpy(str2, p); + strcpy(str, str2); + if (str[strlen(str)-1] != '/') + strcat(str, "/"); + } +} + +/*------------------------------------------------------------------*/ + +void set_location(LOGBOOK *lbs, char *rel_path) +{ +char str[256]; + + if (strncmp(rel_path, "http://", 7) == 0) + rsprintf("Location: %s", rel_path); + else + { + if (lbs) + getcfg(lbs->name, "URL", str); + else + getcfg("global", "URL", str); + + if (str[0]) + { + /* absolute path */ + if (str[strlen(str)-1] != '/') + strcat(str, "/"); + + rsprintf("Location: %s", str); + + if (strncmp(rel_path, "../", 3) == 0) + rsprintf(rel_path+3); + else if (strcmp(rel_path, ".") == 0) + rsprintf(lbs->name_enc); + else if (rel_path[0] == '/') + rsprintf(rel_path+1); + else + { + if (lbs) + rsprintf("%s/%s", lbs->name_enc, rel_path); + else + rsprintf("%s", rel_path); + } + } + else + /* relative path */ + rsprintf("Location: %s", rel_path); + } + + rsprintf("\r\n\r\nredir\r\n"); +} + +/*------------------------------------------------------------------*/ + +void set_redir(LOGBOOK *lbs, char *redir) +{ +char str[256]; + + /* prepare relative path */ + if (redir[0]) + strcpy(str, redir); + else + { + if (lbs) + sprintf(str, "../%s/", lbs->name_enc); + else + sprintf(str, "."); + } + + set_location(lbs, str); +} + +/*------------------------------------------------------------------*/ + +void set_cookie(LOGBOOK *lbs, char *name, char *value, BOOL global, char *expiration) +{ +char lb_name[256], str[256]; +double exp; +time_t now; +struct tm *gmt; + + if (lbs) + strcpy(lb_name, lbs->name); + else + strcpy(lb_name, "global"); + + rsprintf("Set-Cookie: %s=%s;", name, value); + + /* add path */ + if (global) + { + /* path for all logbooks */ + if (getcfg(lb_name, "URL", str)) + { + extract_path(str); + rsprintf(" path=/%s;", str); + } + else + rsprintf(" path=/;"); + } + else + { + /* path for individual logbook */ + if (getcfg(lb_name, "URL", str)) + { + extract_path(str); + rsprintf(" path=%s%s;", str, lbs->name); + } + else + rsprintf(" path=/%s;", lbs->name); + } + + exp = atof(expiration); + + /* add expriation date */ + if (exp != 0) + { + time(&now); + now += (int) (3600*exp); + gmt = gmtime(&now); + strftime(str, sizeof(str), "%A, %d-%b-%y %H:%M:%S GMT", gmt); + + rsprintf(" expires=%s;", str); + } + + rsprintf("\r\n"); +} + +/*------------------------------------------------------------------*/ + +void redirect(LOGBOOK *lbs, char *rel_path) { /* redirect */ rsprintf("HTTP/1.1 302 Found\r\n"); @@ -3450,15 +3595,7 @@ void redirect(char *path) rsprintf("Keep-Alive: timeout=60, max=10\r\n"); } - rsprintf("Location: %s\r\n\r\nredir\r\n", path); -} - -void redirect2(char *path) -{ - redirect(path); - send(_sock, return_buffer, strlen(return_buffer)+1, 0); - closesocket(_sock); - return_length = -1; + set_location(lbs, rel_path); } /*------------------------------------------------------------------*/ @@ -4006,10 +4143,8 @@ void show_error(char *error) void set_login_cookies(LOGBOOK *lbs, char *user, char *enc_pwd) { -char str[256], str2[256], lb_name[256]; -double exp; -time_t now; -struct tm *gmt; +char str[256], lb_name[256], exp[80]; +BOOL global; rsprintf("HTTP/1.1 302 Found\r\n"); rsprintf("Server: ELOG HTTP %s\r\n", VERSION); @@ -4025,53 +4160,16 @@ struct tm *gmt; strcpy(lb_name, "global"); /* get optional expriation from configuration file */ - exp = 0; - if (getcfg(lb_name, "Login expiration", str)) - exp = atof(str); + getcfg(lb_name, "Login expiration", exp); - if (exp == 0) - { - if (getcfg("global", "Password file", str)) - { - rsprintf("Set-Cookie: upwd=%s; path=/\r\n", enc_pwd); - rsprintf("Set-Cookie: unm=%s; path=/\r\n", user); - } - else - { - rsprintf("Set-Cookie: upwd=%s\r\n", enc_pwd); - rsprintf("Set-Cookie: unm=%s\r\n", user); - } - } - else - { - time(&now); - now += (int) (3600*exp); - gmt = gmtime(&now); - strftime(str, sizeof(str), "%A, %d-%b-%y %H:%M:%S GMT", gmt); + /* check if cookies should be global */ + global = getcfg("global", "Password file", str); - if (getcfg("global", "Password file", str2)) - { - rsprintf("Set-Cookie: upwd=%s; path=/; expires=%s\r\n", enc_pwd, str); - rsprintf("Set-Cookie: unm=%s; path=/; expires=%s\r\n", user, str); - } - else - { - rsprintf("Set-Cookie: upwd=%s; expires=%s\r\n", enc_pwd, str); - rsprintf("Set-Cookie: unm=%s; expires=%s\r\n", user, str); - } - } + /* two cookies for password and user name */ + set_cookie(lbs, "upwd", enc_pwd, global, exp); + set_cookie(lbs, "unm", user, global, exp); - strlcpy(str, getparam("redir"), sizeof(str)); - if (!str[0]) - { - if (lbs) - sprintf(str, "../%s/", lbs->name_enc); - else - sprintf(str, "."); - } - - rsprintf("Location: %s\r\n\r\nredir\r\n", str); - return; + set_redir(lbs, getparam("redir")); } /*------------------------------------------------------------------*/ @@ -4381,7 +4479,7 @@ int i, fh, wrong_pwd, size; if (!wrong_pwd) { - redirect("."); + redirect(lbs, "."); return; } } @@ -5582,6 +5680,8 @@ int i, fh, size, self_register; } else { + if (url[strlen(url)-1] != '/') + strlcat(url, "/", sizeof(url)); strlcat(url, lbs->name, sizeof(url)); strlcat(url, "/", sizeof(url)); } @@ -5661,7 +5761,7 @@ int i, fh, size, self_register; if (self_register == 3) { - redirect("?cmd=Requested"); + redirect(lbs, "?cmd=Requested"); return 0; } } @@ -5849,7 +5949,7 @@ int i; rsprintf("