Keep parsed password files in memory

SVN revision: 1233
This commit is contained in:
Stefan Ritt 2005-03-03 15:33:05 +00:00
parent dbdbed76ea
commit 9229e610b7

View File

@ -6,6 +6,9 @@
Contents: Web server program for Electronic Logbook ELOG Contents: Web server program for Electronic Logbook ELOG
$Log$ $Log$
Revision 1.579 2005/03/03 15:33:05 ritt
Keep parsed password files in memory
Revision 1.578 2005/03/02 21:03:16 ritt Revision 1.578 2005/03/02 21:03:16 ritt
Implemented email subscriptions for individual logbooks Implemented email subscriptions for individual logbooks
@ -1189,6 +1192,7 @@ typedef struct {
EL_INDEX *el_index; EL_INDEX *el_index;
int *n_el_index; int *n_el_index;
int n_attr; int n_attr;
PMXML_NODE pwd_xml_tree;
} LOGBOOK; } LOGBOOK;
typedef struct { typedef struct {
@ -1220,7 +1224,7 @@ int run_service(void);
void show_error(char *error); void show_error(char *error);
BOOL enum_user_line(LOGBOOK *lbs, int n, char *user, int size); BOOL enum_user_line(LOGBOOK *lbs, int n, char *user, int size);
int get_user_line(char *logbook_name, char *user, char *password, char *full_name, int get_user_line(LOGBOOK *lbs, char *user, char *password, char *full_name,
char *email, BOOL email_notify[1000]); char *email, BOOL email_notify[1000]);
int strbreak(char *str, char list[][NAME_LENGTH], int size, char *brk); int strbreak(char *str, char list[][NAME_LENGTH], int size, char *brk);
int execute_shell(LOGBOOK *lbs, int message_id, char attrib[MAX_N_ATTR][NAME_LENGTH], int execute_shell(LOGBOOK *lbs, int message_id, char attrib[MAX_N_ATTR][NAME_LENGTH],
@ -1245,7 +1249,7 @@ int build_subst_list(LOGBOOK * lbs, char list[][NAME_LENGTH], char value[][NAME_
char attrib[][NAME_LENGTH], BOOL format_date); char attrib[][NAME_LENGTH], BOOL format_date);
void highlight_searchtext(regex_t * re_buf, char *src, char *dst, BOOL hidden); void highlight_searchtext(regex_t * re_buf, char *src, char *dst, BOOL hidden);
int parse_config_file(char *config_file); int parse_config_file(char *config_file);
PMXML_NODE load_password_file(char *logbook_name); PMXML_NODE load_password_file(LOGBOOK *lbs);
/*---- Funcions from the MIDAS library -----------------------------*/ /*---- Funcions from the MIDAS library -----------------------------*/
@ -2893,6 +2897,7 @@ INT ss_daemon_init()
typedef struct { typedef struct {
char *param; char *param;
char *uparam;
char *value; char *value;
} CONFIG_PARAM; } CONFIG_PARAM;
@ -3022,7 +3027,7 @@ BOOL match_param(char *str, char *param, int conditional_only)
int param_compare(const void *p1, const void *p2) int param_compare(const void *p1, const void *p2)
{ {
return stricmp(((CONFIG_PARAM *)p1)->param, ((CONFIG_PARAM *)p2)->param); return stricmp(((CONFIG_PARAM *)p1)->uparam, ((CONFIG_PARAM *)p2)->uparam);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
@ -3034,6 +3039,7 @@ void free_config()
for (i=0 ; i<n_lb_config ; i++) { for (i=0 ; i<n_lb_config ; i++) {
for (j=0 ; j<lb_config[i].n_params ; j++) { for (j=0 ; j<lb_config[i].n_params ; j++) {
xfree(lb_config[i].config_param[j].param); xfree(lb_config[i].config_param[j].param);
xfree(lb_config[i].config_param[j].uparam);
xfree(lb_config[i].config_param[j].value); xfree(lb_config[i].config_param[j].value);
} }
xfree(lb_config[i].config_param); xfree(lb_config[i].config_param);
@ -3110,10 +3116,12 @@ int parse_config_file(char *file_name)
lb_config[n_lb_config].config_param = xrealloc(lb_config[n_lb_config].config_param, lb_config[n_lb_config].config_param = xrealloc(lb_config[n_lb_config].config_param,
sizeof(CONFIG_PARAM)*(lb_config[n_lb_config].n_params+1)); sizeof(CONFIG_PARAM)*(lb_config[n_lb_config].n_params+1));
lb_config[n_lb_config].config_param[i].param = xmalloc(strlen(str)+1); lb_config[n_lb_config].config_param[i].param = xmalloc(strlen(str)+1);
lb_config[n_lb_config].config_param[i].uparam = xmalloc(strlen(str)+1);
strcpy(lb_config[n_lb_config].config_param[i].param, str);
for (j=0 ; j<(int)strlen(str) ; j++) for (j=0 ; j<(int)strlen(str) ; j++)
lb_config[n_lb_config].config_param[i].param[j] = toupper(str[j]); lb_config[n_lb_config].config_param[i].uparam[j] = toupper(str[j]);
lb_config[n_lb_config].config_param[i].param[j] = 0; lb_config[n_lb_config].config_param[i].uparam[j] = 0;
p++; p++;
while (*p == ' ' || *p == '\t') while (*p == ' ' || *p == '\t')
@ -3179,8 +3187,8 @@ int getcfg_simple(char *group, char *param, char *value, int vsize, int conditio
return 0; return 0;
for (j=0 ; j<lb_config[i].n_params ; j++) for (j=0 ; j<lb_config[i].n_params ; j++)
if (match_param(lb_config[i].config_param[j].param, uparam, conditional)) { if (match_param(lb_config[i].config_param[j].uparam, uparam, conditional)) {
status = strchr(lb_config[i].config_param[j].param, '{') ? 2 : 1; status = strchr(lb_config[i].config_param[j].uparam, '{') ? 2 : 1;
strlcpy(value, lb_config[i].config_param[j].value, vsize); strlcpy(value, lb_config[i].config_param[j].value, vsize);
return status; return status;
} }
@ -3607,7 +3615,7 @@ void retrieve_email_from(LOGBOOK * lbs, char *ret, char attrib[MAX_N_ATTR][NAME_
for (i = 0;; i++) { for (i = 0;; i++) {
if (!enum_user_line(lbs, i, login_name, sizeof(login_name))) if (!enum_user_line(lbs, i, login_name, sizeof(login_name)))
break; break;
get_user_line(lbs->name, login_name, NULL, NULL, str, NULL); get_user_line(lbs, login_name, NULL, NULL, str, NULL);
if (is_admin_user(lbs->name, login_name) && strchr(str, '@')) if (is_admin_user(lbs->name, login_name) && strchr(str, '@'))
break; break;
} }
@ -7286,26 +7294,44 @@ void add_subst_time(LOGBOOK * lbs,
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
BOOL get_password_file(LOGBOOK *lbs, char *file_name, int size)
{
char str[256];
getcfg(lbs->name, "Password file", str, sizeof(str));
if (!str[0])
return FALSE;
if (str[0] == DIR_SEPARATOR || str[1] == ':')
strlcpy(file_name, str, size);
else {
strlcpy(file_name, resource_dir, size);
strlcat(file_name, str, size);
}
return TRUE;
}
/*------------------------------------------------------------------*/
BOOL change_pwd(LOGBOOK *lbs, char *user, char *pwd) BOOL change_pwd(LOGBOOK *lbs, char *user, char *pwd)
{ {
char str[256], file_name[256]; char str[256], file_name[256];
PMXML_NODE xml_tree, node; PMXML_NODE node;
xml_tree = load_password_file(lbs->name); if (!lbs->pwd_xml_tree)
if (!xml_tree)
return FALSE; return FALSE;
sprintf(str, "/list/user[name=%s]/password", user); sprintf(str, "/list/user[name=%s]/password", user);
node = mxml_find_node(xml_tree, str); node = mxml_find_node(lbs->pwd_xml_tree, str);
if (node == NULL) { if (node == NULL)
mxml_free_tree(xml_tree);
return FALSE; return FALSE;
}
mxml_replace_node_value(node, pwd); mxml_replace_node_value(node, pwd);
mxml_write_tree(file_name, xml_tree); if (get_password_file(lbs, file_name, sizeof(file_name)))
mxml_free_tree(xml_tree); mxml_write_tree(file_name, lbs->pwd_xml_tree);
return TRUE; return TRUE;
} }
@ -7334,7 +7360,7 @@ void show_change_pwd_page(LOGBOOK * lbs)
if (old_pwd[0] || new_pwd[0]) { if (old_pwd[0] || new_pwd[0]) {
if (user[0] if (user[0]
&& get_user_line(lbs->name, user, act_pwd, NULL, NULL, NULL)) { && get_user_line(lbs, user, act_pwd, NULL, NULL, NULL)) {
/* administrator does not have to supply old password if changing other user's password */ /* administrator does not have to supply old password if changing other user's password */
if (is_admin_user(lbs->name, getparam("unm")) if (is_admin_user(lbs->name, getparam("unm"))
@ -8321,7 +8347,7 @@ void show_edit_form(LOGBOOK * lbs, int message_id, BOOL breply, BOOL bedit, BOOL
for (i = 0;; i++) { for (i = 0;; i++) {
if (!enum_user_line(lbs, i, login_name, sizeof(login_name))) if (!enum_user_line(lbs, i, login_name, sizeof(login_name)))
break; break;
get_user_line(lbs->name, login_name, NULL, str, NULL, NULL); get_user_line(lbs, login_name, NULL, str, NULL, NULL);
if (strieq(str, attrib[index])) if (strieq(str, attrib[index]))
rsprintf("<option selected value=\"%s\">%s\n", str, str); rsprintf("<option selected value=\"%s\">%s\n", str, str);
@ -9903,7 +9929,7 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
char smtp_host[256], email_addr[256], mail_from[256], subject[256], mail_text[2000]; char smtp_host[256], email_addr[256], mail_from[256], subject[256], mail_text[2000];
char admin_user[80], enc_pwd[80], url[256]; char admin_user[80], enc_pwd[80], url[256];
int i, self_register; int i, self_register;
PMXML_NODE xml_tree, node, subnode; PMXML_NODE node, subnode;
/* check for full name */ /* check for full name */
if (!isparam("new_full_name") || *getparam("new_full_name") == 0) { if (!isparam("new_full_name") || *getparam("new_full_name") == 0) {
@ -9934,7 +9960,7 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
/* check if user exists */ /* check if user exists */
if (new_user && self_register == 3) { if (new_user && self_register == 3) {
if (get_user_line(lbs->name, user, NULL, NULL, NULL, NULL) == 1) { if (get_user_line(lbs, user, NULL, NULL, NULL, NULL) == 1) {
sprintf(str, "%s \"%s\" %s", loc("Login name"), user, loc("exists already")); sprintf(str, "%s \"%s\" %s", loc("Login name"), user, loc("exists already"));
show_error(str); show_error(str);
return 0; return 0;
@ -9945,24 +9971,20 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
if (activate || !new_user || self_register != 3) { /* do not save in mode 3 */ if (activate || !new_user || self_register != 3) { /* do not save in mode 3 */
getcfg(lbs->name, "Password file", str, sizeof(str)); getcfg(lbs->name, "Password file", str, sizeof(str));
xml_tree = load_password_file(lbs->name); if (lbs->pwd_xml_tree == NULL)
if (xml_tree == NULL) {
show_error(str);
return 0; return 0;
}
sprintf(str, "/list/user[name=%s]", user); sprintf(str, "/list/user[name=%s]", user);
node = mxml_find_node(xml_tree, str); node = mxml_find_node(lbs->pwd_xml_tree, str);
if (node && new_user) { if (node && new_user) {
sprintf(str, "%s \"%s\" %s", loc("Login name"), user, loc("exists already")); sprintf(str, "%s \"%s\" %s", loc("Login name"), user, loc("exists already"));
show_error(str); show_error(str);
mxml_free_tree(xml_tree);
return 0; return 0;
} }
if (new_user) { if (new_user) {
node = mxml_add_node(xml_tree, "user", NULL); node = mxml_add_node(lbs->pwd_xml_tree, "user", NULL);
mxml_add_node(node, "full_name", getparam("new_full_name")); mxml_add_node(node, "full_name", getparam("new_full_name"));
mxml_add_node(node, "name", getparam("new_user_name")); mxml_add_node(node, "name", getparam("new_user_name"));
@ -9985,22 +10007,15 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
if (subnode) if (subnode)
mxml_delete_node(subnode); mxml_delete_node(subnode);
mxml_add_node(node, "email_notify", NULL); mxml_add_node(node, "email_notify", NULL);
subnode = mxml_find_node(node, "email_notify");
for (i=0 ; lb_list[i].name[0] ; i++) { for (i=0 ; lb_list[i].name[0] ; i++) {
sprintf(str, "sub_lb%d", i); sprintf(str, "sub_lb%d", i);
if (getparam(str) && atoi(getparam(str))) if (getparam(str) && atoi(getparam(str)))
mxml_add_node(subnode, "logbook", lb_list[i].name); mxml_add_node(subnode, "logbook", lb_list[i].name);
} }
getcfg(lbs->name, "Password file", str, sizeof(str)); if (get_password_file(lbs, file_name, sizeof(file_name)))
if (str[0] == DIR_SEPARATOR || str[1] == ':') mxml_write_tree(file_name, lbs->pwd_xml_tree);
strlcpy(file_name, str, sizeof(file_name));
else {
strlcpy(file_name, resource_dir, sizeof(file_name));
strlcat(file_name, str, sizeof(file_name));
}
mxml_write_tree(file_name, xml_tree);
mxml_free_tree(xml_tree);
} }
/* if requested, send notification email to admin user */ /* if requested, send notification email to admin user */
@ -10045,7 +10060,7 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
if (getcfg(lbs->name, "Admin user", admin_user, sizeof(admin_user))) { if (getcfg(lbs->name, "Admin user", admin_user, sizeof(admin_user))) {
pl = strtok(admin_user, " ,"); pl = strtok(admin_user, " ,");
while (pl) { while (pl) {
get_user_line(lbs->name, pl, NULL, NULL, email_addr, NULL); get_user_line(lbs, pl, NULL, NULL, email_addr, NULL);
if (email_addr[0]) { if (email_addr[0]) {
/* compose subject */ /* compose subject */
if (self_register == 3) { if (self_register == 3) {
@ -10136,27 +10151,21 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
int remove_user(LOGBOOK *lbs, char *user) int remove_user(LOGBOOK *lbs, char *user)
{ {
char file_name[256], str[256]; char file_name[256], str[256];
PMXML_NODE xml_tree, node; PMXML_NODE node;
xml_tree = load_password_file(lbs->name); if (lbs->pwd_xml_tree == NULL)
if (xml_tree == NULL)
return FALSE; return FALSE;
sprintf(str, "/list/user[name=%s]", user); sprintf(str, "/list/user[name=%s]", user);
node = mxml_find_node(xml_tree, str); node = mxml_find_node(lbs->pwd_xml_tree, str);
if (node == NULL) { if (node == NULL)
mxml_free_tree(xml_tree);
return FALSE; return FALSE;
}
mxml_delete_node(node); mxml_delete_node(node);
if (!mxml_write_tree(file_name, xml_tree)) { if (!mxml_write_tree(file_name, lbs->pwd_xml_tree))
mxml_free_tree(xml_tree);
return FALSE; return FALSE;
}
mxml_free_tree(xml_tree);
return TRUE; return TRUE;
} }
@ -10256,7 +10265,7 @@ void show_config_page(LOGBOOK * lbs)
rsprintf("<tr><td nowrap width=\"15%%\">%s:</td>\n", loc("Login name")); rsprintf("<tr><td nowrap width=\"15%%\">%s:</td>\n", loc("Login name"));
if (get_user_line(logbook, user, password, full_name, user_email, email_notify) != 1) if (get_user_line(lbs, user, password, full_name, user_email, email_notify) != 1)
sprintf(str, loc("User [%s] has been deleted"), user); sprintf(str, loc("User [%s] has been deleted"), user);
else else
strcpy(str, user); strcpy(str, user);
@ -10280,7 +10289,7 @@ void show_config_page(LOGBOOK * lbs)
for (i = 0; lb_list[i].name[0]; i++) { for (i = 0; lb_list[i].name[0]; i++) {
if (!getcfg_topgroup() || strieq(getcfg_topgroup(), lb_list[i].name)) { if (!getcfg_topgroup() || strieq(getcfg_topgroup(), lb_list[i].top_group)) {
if (email_notify[i]) if (email_notify[i])
rsprintf("<input type=checkbox checked id=\"lb%d\" name=\"sub_lb%d\" value=\"1\">\n", i, i); rsprintf("<input type=checkbox checked id=\"lb%d\" name=\"sub_lb%d\" value=\"1\">\n", i, i);
@ -10290,6 +10299,32 @@ void show_config_page(LOGBOOK * lbs)
} }
} }
if (i > 2) {
rsprintf("<script language=\"JavaScript\" type=\"text/javascript\">\n");
rsprintf("<!--\n");
rsprintf("function SetNone()\n");
rsprintf(" {\n");
rsprintf(" for (var i = 0; i < document.form1.elements.length; i++)\n");
rsprintf(" {\n");
rsprintf(" if( document.form1.elements[i].type == 'checkbox' )\n");
rsprintf(" document.form1.elements[i].checked = false;\n");
rsprintf(" }\n");
rsprintf(" }\n");
rsprintf("function SetAll()\n");
rsprintf(" {\n");
rsprintf(" for (var i = 0; i < document.form1.elements.length; i++)\n");
rsprintf(" {\n");
rsprintf(" if( document.form1.elements[i].type == 'checkbox' )\n");
rsprintf(" document.form1.elements[i].checked = true;\n");
rsprintf(" }\n");
rsprintf(" }\n");
rsprintf("//-->\n");
rsprintf("</script>\n");
rsprintf("<input type=button value=\"%s\" onClick=\"SetAll();\">\n", loc("Set all"));
rsprintf("<input type=button value=\"%s\" onClick=\"SetNone();\">\n", loc("Set none"));
}
rsprintf("</td></tr>\n"); rsprintf("</td></tr>\n");
rsprintf("</table></td></tr>\n"); rsprintf("</table></td></tr>\n");
@ -10330,7 +10365,7 @@ void show_forgot_pwd_page(LOGBOOK * lbs)
if (!enum_user_line(lbs, i, login_name, sizeof(login_name))) if (!enum_user_line(lbs, i, login_name, sizeof(login_name)))
break; break;
get_user_line(lbs->name, login_name, NULL, full_name, user_email, NULL); get_user_line(lbs, login_name, NULL, full_name, user_email, NULL);
if (strieq(name, login_name) if (strieq(name, login_name)
|| strieq(name, full_name) || strieq(name, full_name)
@ -13246,7 +13281,7 @@ void synchronize(LOGBOOK * lbs, int mode)
/* if called by cron, set user name and password */ /* if called by cron, set user name and password */
if (mode == SYNC_CRON && getcfg(lb_list[i].name, "mirror user", str, sizeof(str))) { if (mode == SYNC_CRON && getcfg(lb_list[i].name, "mirror user", str, sizeof(str))) {
if (get_user_line(lb_list[i].name, str, pwd, NULL, NULL, NULL) == EL_SUCCESS) { if (get_user_line(&lb_list[i], str, pwd, NULL, NULL, NULL) == EL_SUCCESS) {
setparam("unm", str); setparam("unm", str);
setparam("upwd", pwd); setparam("upwd", pwd);
} }
@ -17035,7 +17070,7 @@ void submit_elog(LOGBOOK * lbs)
if (!enum_user_line(lbs, index, user, sizeof(user))) if (!enum_user_line(lbs, index, user, sizeof(user)))
break; break;
get_user_line(lbs->name, user, NULL, NULL, user_email, email_notify); get_user_line(lbs, user, NULL, NULL, user_email, email_notify);
for (i=0 ; lb_list[i].name[0] && i<1000 ; i++) for (i=0 ; lb_list[i].name[0] && i<1000 ; i++)
if (strieq(lb_list[i].name, lbs->name)) if (strieq(lb_list[i].name, lbs->name))
@ -18314,6 +18349,15 @@ BOOL convert_password_file(char *file_name)
buf[i] = 0; buf[i] = 0;
close(fh); close(fh);
/* create backup */
strlcpy(name, file_name, sizeof(name));
strlcat(name, "_bak", sizeof(name));
fh = open(name, O_WRONLY | O_TEXT | O_CREAT, 0644);
if (fh > 0) {
write(fh, buf, len);
close(fh);
}
p = buf; p = buf;
/* skip leading spaces or new lines */ /* skip leading spaces or new lines */
@ -18327,7 +18371,7 @@ BOOL convert_password_file(char *file_name)
/* skip comment lines */ /* skip comment lines */
if (*p != ';' && *p != '#') { if (*p != ';' && *p != '#') {
for (i=0 ; i<(int)sizeof(name) && *p && *p != ':' ; i++) for (i=0 ; i<(int)sizeof(name)-1 && *p && *p != ':' ; i++)
name[i] = *p++; name[i] = *p++;
name[i] = 0; name[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
@ -18335,7 +18379,7 @@ BOOL convert_password_file(char *file_name)
return FALSE; return FALSE;
} }
for (i=0 ; i<(int)sizeof(password) && *p && *p != ':' ; i++) for (i=0 ; i<(int)sizeof(password)-1 && *p && *p != ':' ; i++)
password[i] = *p++; password[i] = *p++;
password[i] = 0; password[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
@ -18343,7 +18387,7 @@ BOOL convert_password_file(char *file_name)
return FALSE; return FALSE;
} }
for (i=0 ; i<(int)sizeof(full_name) && *p && *p != ':' ; i++) for (i=0 ; i<(int)sizeof(full_name)-1 && *p && *p != ':' ; i++)
full_name[i] = *p++; full_name[i] = *p++;
full_name[i] = 0; full_name[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
@ -18351,7 +18395,7 @@ BOOL convert_password_file(char *file_name)
return FALSE; return FALSE;
} }
for (i=0 ; i<(int)sizeof(email) && *p && *p != ':' ; i++) for (i=0 ; i<(int)sizeof(email)-1 && *p && *p != ':' ; i++)
email[i] = *p++; email[i] = *p++;
email[i] = 0; email[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
@ -18359,7 +18403,7 @@ BOOL convert_password_file(char *file_name)
return FALSE; return FALSE;
} }
for (i=0 ; i<(int)sizeof(email_notify) && *p && *p != '\n' ; i++) for (i=0 ; i<(int)sizeof(email_notify)-1 && *p && *p != '\n' ; i++)
email_notify[i] = *p++; email_notify[i] = *p++;
email_notify[i] = 0; email_notify[i] = 0;
if (*p++ != '\n') { if (*p++ != '\n') {
@ -18388,24 +18432,15 @@ BOOL convert_password_file(char *file_name)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
PMXML_NODE load_password_file(char *logbook_name) PMXML_NODE load_password_file(LOGBOOK *lbs)
{ {
PMXML_NODE xml_tree; PMXML_NODE xml_tree;
char str[256], line[256], file_name[256]; char str[256], line[256], file_name[256];
int fh; int fh;
getcfg(logbook_name, "Password file", str, sizeof(str)); if (!get_password_file(lbs, file_name, sizeof(file_name)))
if (!str[0])
return NULL; return NULL;
if (str[0] == DIR_SEPARATOR || str[1] == ':')
strlcpy(file_name, str, sizeof(file_name));
else {
strlcpy(file_name, resource_dir, sizeof(file_name));
strlcat(file_name, str, sizeof(file_name));
}
fh = open(file_name, O_RDONLY); fh = open(file_name, O_RDONLY);
/* if password file doen't exist, try to create it */ /* if password file doen't exist, try to create it */
@ -18449,12 +18484,49 @@ PMXML_NODE load_password_file(char *logbook_name)
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
int get_user_line(char *logbook_name, char *user, char *password, char *full_name, void load_password_files()
{
int i, j;
char str1[256], str2[256];
PMXML_NODE xml_tree;
for (i=0 ; lb_list[i].name[0] ; i++) {
if (lb_list[i].pwd_xml_tree == NULL) {
if (lb_list[i].top_group[0])
setcfg_topgroup(lb_list[i].top_group);
xml_tree = load_password_file(&lb_list[i]);
if (xml_tree) {
lb_list[i].pwd_xml_tree = xml_tree;
/* if other logbook has same password file, copy pointer */
if (lb_list[i].top_group[0])
setcfg_topgroup(lb_list[i].top_group);
getcfg(lb_list[i].name, "Password file", str1, sizeof(str1));
printf("Logbook %s has password file %s\n", lb_list[i].name, str1);
for (j=i+1 ; lb_list[j].name[0] ; j++) {
if (lb_list[j].top_group[0])
setcfg_topgroup(lb_list[j].top_group);
getcfg(lb_list[j].name, "Password file", str2, sizeof(str2));
if (strieq(str1, str2)) {
lb_list[j].pwd_xml_tree = xml_tree;
printf(" ==> assigned to logbook %s\n", lb_list[j].name);
}
}
}
}
}
}
/*------------------------------------------------------------------*/
int get_user_line(LOGBOOK *lbs, char *user, char *password, char *full_name,
char *email, BOOL email_notify[1000]) char *email, BOOL email_notify[1000])
{ {
int i, j; int i, j;
char str[256]; char str[256], global[256];
PMXML_NODE xml_tree, user_node, node, subnode; PMXML_NODE user_node, node, subnode;
if (password) if (password)
password[0] = 0; password[0] = 0;
@ -18465,19 +18537,31 @@ int get_user_line(char *logbook_name, char *user, char *password, char *full_nam
if (email_notify) if (email_notify)
email_notify[0] = 0; email_notify[0] = 0;
getcfg(logbook_name, "Password file", str, sizeof(str)); /* if global password file is requested, search for first
logbook with same password file than global section */
if (lbs == NULL) {
getcfg("global", "Password file", global, sizeof(global));
for (i=0 ; lb_list[i].name[0] ; i++) {
getcfg(lb_list[i].name, "Password file", str, sizeof(str));
if (strieq(str, global)) {
lbs = lb_list+i;
break;
}
}
if (!lb_list[i].name[0])
return 1;
}
getcfg(lbs->name, "Password file", str, sizeof(str));
if (!str[0] || !user[0]) if (!str[0] || !user[0])
return 1; return 1;
xml_tree = load_password_file(logbook_name); if (lbs->pwd_xml_tree) {
if (xml_tree) {
sprintf(str, "/list/user[name=%s]", user); sprintf(str, "/list/user[name=%s]", user);
if ((user_node = mxml_find_node(xml_tree, str)) == NULL) { if ((user_node = mxml_find_node(lbs->pwd_xml_tree, str)) == NULL)
mxml_free_tree(xml_tree);
return 2; return 2;
}
/* if user found, retrieve other info */ /* if user found, retrieve other info */
if ((node = mxml_find_node(user_node, "password")) != NULL && password && mxml_get_value(node)) if ((node = mxml_find_node(user_node, "password")) != NULL && password && mxml_get_value(node))
@ -18510,7 +18594,6 @@ int get_user_line(char *logbook_name, char *user, char *password, char *full_nam
} }
} }
mxml_free_tree(xml_tree);
return 1; return 1;
} else { } else {
if (user[0]) if (user[0])
@ -18525,27 +18608,16 @@ int get_user_line(char *logbook_name, char *user, char *password, char *full_nam
BOOL enum_user_line(LOGBOOK *lbs, int n, char *user, int size) BOOL enum_user_line(LOGBOOK *lbs, int n, char *user, int size)
{ {
char str[256]; char str[256];
PMXML_NODE xml_tree, node; PMXML_NODE node;
getcfg(lbs->name, "Password file", str, sizeof(str)); if (lbs->pwd_xml_tree == NULL)
if (!str[0])
return FALSE; return FALSE;
xml_tree = load_password_file(lbs->name);
if (!xml_tree) {
show_error(str);
return FALSE;
}
sprintf(str, "/list/user[%d]/name", n); sprintf(str, "/list/user[%d]/name", n);
if ((node = mxml_find_node(xml_tree, str)) == NULL) { if ((node = mxml_find_node(lbs->pwd_xml_tree, str)) == NULL)
mxml_free_tree(xml_tree);
return FALSE; return FALSE;
}
strlcpy(user, mxml_get_value(node), size); strlcpy(user, mxml_get_value(node), size);
mxml_free_tree(xml_tree);
return TRUE; return TRUE;
} }
@ -18628,9 +18700,9 @@ BOOL check_user_password(LOGBOOK * lbs, char *user, char *password, char *redir)
int status; int status;
if (lbs == NULL) if (lbs == NULL)
status = get_user_line("global", user, upwd, full_name, email, NULL); status = get_user_line(NULL, user, upwd, full_name, email, NULL);
else else
status = get_user_line(lbs->name, user, upwd, full_name, email, NULL); status = get_user_line(lbs, user, upwd, full_name, email, NULL);
/* check for logout */ /* check for logout */
if (isparam("LO")) { if (isparam("LO")) {
@ -19979,15 +20051,7 @@ void interprete(char *lbook, char *path)
} }
if (strieq(command, "GetPwdFile")) { if (strieq(command, "GetPwdFile")) {
getcfg(lbs->name, "Password file", str, sizeof(str)); if (get_password_file(lbs, file_name, sizeof(file_name)))
if (str[0] == DIR_SEPARATOR || str[1] == ':')
strcpy(file_name, str);
else {
strlcpy(file_name, resource_dir, sizeof(file_name));
strlcat(file_name, str, sizeof(file_name));
}
send_file_direct(file_name); send_file_direct(file_name);
return; return;
} }
@ -20002,7 +20066,7 @@ void interprete(char *lbook, char *path)
if (strieq(command, loc("Save"))) { if (strieq(command, loc("Save"))) {
if (isparam("config")) { if (isparam("config")) {
if (!strieq(getparam("config"), getparam("new_user_name"))) { if (!strieq(getparam("config"), getparam("new_user_name"))) {
if (get_user_line(lbs->name, getparam("new_user_name"), NULL, NULL, NULL, NULL) == 1) { if (get_user_line(lbs, getparam("new_user_name"), NULL, NULL, NULL, NULL) == 1) {
sprintf(str, "%s \"%s\" %s", loc("Login name"), sprintf(str, "%s \"%s\" %s", loc("Login name"),
getparam("new_user_name"), loc("exists already")); getparam("new_user_name"), loc("exists already"));
show_error(str); show_error(str);
@ -20688,6 +20752,9 @@ void server_loop(void)
if (!verbose && !running_as_daemon) if (!verbose && !running_as_daemon)
eputs("done"); eputs("done");
/* load password files */
load_password_files();
/* listen for connection */ /* listen for connection */
status = listen(lsock, SOMAXCONN); status = listen(lsock, SOMAXCONN);
if (status < 0) { if (status < 0) {