diff --git a/src/elogd.c b/src/elogd.c
index a3b83c23..c920df5a 100755
--- a/src/elogd.c
+++ b/src/elogd.c
@@ -6,6 +6,9 @@
Contents: Web server program for Electronic Logbook ELOG
$Log$
+ Revision 1.577 2005/03/02 17:13:24 ritt
+ Implemented central load_password_file()
+
Revision 1.576 2005/03/02 16:29:20 ritt
Encode '&' correctly if present in 'Start page' option
@@ -1239,6 +1242,7 @@ int build_subst_list(LOGBOOK * lbs, char list[][NAME_LENGTH], char value[][NAME_
char attrib[][NAME_LENGTH], BOOL format_date);
void highlight_searchtext(regex_t * re_buf, char *src, char *dst, BOOL hidden);
int parse_config_file(char *config_file);
+PMXML_NODE load_password_file(char *logbook_name);
/*---- Funcions from the MIDAS library -----------------------------*/
@@ -7284,16 +7288,7 @@ BOOL change_pwd(LOGBOOK * lbs, char *user, char *pwd)
char str[256], file_name[256];
PMXML_NODE xml_tree, node;
- getcfg(lbs->name, "Password file", str, sizeof(str));
-
- 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));
- }
-
- xml_tree = mxml_parse_file(file_name, str, sizeof(str));
+ xml_tree = load_password_file(lbs->name);
if (!xml_tree)
return FALSE;
@@ -9947,14 +9942,7 @@ 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 */
getcfg(lbs->name, "Password file", str, sizeof(str));
- 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));
- }
-
- xml_tree = mxml_parse_file(file_name, str, sizeof(str));
+ xml_tree = load_password_file(lbs->name);
if (xml_tree == NULL) {
show_error(str);
return 0;
@@ -9996,6 +9984,14 @@ int save_user_config(LOGBOOK * lbs, char *user, BOOL new_user, BOOL activate)
mxml_replace_subvalue(node, "email_notify", getparam("email_notify"));
}
+ getcfg(lbs->name, "Password file", str, sizeof(str));
+ 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));
+ }
+
mxml_write_tree(file_name, xml_tree);
mxml_free_tree(xml_tree);
}
@@ -10135,16 +10131,7 @@ int remove_user(LOGBOOK * lbs, char *user)
char file_name[256], str[256];
PMXML_NODE xml_tree, node;
- getcfg(lbs->name, "Password file", str, sizeof(str));
-
- 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));
- }
-
- xml_tree = mxml_parse_file(file_name, str, sizeof(str));
+ xml_tree = load_password_file(lbs->name);
if (xml_tree == NULL)
return FALSE;
@@ -18375,12 +18362,71 @@ BOOL convert_password_file(char *file_name)
/*------------------------------------------------------------------*/
+PMXML_NODE load_password_file(char *logbook_name)
+{
+ PMXML_NODE xml_tree;
+ char str[256], line[256], file_name[256];
+ int fh;
+
+ getcfg(logbook_name, "Password file", str, sizeof(str));
+
+ if (!str[0])
+ 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);
+
+ /* if password file doen't exist, try to create it */
+ if (fh < 0) {
+ fh = open(file_name, O_CREAT | O_RDWR, 0600);
+ if (fh < 0) {
+ sprintf(str, loc("Cannot open file %s"), file_name);
+ strcat(str, ": ");
+ strlcat(str, strerror(errno), sizeof(str));
+ show_error(str);
+ return NULL;
+ }
+ close(fh);
+ fh = open(file_name, O_RDONLY);
+ }
+
+ if (fh < 0) {
+ sprintf(str, loc("Cannot open file %s"), file_name);
+ strcat(str, ": ");
+ strlcat(str, strerror(errno), sizeof(str));
+ show_error(str);
+ return NULL;
+ }
+
+ /* check if in XML format, otherwise convert it */
+ line[0] = 0;
+ read(fh, line, sizeof(line));
+ close(fh);
+ if (strstr(line, "name, "Password file", str, sizeof(str));
@@ -18473,14 +18484,8 @@ BOOL enum_user_line(LOGBOOK * lbs, int n, char *user, int size)
if (!str[0])
return FALSE;
- 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));
- }
-
- if ((xml_tree = mxml_parse_file(file_name, str, sizeof(str))) == NULL) {
+ xml_tree = load_password_file(lbs->name);
+ if (!xml_tree) {
show_error(str);
return FALSE;
}