diff --git a/src/elogd.c b/src/elogd.c index 3404d961..96a74f97 100755 --- a/src/elogd.c +++ b/src/elogd.c @@ -6,6 +6,9 @@ Contents: Web server program for Electronic Logbook ELOG $Log$ + Revision 1.69 2003/04/07 15:26:58 midas + Added password recovery facility + Revision 1.68 2003/04/07 09:25:57 midas Added button 'remember me' @@ -1468,9 +1471,12 @@ char list[1024][NAME_LENGTH]; send(s, str, strlen(str), 0); if (verbose) puts(str); - snprintf(str, strsize - 1, "X-Elog-URL: %s\r\n", url); - send(s, str, strlen(str), 0); - if (verbose) puts(str); + if (url) + { + snprintf(str, strsize - 1, "X-Elog-URL: %s\r\n", url); + send(s, str, strlen(str), 0); + if (verbose) puts(str); + } snprintf(str, strsize - 1, "X-Elog-submit-type: web|elog\r\n"); send(s, str, strlen(str), 0); @@ -4703,20 +4709,11 @@ struct tm *ts; /*------------------------------------------------------------------*/ -void show_change_pwd_page(LOGBOOK *lbs) +BOOL change_pwd(LOGBOOK *lbs, char *user, char *pwd) { -char str[256], str2[256], file_name[256], line[256], *p, *pl, old_pwd[32], - new_pwd[32], new_pwd2[32], user[80]; +char str[256], file_name[256], line[256], *p, *pl; char *buf; -int i, fh, wrong_pwd, size; - - do_crypt(getparam("oldpwd"), old_pwd); - do_crypt(getparam("newpwd"), new_pwd); - do_crypt(getparam("newpwd2"), new_pwd2); - - strcpy(user, getparam("unm")); - if (isparam("config")) - strcpy(user, getparam("config")); +int i, fh, size; getcfg(lbs->name, "Password file", str); @@ -4728,108 +4725,134 @@ int i, fh, wrong_pwd, size; strlcat(file_name, str, sizeof(file_name)); } + fh = open(file_name, O_RDWR | O_BINARY, 644); + if (fh > 0) + { + lseek(fh, 0, SEEK_END); + size = TELL(fh); + lseek(fh, 0, SEEK_SET); + + buf = malloc(size+1); + read(fh, buf, size); + buf[size] = 0; + pl = buf; + + while (pl < buf+size) + { + for (i=0 ; pl[i] && pl[i] != '\r' && pl[i] != '\n' ; i++) + line[i] = pl[i]; + line[i] = 0; + + if (line[0] == ';' || line[0] == '#' || line[0] == 0) + { + pl += strlen(line); + while (*pl && (*pl == '\r' || *pl == '\n')) + pl++; + continue; + } + + strcpy(str, line); + if (strchr(str, ':')) + *strchr(str, ':') = 0; + if (strcmp(str, user) == 0) + break; + + pl += strlen(line); + while (*pl && (*pl == '\r' || *pl == '\n')) + pl++; + } + + /* return if not found */ + if (pl >= buf+size) + { + free(buf); + close(fh); + return FALSE; + } + + p = strchr(line, ':'); + if (p) + p = strchr(p+1, ':'); + if (p == NULL) + return FALSE; + + /* replace password */ + lseek(fh, 0, SEEK_SET); + write(fh, buf, pl-buf); + + sprintf(str, "%s:%s%s\n", user, pwd, p); + write(fh, str, strlen(str)); + + pl += strlen(line); + while (*pl && (*pl == '\r' || *pl == '\n')) + pl++; + + write(fh, pl, strlen(pl)); + +#ifdef _MSC_VER + chsize(fh, TELL(fh)); +#else + ftruncate(fh, TELL(fh)); +#endif + + free(buf); + close(fh); + + return TRUE; + } + + return FALSE; +} + +/*------------------------------------------------------------------*/ + +void show_change_pwd_page(LOGBOOK *lbs) +{ +char str[256], old_pwd[32], + new_pwd[32], new_pwd2[32], act_pwd[32], user[80]; +int wrong_pwd; + + do_crypt(getparam("oldpwd"), old_pwd); + do_crypt(getparam("newpwd"), new_pwd); + do_crypt(getparam("newpwd2"), new_pwd2); + + strcpy(user, getparam("unm")); + if (isparam("config")) + strcpy(user, getparam("config")); + wrong_pwd = FALSE; if (old_pwd[0] || new_pwd[0]) { - fh = open(file_name, O_RDWR | O_BINARY, 644); - if (fh > 0) + if (user[0] && get_user_line(lbs->name, user, act_pwd, NULL, NULL, NULL)) { - lseek(fh, 0, SEEK_END); - size = TELL(fh); - lseek(fh, 0, SEEK_SET); - - buf = malloc(size+1); - read(fh, buf, size); - buf[size] = 0; - pl = buf; - - while (pl < buf+size) + if (getcfg(lbs->name, "Admin user", str) && + strstr(str, user) != 0) + wrong_pwd = 0; + else { - for (i=0 ; pl[i] && pl[i] != '\r' && pl[i] != '\n' ; i++) - line[i] = pl[i]; - line[i] = 0; - - if (line[0] == ';' || line[0] == '#' || line[0] == 0) - { - pl += strlen(line); - while (*pl && (*pl == '\r' || *pl == '\n')) - pl++; - continue; - } - - strcpy(str, line); - if (strchr(str, ':')) - *strchr(str, ':') = 0; - if (strcmp(str, user) == 0) - break; - - pl += strlen(line); - while (*pl && (*pl == '\r' || *pl == '\n')) - pl++; + if (strcmp(old_pwd, act_pwd) != 0) + wrong_pwd = 1; } - /* if user found, check old password */ - if (user[0] && (strcmp(str, user) == 0)) - { - p = line+strlen(str); - if (*p) - p++; + if (strcmp(new_pwd, new_pwd2) != 0) + wrong_pwd = 2; + } - strcpy(str2, p); - if (strchr(str2, ':')) - *strchr(str2, ':') = 0; + /* replace password */ + if (!wrong_pwd) + change_pwd(lbs, user, new_pwd); - if (getcfg(lbs->name, "Admin user", str) && - strstr(str, getparam("unm")) != 0) - wrong_pwd = 0; - else - { - if (strcmp(old_pwd, str2) != 0) - wrong_pwd = 1; - } + if (!wrong_pwd && strcmp(user, getparam("unm")) == 0) + { + set_login_cookies(lbs, user, new_pwd); + return; + } - if (strcmp(new_pwd, new_pwd2) != 0) - wrong_pwd = 2; - } - - /* replace password */ - if (!wrong_pwd) - { - lseek(fh, 0, SEEK_SET); - write(fh, buf, pl-buf); - - sprintf(str, "%s:%s:%s:%s\n", user, new_pwd, - getparam("full_name"), getparam("user_email")); - write(fh, str, strlen(str)); - - pl += strlen(line); - while (*pl && (*pl == '\r' || *pl == '\n')) - pl++; - - write(fh, pl, strlen(pl)); - -#ifdef _MSC_VER - chsize(fh, TELL(fh)); -#else - ftruncate(fh, TELL(fh)); -#endif - } - - free(buf); - close(fh); - - if (!wrong_pwd && strcmp(user, getparam("unm")) == 0) - { - set_login_cookies(lbs, user, new_pwd); - return; - } - - if (!wrong_pwd) - { - redirect(lbs, "."); - return; - } + if (!wrong_pwd) + { + redirect(lbs, "."); + return; } } @@ -4852,8 +4875,14 @@ int i, fh, wrong_pwd, size; if (!getcfg(lbs->name, "Admin user", str) || !strstr(str, getparam("unm")) != 0) { - rsprintf("