* First cut at Solaris fixes. There may still be some *printf("%s", NULL)
[citadel.git] / citadel / user_ops.c
index 9904a5823d949b696b04c196c63157ad04cc97b8..6e69ef068a494213248394790714b97e9c9262b4 100644 (file)
@@ -14,9 +14,6 @@
 #include <string.h>
 #include <syslog.h>
 #include <limits.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
-#endif
 #ifndef ENABLE_CHKPWD
 #include "auth.h"
 #endif
@@ -95,6 +92,7 @@ void putuser(struct usersupp *usbuf)
                }
        lowercase_name[sizeof(lowercase_name)-1] = 0;
 
+       usbuf->version = config.c_setup_level;
        cdb_store(CDB_USERSUPP,
                lowercase_name, strlen(lowercase_name),
                usbuf, sizeof(struct usersupp));
@@ -244,24 +242,21 @@ int getuserbynumber(struct usersupp *usbuf, long int number)
 
 
 /*
- * USER cmd
+ * Back end for cmd_user() and its ilk
  */
-void cmd_user(char *cmdbuf)
+int CtdlLoginExistingUser(char *username)
 {
-       char username[256];
        char autoname[256];
        int found_user = 0;
        struct passwd *p;
        int a;
 
-       extract(username,cmdbuf,0);
        username[25] = 0;
        strproc(username);
 
        if ((CC->logged_in)) {
-               cprintf("%d Already logged in.\n",ERROR);
-               return;
-               }
+               return login_already_logged_in;
+       }
 
        found_user = getuser(&CC->usersupp,username);
        if (found_user != 0) {
@@ -271,24 +266,56 @@ void cmd_user(char *cmdbuf)
                        for (a=0; a<strlen(autoname); ++a)
                                if (autoname[a]==',') autoname[a]=0;
                        found_user = getuser(&CC->usersupp,autoname);
-                       }
                }
+       }
        if (found_user == 0) {
                if (((CC->nologin)) && (CC->usersupp.axlevel < 6)) {
-                       cprintf("%d %s: Too many users are already online (maximum is %d)\n",
-                       ERROR+MAX_SESSIONS_EXCEEDED,
-                       config.c_nodename,config.c_maxsessions);
-                       }
+                       return login_too_many_users;
+               }
                else {
                        strcpy(CC->curr_user,CC->usersupp.fullname);
+                       return login_ok;
+               }
+       }
+       return login_not_found;
+}
+
+
+
+/*
+ * USER cmd
+ */
+void cmd_user(char *cmdbuf)
+{
+       char username[256];
+       int a;
+
+       extract(username,cmdbuf,0);
+       username[25] = 0;
+       strproc(username);
+
+       a = CtdlLoginExistingUser(username);
+       switch(a) {
+               case login_already_logged_in:
+                       cprintf("%d Already logged in.\n",ERROR);
+                       return;
+               case login_too_many_users:
+                       cprintf("%d %s: "
+                               "Too many users are already online "
+                               "(maximum is %d)\n",
+                               ERROR+MAX_SESSIONS_EXCEEDED,
+                               config.c_nodename,config.c_maxsessions);
+                       return;
+               case login_ok:
                        cprintf("%d Password required for %s\n",
                                MORE_DATA,CC->curr_user);
-                       }
-               }
-       else {
-               cprintf("%d %s not found.\n",ERROR,username);
-               }
+                       return;
+               case login_not_found:
+                       cprintf("%d %s not found.\n", ERROR, username);
+                       return;
+               cprintf("%d Internal error\n", ERROR);
        }
+}
 
 
 
@@ -318,14 +345,21 @@ void session_startup(void) {
         /* Run any cleanup routines registered by loadable modules */
        PerformSessionHooks(EVT_LOGIN);
 
-       cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
-               CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
-               CC->usersupp.usernum);
        usergoto(BASEROOM,0);           /* Enter the lobby */   
        rec_log(CL_LOGIN,CC->curr_user);
        }
 
 
+void logged_in_response(void) {
+       cprintf("%d %s|%d|%d|%d|%u|%ld\n",
+               OK, CC->usersupp.fullname, CC->usersupp.axlevel,
+               CC->usersupp.timescalled, CC->usersupp.posted,
+               CC->usersupp.flags,
+               CC->usersupp.usernum);
+}
+
+
+
 /* 
  * misc things to be taken care of when a user is logged out
  */
@@ -383,7 +417,7 @@ static int validpw(uid_t uid, const char *pass)
                }
 
        close(pipev[0]);
-       write(pipev[1], buf, sprintf(buf, "%u\n", uid));
+       write(pipev[1], buf, sprintf(buf, "%lu\n", (unsigned long)uid));
        write(pipev[1], pass, strlen(pass));
        write(pipev[1], "\n", 1);
        close(pipev[1]);
@@ -403,35 +437,31 @@ static int validpw(uid_t uid, const char *pass)
        }
 #endif
 
-void cmd_pass(char *buf)
+
+
+int CtdlTryPassword(char *password)
 {
-       char password[256];
        int code;
 
-       extract(password,buf,0);
-
        if ((CC->logged_in)) {
-               cprintf("%d Already logged in.\n",ERROR);
-               return;
+               return pass_already_logged_in;
                }
-       if (!strcmp(CC->curr_user,"")) {
-               cprintf("%d You must send a name with USER first.\n",ERROR);
-               return;
+       if (!strcmp(CC->curr_user, NLI)) {
+               return pass_no_user;
                }
-       if (getuser(&CC->usersupp,CC->curr_user)) {
-               cprintf("%d Can't find user record!\n",ERROR+INTERNAL_ERROR);
-               return;
+       if (getuser(&CC->usersupp, CC->curr_user)) {
+               return pass_internal_error;
                }
 
        code = (-1);
-       if (CC->usersupp.USuid == BBSUID) {
+       if (CC->usersupp.uid == BBSUID) {
                strproc(password);
                strproc(CC->usersupp.password);
                code = strcasecmp(CC->usersupp.password,password);
                }
 #ifdef ENABLE_AUTOLOGIN
        else {
-               if (validpw(CC->usersupp.USuid, password)) {
+               if (validpw(CC->usersupp.uid, password)) {
                        code = 0;
                        lgetuser(&CC->usersupp, CC->curr_user);
                        safestrncpy(CC->usersupp.password, password,
@@ -444,14 +474,44 @@ void cmd_pass(char *buf)
        if (!code) {
                (CC->logged_in) = 1;
                session_startup();
+               return pass_ok;
                }
        else {
-               cprintf("%d Wrong password.\n",ERROR);
                rec_log(CL_BADPW,CC->curr_user);
+               return pass_wrong_password;
                }
        }
 
 
+void cmd_pass(char *buf)
+{
+       char password[256];
+       int a;
+
+       extract(password, buf, 0);
+       a = CtdlTryPassword(password);
+
+       switch (a) {
+               case pass_already_logged_in:
+                       cprintf("%d Already logged in.\n",ERROR);
+                       return;
+               case pass_no_user:
+                       cprintf("%d You must send a name with USER first.\n",
+                               ERROR);
+                       return;
+               case pass_wrong_password:
+                       cprintf("%d Wrong password.\n", ERROR);
+                       return;
+               case pass_ok:
+                       logged_in_response();
+                       return;
+               cprintf("%d Can't find user record!\n",
+                       ERROR+INTERNAL_ERROR);
+       }
+}
+
+
+
 /*
  * Delete a user record *and* all of its related resources.
  */
@@ -536,10 +596,10 @@ int create_user(char *newusername)
                for (a=0; a<strlen(username); ++a) {
                        if (username[a] == ',') username[a] = 0;
                        }
-               CC->usersupp.USuid = p->pw_uid;
+               CC->usersupp.uid = p->pw_uid;
                }
        else {
-               CC->usersupp.USuid = BBSUID;
+               CC->usersupp.uid = BBSUID;
                }
 
        if (!getuser(&usbuf,username)) {
@@ -561,12 +621,6 @@ int create_user(char *newusername)
        CC->usersupp.USscreenwidth = 80;
        CC->usersupp.USscreenheight = 24;
        time(&CC->usersupp.lastcall);
-       strcpy(CC->usersupp.USname, "");
-       strcpy(CC->usersupp.USaddr, "");
-       strcpy(CC->usersupp.UScity, "");
-       strcpy(CC->usersupp.USstate, "");
-       strcpy(CC->usersupp.USzip, "");
-       strcpy(CC->usersupp.USphone, "");
 
        /* fetch a new user number */
        CC->usersupp.usernum = get_new_user_number();
@@ -640,6 +694,7 @@ void cmd_newu(char *cmdbuf)
                }
        else if (a==0) {
                session_startup();
+               logged_in_response();
                }
        else {
                cprintf("%d unknown error\n",ERROR);
@@ -658,7 +713,7 @@ void cmd_setp(char *new_pw)
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
                return;
                }
-       if (CC->usersupp.USuid != BBSUID) {
+       if (CC->usersupp.uid != BBSUID) {
                cprintf("%d Not allowed.  Use the 'passwd' command.\n",ERROR);
                return;
                }
@@ -790,7 +845,7 @@ void cmd_invt_kick(char *iuser, int op)
        lputuser(&USscratch);
 
        /* post a message in Aide> saying what we just did */
-       sprintf(bbb,"%s %s %s> by %s",
+       sprintf(bbb,"%s %s %s> by %s\n",
                iuser,
                ((op == 1) ? "invited to" : "kicked out of"),
                CC->quickroom.QRname,
@@ -888,67 +943,6 @@ void cmd_gnur(void) {
        }
 
 
-/*
- * get registration info for a user
- */
-void cmd_greg(char *who)
-{
-       struct usersupp usbuf;
-       int a,b;
-       char pbuf[32];
-
-       if (!(CC->logged_in)) {
-               cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
-               return;
-               }
-
-       if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
-
-       if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
-               cprintf("%d Higher access required.\n",
-                       ERROR+HIGHER_ACCESS_REQUIRED);
-               return;
-               }
-
-       if (getuser(&usbuf,who) != 0) {
-               cprintf("%d '%s' not found.\n",ERROR+NO_SUCH_USER,who);
-               return;
-               }
-
-       cprintf("%d %s\n",LISTING_FOLLOWS,usbuf.fullname);
-       cprintf("%ld\n",usbuf.usernum);
-       cprintf("%s\n",usbuf.password);
-       cprintf("%s\n",usbuf.USname);
-       cprintf("%s\n",usbuf.USaddr);
-       cprintf("%s\n%s\n%s\n",
-               usbuf.UScity,usbuf.USstate,usbuf.USzip);
-       strcpy(pbuf,usbuf.USphone);
-       usbuf.USphone[0]=0;
-       for (a=0; a<strlen(pbuf); ++a) {
-               if ((pbuf[a]>='0')&&(pbuf[a]<='9')) {
-                       b=strlen(usbuf.USphone);
-                       usbuf.USphone[b]=pbuf[a];
-                       usbuf.USphone[b+1]=0;
-                       }
-               }
-       while(strlen(usbuf.USphone)<10) {
-               strcpy(pbuf,usbuf.USphone);
-               strcpy(usbuf.USphone," ");
-               strcat(usbuf.USphone,pbuf);
-               }
-
-       cprintf("(%c%c%c) %c%c%c-%c%c%c%c\n",
-               usbuf.USphone[0],usbuf.USphone[1],
-               usbuf.USphone[2],usbuf.USphone[3],
-               usbuf.USphone[4],usbuf.USphone[5],
-               usbuf.USphone[6],usbuf.USphone[7],
-               usbuf.USphone[8],usbuf.USphone[9]);
-
-       cprintf("%d\n",usbuf.axlevel);
-       cprintf("%s\n",usbuf.USemail);
-       cprintf("000\n");
-       }
-
 /*
  * validate a user
  */
@@ -998,7 +992,8 @@ void cmd_vali(char *v_args)
 /* 
  *  Traverse the user file...
  */
-void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
+void ForEachUser(void (*CallBack)(struct usersupp *EachUser, void *out_data),
+               void *in_data) {
        struct usersupp usbuf;
        struct cdbdata *cdbus;
 
@@ -1010,7 +1005,7 @@ void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
                        ( (cdbus->len > sizeof(struct usersupp)) ?
                        sizeof(struct usersupp) : cdbus->len) );
                cdb_free(cdbus);
-               (*CallBack)(&usbuf);
+               (*CallBack)(&usbuf, in_data);
                }
        }
 
@@ -1018,7 +1013,7 @@ void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
 /*
  * List one user (this works with cmd_list)
  */
-void ListThisUser(struct usersupp *usbuf) {
+void ListThisUser(struct usersupp *usbuf, void *data) {
        if (usbuf->axlevel > 0) {
                if ((CC->usersupp.axlevel>=6)
                   ||((usbuf->flags&US_UNLISTED)==0)
@@ -1042,94 +1037,11 @@ void ListThisUser(struct usersupp *usbuf) {
  */
 void cmd_list(void) {
        cprintf("%d \n",LISTING_FOLLOWS);
-       ForEachUser(ListThisUser);
+       ForEachUser(ListThisUser, NULL);
        cprintf("000\n");
        }
 
 
-/*
- * enter registration info
- */
-void cmd_regi(void) {
-       int a,b,c;
-       char buf[256];
-
-       char tmpname[256];
-       char tmpaddr[256];
-       char tmpcity[256];
-       char tmpstate[256];
-       char tmpzip[256];
-       char tmpphone[256];
-       char tmpemail[256];
-
-       if (!(CC->logged_in)) {
-               cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
-               return;
-               }
-
-       strcpy(tmpname,"");
-       strcpy(tmpaddr,"");
-       strcpy(tmpcity,"");
-       strcpy(tmpstate,"");
-       strcpy(tmpzip,"");
-       strcpy(tmpphone,"");
-       strcpy(tmpemail,"");
-
-       cprintf("%d Send registration...\n",SEND_LISTING);
-       a=0;
-       while (client_gets(buf), strcmp(buf,"000")) {
-               if (a==0) strcpy(tmpname,buf);
-               if (a==1) strcpy(tmpaddr,buf);
-               if (a==2) strcpy(tmpcity,buf);
-               if (a==3) strcpy(tmpstate,buf);
-               if (a==4) {
-                       for (c=0; c<strlen(buf); ++c) {
-                               if ((buf[c]>='0')&&(buf[c]<='9')) {
-                                       b=strlen(tmpzip);
-                                       tmpzip[b]=buf[c];
-                                       tmpzip[b+1]=0;
-                                       }
-                               }
-                       }
-               if (a==5) {
-                       for (c=0; c<strlen(buf); ++c) {
-                               if ((buf[c]>='0')&&(buf[c]<='9')) {
-                                       b=strlen(tmpphone);
-                                       tmpphone[b]=buf[c];
-                                       tmpphone[b+1]=0;
-                                       }
-                               }
-                       }
-               if (a==6) strncpy(tmpemail,buf,31);
-               ++a;
-               }
-
-       tmpname[29]=0;
-       tmpaddr[24]=0;
-       tmpcity[14]=0;
-       tmpstate[2]=0;
-       tmpzip[9]=0;
-       tmpphone[10]=0;
-       tmpemail[31]=0;
-
-       lgetuser(&CC->usersupp,CC->curr_user);
-       strcpy(CC->usersupp.USname,tmpname);
-       strcpy(CC->usersupp.USaddr,tmpaddr);
-       strcpy(CC->usersupp.UScity,tmpcity);
-       strcpy(CC->usersupp.USstate,tmpstate);
-       strcpy(CC->usersupp.USzip,tmpzip);
-       strcpy(CC->usersupp.USphone,tmpphone);
-       strcpy(CC->usersupp.USemail,tmpemail);
-       CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
-       lputuser(&CC->usersupp);
-
-       /* set global flag calling for validation */
-       begin_critical_section(S_CONTROL);
-       get_control();
-       CitControl.MMflags = CitControl.MMflags | MM_VALID ;
-       put_control();
-       end_critical_section(S_CONTROL);
-       }
 
 
 /*
@@ -1177,82 +1089,6 @@ void cmd_qusr(char *who)
        }
 
 
-/*
- * enter user bio
- */
-void cmd_ebio(void) {
-       char buf[256];
-       FILE *fp;
-
-       if (!(CC->logged_in)) {
-               cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
-               return;
-               }
-
-       sprintf(buf,"./bio/%ld",CC->usersupp.usernum);
-       fp = fopen(buf,"w");
-       if (fp == NULL) {
-               cprintf("%d Cannot create file\n",ERROR);
-               return;
-               }
-       cprintf("%d  \n",SEND_LISTING);
-       while(client_gets(buf), strcmp(buf,"000")) {
-               fprintf(fp,"%s\n",buf);
-               }
-       fclose(fp);
-       }
-
-/*
- * read user bio
- */
-void cmd_rbio(char *cmdbuf)
-{
-       struct usersupp ruser;
-       char buf[256];
-       FILE *fp;
-
-       extract(buf,cmdbuf,0);
-       if (getuser(&ruser,buf)!=0) {
-               cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
-               return;
-               }
-       sprintf(buf,"./bio/%ld",ruser.usernum);
-       
-       fp = fopen(buf,"r");
-       if (fp == NULL) {
-               cprintf("%d %s has no bio on file.\n",
-                       ERROR+FILE_NOT_FOUND,ruser.fullname);
-               return;
-               }
-       cprintf("%d  \n",LISTING_FOLLOWS);
-       while (fgets(buf,256,fp)!=NULL) cprintf("%s",buf);
-       fclose(fp);
-       cprintf("000\n");
-       }
-
-/*
- * list of users who have entered bios
- */
-void cmd_lbio(void) {
-       char buf[256];
-       FILE *ls;
-       struct usersupp usbuf;
-
-       ls=popen("cd ./bio; ls","r");
-       if (ls==NULL) {
-               cprintf("%d Cannot open listing.\n",ERROR+FILE_NOT_FOUND);
-               return;
-               }
-
-       cprintf("%d\n",LISTING_FOLLOWS);
-       while (fgets(buf,255,ls)!=NULL)
-               if (getuserbynumber(&usbuf,atol(buf))==0)
-                       cprintf("%s\n",usbuf.fullname);
-       pclose(ls);
-       cprintf("000\n");
-       }
-
-
 /*
  * Administrative Get User Parameters
  */
@@ -1296,6 +1132,7 @@ void cmd_asup(char *cmdbuf) {
        char requested_user[256];
        int np;
        int newax;
+       int deleted = 0;
        
        if ( (CC->internal_pgm==0)
           && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
@@ -1331,10 +1168,12 @@ void cmd_asup(char *cmdbuf) {
        lputuser(&usbuf);
        if (usbuf.axlevel == 0) {
                if (purge_user(requested_user)==0) {
-                       cprintf("%d %s deleted.\n", OK, requested_user);
+                       deleted = 1;
                        }
                }
-       cprintf("%d Ok\n", OK);
+       cprintf("%d Ok", OK);
+       if (deleted) cprintf(" (%s deleted)", requested_user);
+       cprintf("\n");
        }
 
 
@@ -1372,5 +1211,7 @@ int NewMailCount() {
                        }
                }
 
+       if (msglist != NULL) phree(msglist);
+
        return(num_newmsgs);
        }