Ok, here we go.

This patch enables setting the admin password with the command line
option "--set-admin-password=<password>".

The "-A" and "--set-admin-password" option haven't changed, they still ask the
user for the password.

I've also updated the comments in admin.c, because we no longer set
the default password to "admin".

Please test and report back to me. If no one objects I'll commit
that later.

// Dennis
Index: admin.c
===================================================================
RCS file: /export/home/ntop/ntop/admin.c,v
retrieving revision 2.13
diff -u -r2.13 admin.c
--- admin.c     2002/05/14 12:14:46     2.13
+++ admin.c     2002/05/27 13:48:58
@@ -933,29 +933,29 @@
   if((return_data.dptr == NULL) || (existingOK != 0)) {
     char *thePw, pw1[16], pw2[16];
 
-    /* If not existing, the add user 'admin', pw 'admin' */
+    /* If not existing, then add user 'admin' and ask for password  */
 
     if(userQuestion != NULL) {
       memset(pw1, 0, sizeof(pw1)); memset(pw2, 0, sizeof(pw2));
 
       while(pw1[0] == '\0') {
-       thePw = getpass(userQuestion);
+        thePw = getpass(userQuestion);
 
-       if(thePw[0] != '\0') {
-         if(strlen(thePw) > (sizeof(pw1)-1)) thePw[sizeof(pw1)-1] = '\0';
-         strcpy(pw1, thePw);
-
-         thePw = getpass("Please enter the password again: ");
-
-         if(strlen(thePw) > (sizeof(pw2)-1)) thePw[sizeof(pw2)-1] = '\0';
-         strcpy(pw2, thePw);
-
-         if(strcmp(pw1, pw2)) {
-           printf("Passwords don't match. Please try again.\n");
-           memset(pw1, 0, sizeof(pw1)); memset(pw2, 0, sizeof(pw2));
-           sleep(1); /* It avoids message loops */
-         }
-       }
+        if(thePw[0] != '\0') {
+          if(strlen(thePw) > (sizeof(pw1)-1)) thePw[sizeof(pw1)-1] = '\0';
+          strcpy(pw1, thePw);
+
+          thePw = getpass("Please enter the password again: ");
+
+          if(strlen(thePw) > (sizeof(pw2)-1)) thePw[sizeof(pw2)-1] = '\0';
+            strcpy(pw2, thePw);
+
+          if(strcmp(pw1, pw2)) {
+            printf("Passwords don't match. Please try again.\n");
+            memset(pw1, 0, sizeof(pw1)); memset(pw2, 0, sizeof(pw2));
+            sleep(1); /* It avoids message loops */
+          }
+        }
       }
 
       value = pw1;
@@ -984,6 +984,11 @@
 #ifdef MULTITHREADED
     releaseMutex(&myGlobals.gdbmMutex);
 #endif
+
+    /* print notice to the user */
+    if(key == "1admin")
+      traceEvent(TRACE_INFO, "Admin user password has been set.\n");
+
   } else
     free(return_data.dptr);
 }
@@ -994,10 +999,17 @@
   addKeyIfMissing("1admin", NULL, 1, 1, "\nPlease enter the password for the admin 
user: ");
 }
 
+void setAdminPasswordTo(char* pass) {
+  /* Dennis Schoen ([EMAIL PROTECTED])
+     set the admin password without user intervention via the command line
+  */
+  addKeyIfMissing("1admin", pass, 1, 1, NULL);
+}
+
 /* *******************************/
 
 void addDefaultAdminUser(void) {
-  /* Add user 'admin/admin' if not existing */
+  /* Add user 'admin' and ask for password if not existing */
   addKeyIfMissing("1admin", NULL, 1, 0, "\nPlease enter the password for the admin 
user: ");
 
   /* Add user 'admin' for URL 'show...' if not existing */
Index: globals-core.h
===================================================================
RCS file: /export/home/ntop/ntop/globals-core.h,v
retrieving revision 2.53
diff -u -r2.53 globals-core.h
--- globals-core.h      2002/05/24 06:38:58     2.53
+++ globals-core.h      2002/05/27 13:48:59
@@ -58,6 +58,7 @@
 extern int doChangeFilter(int len);
 extern void changeFilter(void);
 extern void setAdminPassword(void);
+extern void setAdminPasswordTo(char* pass);
 extern void addDefaultAdminUser(void);
 
 /* dataFormat.c */
Index: main.c
===================================================================
RCS file: /export/home/ntop/ntop/main.c,v
retrieving revision 2.54
diff -u -r2.54 main.c
--- main.c      2002/05/27 11:47:02     2.54
+++ main.c      2002/05/27 13:48:59
@@ -111,7 +111,7 @@
 #endif
 
   { "http-server",                      required_argument, NULL, 'w' },
-  { "set-admin-password",               no_argument,       NULL, 'A' },
+  { "set-admin-password",               optional_argument, NULL, 135 },
   { "filter-expression",                required_argument, NULL, 'B' },
   { "domain",                           required_argument, NULL, 'D' },
 
@@ -228,7 +228,8 @@
 #endif
 
   fprintf(fp, "    [-w <port>      | --http-server <port>]               Web server 
(http:) port (or address:port) to listen on\n");
-  fprintf(fp, "    [-A             | --set-admin-password]               Set password 
for the admin user and exit\n");
+  fprintf(fp, "    [-A                                                   Ask admin 
+user password and exit\n");
+  fprintf(fp, "    [                 --set-admin-password <pass>]        Set password 
+for the admin user to <pass>\n");
   fprintf(fp, "    [-B <filter>]   | --filter-expression                 Packet 
filter expression, like tcpdump\n");
   fprintf(fp, "    [-D <name>      | --domain <name>]                    Internet 
domain name\n");
 
@@ -308,7 +309,7 @@
 #endif
 
   fprintf(fp, "    [-w <HTTP port>]\n");
-  fprintf(fp, "    [-A <Set password for the admin user and exit]\n");
+  fprintf(fp, "    [-A <Ask for admin user password and exit]\n");
   fprintf(fp, "    [-B <filter expression (like tcpdump)>]\n");
   fprintf(fp, "    [-D <Internet domain name>]\n");
 
@@ -669,6 +670,19 @@
       }
       break;
 #endif
+
+    case 135:
+      /* Dennis Schoen ([EMAIL PROTECTED]) allow 
+--set-admin-password=<password> */
+      if (optarg) {
+        stringSanityCheck(optarg);
+        initGdbm(NULL);
+        initThreads();
+        setAdminPasswordTo(optarg);
+        exit(0);
+      } else {
+        setAdminPw = 1;
+      }
+      break;
 
     default:
       printf("FATAL ERROR: unknown ntop option, '%s'\n", argv[optind-1]);

Reply via email to