]> code.citadel.org Git - citadel.git/blobdiff - citadel/user_ops.c
* Removed all of the thread cancellation cruft that is no longer necessary
[citadel.git] / citadel / user_ops.c
index 2a7f24d2765f795c85bc104253fabadecd31ff88..5cf386354cdb7c410d70e23e92b7f7c4b1da78b9 100644 (file)
@@ -1,15 +1,7 @@
 /* $Id$ */
 
-#ifndef _SGI_SOURCE
-/* needed to properly enable crypt() stuff on some systems */
-#define _XOPEN_SOURCE
-/* needed for str[n]casecmp() on some systems if the above is defined */
-#define _XOPEN_SOURCE_EXTENDED
-/* needed to enable threads on some systems if the above are defined */
-#define _POSIX_C_SOURCE 199506L
-#endif
-
 #include "sysdep.h"
+#include <errno.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <signal.h>
 #include <pwd.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <sys/time.h>
 #include <string.h>
 #include <syslog.h>
 #include <limits.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
+#ifndef ENABLE_CHKPWD
+#include "auth.h"
 #endif
 #include "citadel.h"
 #include "server.h"
@@ -88,17 +81,18 @@ int lgetuser(struct usersupp *usbuf, char *name)
 /*
  * putuser()  -  write user buffer into the correct place on disk
  */
-void putuser(struct usersupp *usbuf, char *name)
+void putuser(struct usersupp *usbuf)
 {
        char lowercase_name[32];
        int a;
 
-       for (a=0; a<=strlen(name); ++a) {
+       for (a=0; a<=strlen(usbuf->fullname); ++a) {
                if (a < sizeof(lowercase_name))
-                       lowercase_name[a] = tolower(name[a]);
+                       lowercase_name[a] = tolower(usbuf->fullname[a]);
                }
        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));
@@ -109,8 +103,8 @@ void putuser(struct usersupp *usbuf, char *name)
 /*
  * lputuser()  -  same as putuser() but locks the record
  */
-void lputuser(struct usersupp *usbuf, char *name) {
-       putuser(usbuf,name);
+void lputuser(struct usersupp *usbuf) {
+       putuser(usbuf);
        end_critical_section(S_USERSUPP);
        }
 
@@ -317,7 +311,7 @@ void session_startup(void) {
                CC->usersupp.axlevel = 6;
                }
 
-       lputuser(&CC->usersupp,CC->curr_user);
+       lputuser(&CC->usersupp);
 
         /* Run any cleanup routines registered by loadable modules */
        PerformSessionHooks(EVT_LOGIN);
@@ -348,12 +342,69 @@ void logout(struct CitContext *who)
        PerformSessionHooks(EVT_LOGOUT);
        }
 
+#ifdef ENABLE_CHKPWD
+/*
+ * an alternate version of validpw() which executes `chkpwd' instead of
+ * verifying the password directly
+ */
+static int validpw(uid_t uid, const char *pass)
+{
+       pid_t pid;
+       int status, pipev[2];
+       char buf[24];
+
+       if (pipe(pipev)) {
+               lprintf(1, "pipe failed (%s): denying autologin access for "
+                          "uid %u\n", strerror(errno), uid);
+               return 0;
+               }
+
+       switch (pid = fork()) {
+           case -1:
+               lprintf(1, "fork failed (%s): denying autologin access for "
+                          "uid %u\n", strerror(errno), uid);
+               close(pipev[0]);
+               close(pipev[1]);
+               return 0;
+
+           case 0:
+               close(pipev[1]);
+               if (dup2(pipev[0], 0) == -1) {
+                       perror("dup2");
+                       exit(1);
+                       }
+               close(pipev[0]);
+
+               execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
+               perror(BBSDIR "/chkpwd");
+               exit(1);
+               }
+
+       close(pipev[0]);
+       write(pipev[1], buf, sprintf(buf, "%u\n", uid));
+       write(pipev[1], pass, strlen(pass));
+       write(pipev[1], "\n", 1);
+       close(pipev[1]);
+
+       while (waitpid(pid, &status, 0) == -1)
+               if (errno != EINTR) {
+                       lprintf(1, "waitpid failed (%s): denying autologin "
+                                  "access for uid %u\n",
+                               strerror(errno), uid);
+                       return 0;
+                       }
+
+       if (WIFEXITED(status) && !WEXITSTATUS(status))
+               return 1;
+
+       return 0;
+       }
+#endif
 
 void cmd_pass(char *buf)
 {
        char password[256];
        int code;
-       struct passwd *p;
 
        extract(password,buf,0);
 
@@ -371,25 +422,22 @@ void cmd_pass(char *buf)
                }
 
        code = (-1);
-       if (CC->usersupp.USuid == BBSUID) {
+       if (CC->usersupp.uid == BBSUID) {
                strproc(password);
                strproc(CC->usersupp.password);
                code = strcasecmp(CC->usersupp.password,password);
                }
-       else {
-               p = (struct passwd *)getpwuid(CC->usersupp.USuid);
 #ifdef ENABLE_AUTOLOGIN
-               if (p!=NULL) {
-                       if (!strcmp(p->pw_passwd,
-                          (char *)crypt(password,p->pw_passwd))) {
-                               code = 0;
-                               lgetuser(&CC->usersupp, CC->curr_user);
-                               strcpy(CC->usersupp.password, password);
-                               lputuser(&CC->usersupp, CC->curr_user);
-                               }
+       else {
+               if (validpw(CC->usersupp.uid, password)) {
+                       code = 0;
+                       lgetuser(&CC->usersupp, CC->curr_user);
+                       safestrncpy(CC->usersupp.password, password,
+                                   sizeof CC->usersupp.password);
+                       lputuser(&CC->usersupp);
                        }
-#endif
                }
+#endif
 
        if (!code) {
                (CC->logged_in) = 1;
@@ -407,9 +455,7 @@ void cmd_pass(char *buf)
  */
 int purge_user(char pname[]) {
        char filename[64];
-       char mailboxname[ROOMNAMELEN];
        struct usersupp usbuf;
-       struct quickroom qrbuf;
        char lowercase_name[32];
        int a;
        struct CitContext *ccptr;
@@ -439,7 +485,7 @@ int purge_user(char pname[]) {
        if (user_is_logged_in == 1) {
                lprintf(5, "User <%s> is logged in; not deleting.\n", pname);
                usbuf.axlevel = 0;
-               putuser(&usbuf, pname);
+               putuser(&usbuf);
                return(1);
                }
 
@@ -451,12 +497,6 @@ int purge_user(char pname[]) {
        /* delete any existing user/room relationships */
        cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
 
-       /* Delete the user's mailbox and its contents */
-       MailboxName(mailboxname, &usbuf, MAILROOM);
-       if (getroom(&qrbuf, mailboxname)==0) {
-               delete_room(&qrbuf);
-               }
-
        /* delete the userlog entry */
        cdb_delete(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
 
@@ -494,10 +534,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)) {
@@ -519,12 +559,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();
@@ -534,7 +568,7 @@ int create_user(char *newusername)
                }
 
        /* add user to userlog */
-       putuser(&CC->usersupp,CC->curr_user);
+       putuser(&CC->usersupp);
        if (getuser(&CC->usersupp,CC->curr_user)) {
                return(ERROR+INTERNAL_ERROR);
                }
@@ -616,7 +650,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;
                }
@@ -627,7 +661,7 @@ void cmd_setp(char *new_pw)
                }
        lgetuser(&CC->usersupp,CC->curr_user);
        strcpy(CC->usersupp.password,new_pw);
-       lputuser(&CC->usersupp,CC->curr_user);
+       lputuser(&CC->usersupp);
        cprintf("%d Password changed.\n",OK);
        rec_log(CL_PWCHANGE,CC->curr_user);
        PerformSessionHooks(EVT_SETPASS);
@@ -670,7 +704,7 @@ void cmd_setu(char *new_parms)
        CC->usersupp.flags = CC->usersupp.flags & (~US_USER_SET);
        CC->usersupp.flags = CC->usersupp.flags | 
                (extract_int(new_parms,2) & US_USER_SET);
-       lputuser(&CC->usersupp,CC->curr_user);
+       lputuser(&CC->usersupp);
        cprintf("%d Ok\n",OK);
        }
 
@@ -700,7 +734,7 @@ void cmd_slrp(char *new_ptr)
        vbuf.v_lastseen = newlr;
        CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
 
-       lputuser(&CC->usersupp, CC->curr_user);
+       lputuser(&CC->usersupp);
        cprintf("%d %ld\n",OK,newlr);
        }
 
@@ -745,10 +779,10 @@ void cmd_invt_kick(char *iuser, int op)
 
        CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
 
-       lputuser(&USscratch,iuser);
+       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,
@@ -786,7 +820,7 @@ void cmd_forg(void) {
        vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
 
        CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
-       lputuser(&CC->usersupp,CC->curr_user);
+       lputuser(&CC->usersupp);
        cprintf("%d Ok\n",OK);
        usergoto(BASEROOM, 0);
        }
@@ -846,67 +880,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
  */
@@ -938,7 +911,7 @@ void cmd_vali(char *v_args)
        userbuf.axlevel = newax;
        userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
 
-       lputuser(&userbuf,user);
+       lputuser(&userbuf);
 
        /* If the access level was set to zero, delete the user */
        if (newax == 0) {
@@ -1005,89 +978,6 @@ void cmd_list(void) {
        }
 
 
-/*
- * 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,CC->curr_user);
-
-       /* 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);
-       }
 
 
 /*
@@ -1286,7 +1176,7 @@ void cmd_asup(char *cmdbuf) {
                usbuf.USuserpurge = extract_int(cmdbuf, 8);
                }
 
-       lputuser(&usbuf, requested_user);
+       lputuser(&usbuf);
        if (usbuf.axlevel == 0) {
                if (purge_user(requested_user)==0) {
                        cprintf("%d %s deleted.\n", OK, requested_user);
@@ -1302,22 +1192,35 @@ void cmd_asup(char *cmdbuf) {
 int NewMailCount() {
        int num_newmsgs = 0;
        int a;
-       char mailboxname[32];
+       char mailboxname[ROOMNAMELEN];
        struct quickroom mailbox;
        struct visit vbuf;
+        struct cdbdata *cdbfr;
+       long *msglist = NULL;
+       int num_msgs = 0;
 
        MailboxName(mailboxname, &CC->usersupp, MAILROOM);
        if (getroom(&mailbox, mailboxname)!=0) return(0);
        CtdlGetRelationship(&vbuf, &CC->usersupp, &mailbox);
 
-       get_msglist(&mailbox);
-       for (a=0; a<CC->num_msgs; ++a) {
-               if (MessageFromList(a)>0L) {
-                       if (MessageFromList(a) > vbuf.v_lastseen) {
+        cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
+
+        if (cdbfr != NULL) {
+               msglist = mallok(cdbfr->len);
+               memcpy(msglist, cdbfr->ptr, cdbfr->len);
+               num_msgs = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
+       }
+
+       if (num_msgs > 0) for (a=0; a<num_msgs; ++a) {
+               if (msglist[a]>0L) {
+                       if (msglist[a] > vbuf.v_lastseen) {
                                ++num_newmsgs;
                                }
                        }
                }
 
+       if (msglist != NULL) phree(msglist);
+
        return(num_newmsgs);
        }