]> code.citadel.org Git - citadel.git/blob - citadel/user_ops.c
* REMOVED MIXED MODE AUTHENTICATION.
[citadel.git] / citadel / user_ops.c
1 /* 
2  * $Id$
3  *
4  * Server functions which perform operations on user objects.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <ctype.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <string.h>
32 #include <limits.h>
33 #ifndef ENABLE_CHKPWD
34 #include "auth.h"
35 #endif
36 #include "citadel.h"
37 #include "server.h"
38 #include "database.h"
39 #include "user_ops.h"
40 #include "serv_extensions.h"
41 #include "sysdep_decls.h"
42 #include "support.h"
43 #include "room_ops.h"
44 #include "file_ops.h"
45 #include "control.h"
46 #include "msgbase.h"
47 #include "config.h"
48 #include "tools.h"
49 #include "citserver.h"
50
51
52 /*
53  * makeuserkey() - convert a username into the format used as a database key
54  *                 (it's just the username converted into lower case)
55  */
56 static INLINE void makeuserkey(char *key, char *username) {
57         int i, len;
58
59         len = strlen(username);
60         for (i=0; i<=len; ++i) {
61                 key[i] = tolower(username[i]);
62         }
63 }
64
65
66 /*
67  * getuser()  -  retrieve named user into supplied buffer.
68  *               returns 0 on success
69  */
70 int getuser(struct ctdluser *usbuf, char name[])
71 {
72
73         char usernamekey[USERNAME_SIZE];
74         struct cdbdata *cdbus;
75
76         if (usbuf != NULL) {
77                 memset(usbuf, 0, sizeof(struct ctdluser));
78         }
79
80         makeuserkey(usernamekey, name);
81         cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
82
83         if (cdbus == NULL) {    /* user not found */
84                 return(1);
85         }
86         if (usbuf != NULL) {
87                 memcpy(usbuf, cdbus->ptr,
88                         ((cdbus->len > sizeof(struct ctdluser)) ?
89                          sizeof(struct ctdluser) : cdbus->len));
90         }
91         cdb_free(cdbus);
92
93         return (0);
94 }
95
96
97 /*
98  * lgetuser()  -  same as getuser() but locks the record
99  */
100 int lgetuser(struct ctdluser *usbuf, char *name)
101 {
102         int retcode;
103
104         retcode = getuser(usbuf, name);
105         if (retcode == 0) {
106                 begin_critical_section(S_USERS);
107         }
108         return (retcode);
109 }
110
111
112 /*
113  * putuser()  -  write user buffer into the correct place on disk
114  */
115 void putuser(struct ctdluser *usbuf)
116 {
117         char usernamekey[USERNAME_SIZE];
118
119         makeuserkey(usernamekey, usbuf->fullname);
120
121         usbuf->version = REV_LEVEL;
122         cdb_store(CDB_USERS,
123                   usernamekey, strlen(usernamekey),
124                   usbuf, sizeof(struct ctdluser));
125
126 }
127
128
129 /*
130  * lputuser()  -  same as putuser() but locks the record
131  */
132 void lputuser(struct ctdluser *usbuf)
133 {
134         putuser(usbuf);
135         end_critical_section(S_USERS);
136 }
137
138 /*
139  * Index-generating function used by Ctdl[Get|Set]Relationship
140  */
141 int GenerateRelationshipIndex(char *IndexBuf,
142                               long RoomID,
143                               long RoomGen,
144                               long UserID)
145 {
146
147         struct {
148                 long iRoomID;
149                 long iRoomGen;
150                 long iUserID;
151         } TheIndex;
152
153         TheIndex.iRoomID = RoomID;
154         TheIndex.iRoomGen = RoomGen;
155         TheIndex.iUserID = UserID;
156
157         memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
158         return (sizeof(TheIndex));
159 }
160
161
162
163 /*
164  * Back end for CtdlSetRelationship()
165  */
166 void put_visit(struct visit *newvisit)
167 {
168         char IndexBuf[32];
169         int IndexLen;
170
171         /* Generate an index */
172         IndexLen = GenerateRelationshipIndex(IndexBuf,
173                                              newvisit->v_roomnum,
174                                              newvisit->v_roomgen,
175                                              newvisit->v_usernum);
176
177         /* Store the record */
178         cdb_store(CDB_VISIT, IndexBuf, IndexLen,
179                   newvisit, sizeof(struct visit)
180         );
181 }
182
183
184
185
186 /*
187  * Define a relationship between a user and a room
188  */
189 void CtdlSetRelationship(struct visit *newvisit,
190                          struct ctdluser *rel_user,
191                          struct ctdlroom *rel_room)
192 {
193
194
195         /* We don't use these in Citadel because they're implicit by the
196          * index, but they must be present if the database is exported.
197          */
198         newvisit->v_roomnum = rel_room->QRnumber;
199         newvisit->v_roomgen = rel_room->QRgen;
200         newvisit->v_usernum = rel_user->usernum;
201
202         put_visit(newvisit);
203 }
204
205 /*
206  * Locate a relationship between a user and a room
207  */
208 void CtdlGetRelationship(struct visit *vbuf,
209                          struct ctdluser *rel_user,
210                          struct ctdlroom *rel_room)
211 {
212
213         char IndexBuf[32];
214         int IndexLen;
215         struct cdbdata *cdbvisit;
216
217         /* Generate an index */
218         IndexLen = GenerateRelationshipIndex(IndexBuf,
219                                              rel_room->QRnumber,
220                                              rel_room->QRgen,
221                                              rel_user->usernum);
222
223         /* Clear out the buffer */
224         memset(vbuf, 0, sizeof(struct visit));
225
226         cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
227         if (cdbvisit != NULL) {
228                 memcpy(vbuf, cdbvisit->ptr,
229                        ((cdbvisit->len > sizeof(struct visit)) ?
230                         sizeof(struct visit) : cdbvisit->len));
231                 cdb_free(cdbvisit);
232         }
233         else {
234                 /* If this is the first time the user has seen this room,
235                  * set the view to be the default for the room.
236                  */
237                 vbuf->v_view = rel_room->QRdefaultview;
238         }
239
240         /* Set v_seen if necessary */
241         if (vbuf->v_seen[0] == 0) {
242                 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
243         }
244 }
245
246
247 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
248 {
249         snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
250 }
251
252
253 /*
254  * Is the user currently logged in an Aide?
255  */
256 int is_aide(void)
257 {
258         if (CC->user.axlevel >= 6)
259                 return (1);
260         else
261                 return (0);
262 }
263
264
265 /*
266  * Is the user currently logged in an Aide *or* the room aide for this room?
267  */
268 int is_room_aide(void)
269 {
270
271         if (!CC->logged_in) {
272                 return (0);
273         }
274
275         if ((CC->user.axlevel >= 6)
276             || (CC->room.QRroomaide == CC->user.usernum)) {
277                 return (1);
278         } else {
279                 return (0);
280         }
281 }
282
283 /*
284  * getuserbynumber()  -  get user by number
285  *                       returns 0 if user was found
286  *
287  * WARNING: don't use this function unless you absolutely have to.  It does
288  *          a sequential search and therefore is computationally expensive.
289  */
290 int getuserbynumber(struct ctdluser *usbuf, long int number)
291 {
292         struct cdbdata *cdbus;
293
294         cdb_rewind(CDB_USERS);
295
296         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
297                 memset(usbuf, 0, sizeof(struct ctdluser));
298                 memcpy(usbuf, cdbus->ptr,
299                        ((cdbus->len > sizeof(struct ctdluser)) ?
300                         sizeof(struct ctdluser) : cdbus->len));
301                 cdb_free(cdbus);
302                 if (usbuf->usernum == number) {
303                         cdb_close_cursor(CDB_USERS);
304                         return (0);
305                 }
306         }
307         return (-1);
308 }
309
310
311 #ifdef ENABLE_AUTOLOGIN
312 /*
313  * getuserbyuid()  -     get user by system uid (for PAM mode authentication)
314  *                       returns 0 if user was found
315  *
316  * WARNING: don't use this function unless you absolutely have to.  It does
317  *          a sequential search and therefore is computationally expensive.
318  */
319 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
320 {
321         struct cdbdata *cdbus;
322
323         cdb_rewind(CDB_USERS);
324
325         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
326                 memset(usbuf, 0, sizeof(struct ctdluser));
327                 memcpy(usbuf, cdbus->ptr,
328                        ((cdbus->len > sizeof(struct ctdluser)) ?
329                         sizeof(struct ctdluser) : cdbus->len));
330                 cdb_free(cdbus);
331                 if (usbuf->uid == number) {
332                         cdb_close_cursor(CDB_USERS);
333                         return (0);
334                 }
335         }
336         return (-1);
337 }
338 #endif /* ENABLE_AUTOLOGIN */
339
340
341
342 /*
343  * Back end for cmd_user() and its ilk
344  */
345 int CtdlLoginExistingUser(char *trythisname)
346 {
347         char username[SIZ];
348         int found_user;
349
350         if ((CC->logged_in)) {
351                 return login_already_logged_in;
352         }
353
354         if (trythisname == NULL) return login_not_found;
355         safestrncpy(username, trythisname, USERNAME_SIZE);
356         striplt(username);
357
358 #ifdef ENABLE_AUTOLOGIN
359
360         /* If this is an autologin build, the only valid auth source is the
361          * host operating system.
362          */
363         struct passwd pd;
364         struct passwd *tempPwdPtr;
365         char pwdbuffer[256];
366
367         lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
368         getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
369         if (tempPwdPtr == NULL) {
370                 return login_not_found;
371         }
372         lprintf(CTDL_DEBUG, "found it! uid=%d\n", pd.pw_uid);
373
374         /* Locate the associated Citadel account.
375          * If not found, make one attempt to create it.
376          */
377         found_user = getuserbyuid(&CC->user, pd.pw_uid);
378         if (found_user != 0) {
379                 create_user(username, 0);
380                 found_user = getuserbyuid(&CC->user, pd.pw_uid);
381         }
382
383 #else /* ENABLE_AUTOLOGIN */
384         struct recptypes *valid = NULL;
385
386         /* First, try to log in as if the supplied name is a display name */
387         found_user = getuser(&CC->user, username);
388
389         /* If that didn't work, try to log in as if the supplied name
390          * is an e-mail address
391          */
392         if (found_user != 0) {
393                 valid = validate_recipients(username);
394                 if (valid != NULL) {
395                         if (valid->num_local == 1) {
396                                 found_user = getuser(&CC->user,
397                                                 valid->recp_local);
398                         }
399                         free(valid);
400                 }
401         }
402
403 #endif /* ENABLE_AUTOLOGIN */
404
405         /* Did we find something? */
406         if (found_user == 0) {
407                 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
408                         return login_too_many_users;
409                 } else {
410                         safestrncpy(CC->curr_user, CC->user.fullname,
411                                         sizeof CC->curr_user);
412                         return login_ok;
413                 }
414         }
415         return login_not_found;
416 }
417
418
419
420 /*
421  * USER cmd
422  */
423 void cmd_user(char *cmdbuf)
424 {
425         char username[256];
426         int a;
427
428         extract_token(username, cmdbuf, 0, '|', sizeof username);
429         striplt(username);
430
431         a = CtdlLoginExistingUser(username);
432         switch (a) {
433         case login_already_logged_in:
434                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
435                 return;
436         case login_too_many_users:
437                 cprintf("%d %s: "
438                         "Too many users are already online "
439                         "(maximum is %d)\n",
440                         ERROR + MAX_SESSIONS_EXCEEDED,
441                         config.c_nodename, config.c_maxsessions);
442                 return;
443         case login_ok:
444                 cprintf("%d Password required for %s\n",
445                         MORE_DATA, CC->curr_user);
446                 return;
447         case login_not_found:
448                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
449                 return;
450         default:
451                 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
452         }
453 }
454
455
456
457 /*
458  * session startup code which is common to both cmd_pass() and cmd_newu()
459  */
460 void session_startup(void)
461 {
462         int i;
463
464         lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
465
466         lgetuser(&CC->user, CC->curr_user);
467         ++(CC->user.timescalled);
468         CC->previous_login = CC->user.lastcall;
469         time(&CC->user.lastcall);
470
471         /* If this user's name is the name of the system administrator
472          * (as specified in setup), automatically assign access level 6.
473          */
474         if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
475                 CC->user.axlevel = 6;
476         }
477         lputuser(&CC->user);
478
479         /*
480          * Populate CC->cs_inet_email with a default address.  This will be
481          * overwritten with the user's directory address, if one exists, when
482          * the vCard module's login hook runs.
483          */
484         snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
485                 CC->user.fullname, config.c_fqdn);
486         for (i=0; i<strlen(CC->cs_inet_email); ++i) {
487                 if (isspace(CC->cs_inet_email[i])) {
488                         CC->cs_inet_email[i] = '_';
489                 }
490         }
491
492         /* Create any personal rooms required by the system.
493          * (Technically, MAILROOM should be there already, but just in case...)
494          */
495         create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
496         create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
497         create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
498
499         /* Run any startup routines registered by loadable modules */
500         PerformSessionHooks(EVT_LOGIN);
501
502         /* Enter the lobby */
503         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
504 }
505
506
507 void logged_in_response(void)
508 {
509         cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
510                 CIT_OK, CC->user.fullname, CC->user.axlevel,
511                 CC->user.timescalled, CC->user.posted,
512                 CC->user.flags, CC->user.usernum,
513                 CC->previous_login);
514 }
515
516
517
518 /* 
519  * misc things to be taken care of when a user is logged out
520  */
521 void logout(struct CitContext *who)
522 {
523         /*
524          * Clear out some session data.  Most likely, the CitContext for this
525          * session is about to get nuked when the session disconnects, but
526          * since it's possible to log in again without reconnecting, we cannot
527          * make that assumption.
528          */
529         strcpy(who->fake_username, "");
530         strcpy(who->fake_postname, "");
531         strcpy(who->fake_hostname, "");
532         strcpy(who->fake_roomname, "");
533         who->logged_in = 0;
534
535         /*
536          * If there is a download in progress, abort it.
537          */
538         if (who->download_fp != NULL) {
539                 fclose(who->download_fp);
540                 who->download_fp = NULL;
541         }
542
543         /*
544          * If there is an upload in progress, abort it.
545          */
546         if (who->upload_fp != NULL) {
547                 abort_upl(who);
548         }
549
550         /*
551          * If we were talking to a network node, we're not anymore...
552          */
553         if (strlen(who->net_node) > 0) {
554                 network_talking_to(who->net_node, NTT_REMOVE);
555         }
556
557         /* Do modular stuff... */
558         PerformSessionHooks(EVT_LOGOUT);
559
560         /* Free any output buffers */
561         if (who->output_buffer != NULL) {
562                 unbuffer_output();
563         }
564 }
565
566 #ifdef ENABLE_CHKPWD
567 /*
568  * an alternate version of validpw() which executes `chkpwd' instead of
569  * verifying the password directly
570  */
571 static int validpw(uid_t uid, const char *pass)
572 {
573         pid_t pid;
574         int status, pipev[2];
575         char buf[24];
576
577         if (pipe(pipev)) {
578                 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
579                         "uid %ld\n", strerror(errno), (long)uid);
580                 return 0;
581         }
582         switch (pid = fork()) {
583         case -1:
584                 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
585                         "uid %ld\n", strerror(errno), (long)uid);
586                 close(pipev[0]);
587                 close(pipev[1]);
588                 return 0;
589
590         case 0:
591                 close(pipev[1]);
592                 if (dup2(pipev[0], 0) == -1) {
593                         perror("dup2");
594                         exit(1);
595                 }
596                 close(pipev[0]);
597
598                 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
599                 perror(CTDLDIR "/chkpwd");
600                 exit(1);
601         }
602
603         close(pipev[0]);
604         write(pipev[1], buf,
605               snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
606         write(pipev[1], pass, strlen(pass));
607         write(pipev[1], "\n", 1);
608         close(pipev[1]);
609
610         while (waitpid(pid, &status, 0) == -1)
611                 if (errno != EINTR) {
612                         lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
613                                 "access for uid %ld\n",
614                                 strerror(errno), (long)uid);
615                         return 0;
616                 }
617         if (WIFEXITED(status) && !WEXITSTATUS(status))
618                 return 1;
619
620         return 0;
621 }
622 #endif
623
624 void do_login()
625 {
626         (CC->logged_in) = 1;
627         session_startup();
628 }
629
630
631 int CtdlTryPassword(char *password)
632 {
633         int code;
634
635         if ((CC->logged_in)) {
636                 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
637                 return pass_already_logged_in;
638         }
639         if (!strcmp(CC->curr_user, NLI)) {
640                 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
641                 return pass_no_user;
642         }
643         if (getuser(&CC->user, CC->curr_user)) {
644                 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
645                 return pass_internal_error;
646         }
647         if (password == NULL) {
648                 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
649                 return pass_wrong_password;
650         }
651         code = (-1);
652
653
654 #ifdef ENABLE_AUTOLOGIN
655
656         if (validpw(CC->user.uid, password)) {
657                 code = 0;
658                 /* we could get rid of this */
659                 lgetuser(&CC->user, CC->curr_user);
660                 safestrncpy(CC->user.password, password, sizeof CC->user.password);
661                 lputuser(&CC->user);
662                 /*                          */
663         }
664         else {
665                 code = (-1);
666         }
667
668 #else /* ENABLE_AUTOLOGIN */
669
670         strproc(password);
671         strproc(CC->user.password);
672         code = strcasecmp(CC->user.password, password);
673         strproc(password);
674         strproc(CC->user.password);
675         code = strcasecmp(CC->user.password, password);
676
677 #endif /* ENABLE_AUTOLOGIN */
678
679         if (!code) {
680                 do_login();
681                 return pass_ok;
682         } else {
683                 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
684                 return pass_wrong_password;
685         }
686 }
687
688
689 void cmd_pass(char *buf)
690 {
691         char password[256];
692         int a;
693
694         extract_token(password, buf, 0, '|', sizeof password);
695         a = CtdlTryPassword(password);
696
697         switch (a) {
698         case pass_already_logged_in:
699                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
700                 return;
701         case pass_no_user:
702                 cprintf("%d You must send a name with USER first.\n",
703                         ERROR + USERNAME_REQUIRED);
704                 return;
705         case pass_wrong_password:
706                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
707                 return;
708         case pass_ok:
709                 logged_in_response();
710                 return;
711         }
712 }
713
714
715
716 /*
717  * Delete a user record *and* all of its related resources.
718  */
719 int purge_user(char pname[])
720 {
721         char filename[64];
722         struct ctdluser usbuf;
723         char usernamekey[USERNAME_SIZE];
724         struct CitContext *ccptr;
725         int user_is_logged_in = 0;
726
727         makeuserkey(usernamekey, pname);
728
729         if (getuser(&usbuf, pname) != 0) {
730                 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
731                 return (ERROR + NO_SUCH_USER);
732         }
733         /* Don't delete a user who is currently logged in.  Instead, just
734          * set the access level to 0, and let the account get swept up
735          * during the next purge.
736          */
737         user_is_logged_in = 0;
738         begin_critical_section(S_SESSION_TABLE);
739         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
740                 if (ccptr->user.usernum == usbuf.usernum) {
741                         user_is_logged_in = 1;
742                 }
743         }
744         end_critical_section(S_SESSION_TABLE);
745         if (user_is_logged_in == 1) {
746                 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
747                 usbuf.axlevel = 0;
748                 putuser(&usbuf);
749                 return (1);
750         }
751         lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
752
753         /* Perform any purge functions registered by server extensions */
754         PerformUserHooks(&usbuf, EVT_PURGEUSER);
755
756         /* delete any existing user/room relationships */
757         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
758
759         /* delete the userlog entry */
760         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
761
762         /* remove the user's bio file */
763         snprintf(filename, 
764                          sizeof filename, 
765                          "%s/%ld",
766                          ctdl_bio_dir,
767                          usbuf.usernum);
768         unlink(filename);
769
770         /* remove the user's picture */
771         snprintf(filename, 
772                          sizeof filename, 
773                          "%s/%ld.gif",
774                          ctdl_image_dir,
775                          usbuf.usernum);
776         unlink(filename);
777
778         return (0);
779 }
780
781
782 /*
783  * create_user()  -  back end processing to create a new user
784  *
785  * Set 'newusername' to the desired account name.
786  * Set 'become_user' to nonzero if this is self-service account creation and we want
787  * to actually log in as the user we just created, otherwise set it to 0.
788  */
789 int create_user(char *newusername, int become_user)
790 {
791         struct ctdluser usbuf;
792         struct ctdlroom qrbuf;
793         char username[256];
794         char mailboxname[ROOMNAMELEN];
795         uid_t uid;
796
797         safestrncpy(username, newusername, sizeof username);
798         strproc(username);
799
800 #ifdef ENABLE_AUTOLOGIN
801
802         struct passwd *p = (struct passwd *) getpwnam(username);
803
804         if (p != NULL) {
805                 extract_token(username, p->pw_gecos, 0, ',', sizeof username);
806                 uid = p->pw_uid;
807         } else {
808                 uid = (-1);
809         }
810
811 #else
812
813         uid = (-1);
814
815 #endif
816
817         if (!getuser(&usbuf, username)) {
818                 return (ERROR + ALREADY_EXISTS);
819         }
820
821         /* Go ahead and initialize a new user record */
822         memset(&usbuf, 0, sizeof(struct ctdluser));
823         safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
824         strcpy(usbuf.password, "");
825         usbuf.uid = uid;
826
827         /* These are the default flags on new accounts */
828         usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
829
830         usbuf.timescalled = 0;
831         usbuf.posted = 0;
832         usbuf.axlevel = config.c_initax;
833         usbuf.USscreenwidth = 80;
834         usbuf.USscreenheight = 24;
835         usbuf.lastcall = time(NULL);
836
837         /* fetch a new user number */
838         usbuf.usernum = get_new_user_number();
839
840         /* The very first user created on the system will always be an Aide */
841         if (usbuf.usernum == 1L) {
842                 usbuf.axlevel = 6;
843         }
844
845         /* add user to userlog */
846         putuser(&usbuf);
847
848         /*
849          * Give the user a private mailbox and a configuration room.
850          * Make the latter an invisible system room.
851          */
852         MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
853         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
854
855         MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
856         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
857         if (lgetroom(&qrbuf, mailboxname) == 0) {
858                 qrbuf.QRflags2 |= QR2_SYSTEM;
859                 lputroom(&qrbuf);
860         }
861
862         /* Perform any create functions registered by server extensions */
863         PerformUserHooks(&usbuf, EVT_NEWUSER);
864
865         /* Everything below this line can be bypassed if administratively
866          * creating a user, instead of doing self-service account creation
867          */
868
869         if (become_user) {
870                 /* Now become the user we just created */
871                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
872                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
873                 CC->logged_in = 1;
874         
875                 /* Check to make sure we're still who we think we are */
876                 if (getuser(&CC->user, CC->curr_user)) {
877                         return (ERROR + INTERNAL_ERROR);
878                 }
879         }
880
881         lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
882         return (0);
883 }
884
885
886
887
888 /*
889  * cmd_newu()  -  create a new user account and log in as that user
890  */
891 void cmd_newu(char *cmdbuf)
892 {
893         int a;
894         char username[26];
895
896 #ifdef ENABLE_AUTOLOGIN
897         cprintf("%d This system does not use native mode authentication.\n",
898                 ERROR + NOT_HERE);
899         return;
900 #endif /* ENABLE_AUTOLOGIN */
901
902         if (config.c_disable_newu) {
903                 cprintf("%d Self-service user account creation "
904                         "is disabled on this system.\n", ERROR + NOT_HERE);
905                 return;
906         }
907
908         if (CC->logged_in) {
909                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
910                 return;
911         }
912         if (CC->nologin) {
913                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
914                         ERROR + MAX_SESSIONS_EXCEEDED,
915                         config.c_nodename, config.c_maxsessions);
916         }
917         extract_token(username, cmdbuf, 0, '|', sizeof username);
918         username[25] = 0;
919         strproc(username);
920
921         if (strlen(username) == 0) {
922                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
923                 return;
924         }
925
926         if ((!strcasecmp(username, "bbs")) ||
927             (!strcasecmp(username, "new")) ||
928             (!strcasecmp(username, "."))) {
929                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
930                 return;
931         }
932
933         a = create_user(username, 1);
934
935         if (a == 0) {
936                 session_startup();
937                 logged_in_response();
938         } else if (a == ERROR + ALREADY_EXISTS) {
939                 cprintf("%d '%s' already exists.\n",
940                         ERROR + ALREADY_EXISTS, username);
941                 return;
942         } else if (a == ERROR + INTERNAL_ERROR) {
943                 cprintf("%d Internal error - user record disappeared?\n",
944                         ERROR + INTERNAL_ERROR);
945                 return;
946         } else {
947                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
948         }
949 }
950
951
952
953 /*
954  * set password
955  */
956 void cmd_setp(char *new_pw)
957 {
958         if (CtdlAccessCheck(ac_logged_in)) {
959                 return;
960         }
961         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
962                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
963                 return;
964         }
965         strproc(new_pw);
966         if (strlen(new_pw) == 0) {
967                 cprintf("%d Password unchanged.\n", CIT_OK);
968                 return;
969         }
970         lgetuser(&CC->user, CC->curr_user);
971         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
972         lputuser(&CC->user);
973         cprintf("%d Password changed.\n", CIT_OK);
974         lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
975         PerformSessionHooks(EVT_SETPASS);
976 }
977
978
979 /*
980  * cmd_creu() - administratively create a new user account (do not log in to it)
981  */
982 void cmd_creu(char *cmdbuf)
983 {
984         int a;
985         char username[26];
986         char password[32];
987         struct ctdluser tmp;
988
989         if (CtdlAccessCheck(ac_aide)) {
990                 return;
991         }
992
993         extract_token(username, cmdbuf, 0, '|', sizeof username);
994         extract_token(password, cmdbuf, 1, '|', sizeof password);
995         username[25] = 0;
996         password[31] = 0;
997         strproc(username);
998         strproc(password);
999
1000         if (strlen(username) == 0) {
1001                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1002                 return;
1003         }
1004
1005         a = create_user(username, 0);
1006
1007         if (a == 0) {
1008                 if (strlen(password) > 0) {
1009                         lgetuser(&tmp, username);
1010                         safestrncpy(tmp.password, password, sizeof(tmp.password));
1011                         lputuser(&tmp);
1012                 }
1013                 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1014                                 (strlen(password) > 0) ? "and password set" :
1015                                 "with no password");
1016                 return;
1017         } else if (a == ERROR + ALREADY_EXISTS) {
1018                 cprintf("%d '%s' already exists.\n",
1019                         ERROR + ALREADY_EXISTS, username);
1020                 return;
1021         } else {
1022                 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1023         }
1024 }
1025
1026
1027
1028 /*
1029  * get user parameters
1030  */
1031 void cmd_getu(void)
1032 {
1033
1034         if (CtdlAccessCheck(ac_logged_in))
1035                 return;
1036
1037         getuser(&CC->user, CC->curr_user);
1038         cprintf("%d %d|%d|%d|\n",
1039                 CIT_OK,
1040                 CC->user.USscreenwidth,
1041                 CC->user.USscreenheight,
1042                 (CC->user.flags & US_USER_SET)
1043             );
1044 }
1045
1046 /*
1047  * set user parameters
1048  */
1049 void cmd_setu(char *new_parms)
1050 {
1051         if (CtdlAccessCheck(ac_logged_in))
1052                 return;
1053
1054         if (num_parms(new_parms) < 3) {
1055                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1056                 return;
1057         }
1058         lgetuser(&CC->user, CC->curr_user);
1059         CC->user.USscreenwidth = extract_int(new_parms, 0);
1060         CC->user.USscreenheight = extract_int(new_parms, 1);
1061         CC->user.flags = CC->user.flags & (~US_USER_SET);
1062         CC->user.flags = CC->user.flags |
1063             (extract_int(new_parms, 2) & US_USER_SET);
1064
1065         lputuser(&CC->user);
1066         cprintf("%d Ok\n", CIT_OK);
1067 }
1068
1069 /*
1070  * set last read pointer
1071  */
1072 void cmd_slrp(char *new_ptr)
1073 {
1074         long newlr;
1075         struct visit vbuf;
1076         struct visit original_vbuf;
1077
1078         if (CtdlAccessCheck(ac_logged_in)) {
1079                 return;
1080         }
1081
1082         if (!strncasecmp(new_ptr, "highest", 7)) {
1083                 newlr = CC->room.QRhighest;
1084         } else {
1085                 newlr = atol(new_ptr);
1086         }
1087
1088         lgetuser(&CC->user, CC->curr_user);
1089
1090         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1091         memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1092         vbuf.v_lastseen = newlr;
1093         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1094
1095         /* Only rewrite the record if it changed */
1096         if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1097            || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1098                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1099         }
1100
1101         lputuser(&CC->user);
1102         cprintf("%d %ld\n", CIT_OK, newlr);
1103 }
1104
1105
1106 void cmd_seen(char *argbuf) {
1107         long target_msgnum = 0L;
1108         int target_setting = 0;
1109
1110         if (CtdlAccessCheck(ac_logged_in)) {
1111                 return;
1112         }
1113
1114         if (num_parms(argbuf) != 2) {
1115                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1116                 return;
1117         }
1118
1119         target_msgnum = extract_long(argbuf, 0);
1120         target_setting = extract_int(argbuf, 1);
1121
1122         CtdlSetSeen(&target_msgnum, 1, target_setting,
1123                         ctdlsetseen_seen, NULL, NULL);
1124         cprintf("%d OK\n", CIT_OK);
1125 }
1126
1127
1128 void cmd_gtsn(char *argbuf) {
1129         char buf[SIZ];
1130
1131         if (CtdlAccessCheck(ac_logged_in)) {
1132                 return;
1133         }
1134
1135         CtdlGetSeen(buf, ctdlsetseen_seen);
1136         cprintf("%d %s\n", CIT_OK, buf);
1137 }
1138
1139
1140 /*
1141  * API function for cmd_invt_kick() and anything else that needs to
1142  * invite or kick out a user to/from a room.
1143  * 
1144  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1145  */
1146 int CtdlInvtKick(char *iuser, int op) {
1147         struct ctdluser USscratch;
1148         struct visit vbuf;
1149         char bbb[SIZ];
1150
1151         if (getuser(&USscratch, iuser) != 0) {
1152                 return(1);
1153         }
1154
1155         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1156         if (op == 1) {
1157                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1158                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1159         }
1160         if (op == 0) {
1161                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1162                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1163         }
1164         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1165
1166         /* post a message in Aide> saying what we just did */
1167         snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1168                 iuser,
1169                 ((op == 1) ? "invited to" : "kicked out of"),
1170                 CC->room.QRname,
1171                 CC->user.fullname);
1172         aide_message(bbb);
1173
1174         return(0);
1175 }
1176
1177
1178 /*
1179  * INVT and KICK commands
1180  */
1181 void cmd_invt_kick(char *iuser, int op) {
1182
1183         /*
1184          * These commands are only allowed by aides, room aides,
1185          * and room namespace owners
1186          */
1187         if (is_room_aide()
1188            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1189                 /* access granted */
1190         } else {
1191                 /* access denied */
1192                 cprintf("%d Higher access or room ownership required.\n",
1193                         ERROR + HIGHER_ACCESS_REQUIRED);
1194                 return;
1195         }
1196
1197         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1198                          ROOMNAMELEN)) {
1199                 cprintf("%d Can't add/remove users from this room.\n",
1200                         ERROR + NOT_HERE);
1201                 return;
1202         }
1203
1204         if (CtdlInvtKick(iuser, op) != 0) {
1205                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1206                 return;
1207         }
1208
1209         cprintf("%d %s %s %s.\n",
1210                 CIT_OK, iuser,
1211                 ((op == 1) ? "invited to" : "kicked out of"),
1212                 CC->room.QRname);
1213         return;
1214 }
1215
1216
1217 /*
1218  * Forget (Zap) the current room (API call)
1219  * Returns 0 on success
1220  */
1221 int CtdlForgetThisRoom(void) {
1222         struct visit vbuf;
1223
1224         /* On some systems, Aides are not allowed to forget rooms */
1225         if (is_aide() && (config.c_aide_zap == 0)
1226            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1227                 return(1);
1228         }
1229
1230         lgetuser(&CC->user, CC->curr_user);
1231         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1232
1233         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1234         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1235
1236         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1237         lputuser(&CC->user);
1238
1239         /* Return to the Lobby, so we don't end up in an undefined room */
1240         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1241         return(0);
1242
1243 }
1244
1245
1246 /*
1247  * forget (Zap) the current room
1248  */
1249 void cmd_forg(void)
1250 {
1251
1252         if (CtdlAccessCheck(ac_logged_in)) {
1253                 return;
1254         }
1255
1256         if (CtdlForgetThisRoom() == 0) {
1257                 cprintf("%d Ok\n", CIT_OK);
1258         }
1259         else {
1260                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1261         }
1262 }
1263
1264 /*
1265  * Get Next Unregistered User
1266  */
1267 void cmd_gnur(void)
1268 {
1269         struct cdbdata *cdbus;
1270         struct ctdluser usbuf;
1271
1272         if (CtdlAccessCheck(ac_aide)) {
1273                 return;
1274         }
1275
1276         if ((CitControl.MMflags & MM_VALID) == 0) {
1277                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1278                 return;
1279         }
1280
1281         /* There are unvalidated users.  Traverse the user database,
1282          * and return the first user we find that needs validation.
1283          */
1284         cdb_rewind(CDB_USERS);
1285         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1286                 memset(&usbuf, 0, sizeof(struct ctdluser));
1287                 memcpy(&usbuf, cdbus->ptr,
1288                        ((cdbus->len > sizeof(struct ctdluser)) ?
1289                         sizeof(struct ctdluser) : cdbus->len));
1290                 cdb_free(cdbus);
1291                 if ((usbuf.flags & US_NEEDVALID)
1292                     && (usbuf.axlevel > 0)) {
1293                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1294                         cdb_close_cursor(CDB_USERS);
1295                         return;
1296                 }
1297         }
1298
1299         /* If we get to this point, there are no more unvalidated users.
1300          * Therefore we clear the "users need validation" flag.
1301          */
1302
1303         begin_critical_section(S_CONTROL);
1304         get_control();
1305         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1306         put_control();
1307         end_critical_section(S_CONTROL);
1308         cprintf("%d *** End of registration.\n", CIT_OK);
1309
1310
1311 }
1312
1313
1314 /*
1315  * validate a user
1316  */
1317 void cmd_vali(char *v_args)
1318 {
1319         char user[128];
1320         int newax;
1321         struct ctdluser userbuf;
1322
1323         extract_token(user, v_args, 0, '|', sizeof user);
1324         newax = extract_int(v_args, 1);
1325
1326         if (CtdlAccessCheck(ac_aide)) {
1327                 return;
1328         }
1329
1330         if (lgetuser(&userbuf, user) != 0) {
1331                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1332                 return;
1333         }
1334
1335         userbuf.axlevel = newax;
1336         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1337
1338         lputuser(&userbuf);
1339
1340         /* If the access level was set to zero, delete the user */
1341         if (newax == 0) {
1342                 if (purge_user(user) == 0) {
1343                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1344                         return;
1345                 }
1346         }
1347         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1348 }
1349
1350
1351
1352 /* 
1353  *  Traverse the user file...
1354  */
1355 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1356                  void *in_data)
1357 {
1358         struct ctdluser usbuf;
1359         struct cdbdata *cdbus;
1360
1361         cdb_rewind(CDB_USERS);
1362
1363         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1364                 memset(&usbuf, 0, sizeof(struct ctdluser));
1365                 memcpy(&usbuf, cdbus->ptr,
1366                        ((cdbus->len > sizeof(struct ctdluser)) ?
1367                         sizeof(struct ctdluser) : cdbus->len));
1368                 cdb_free(cdbus);
1369                 (*CallBack) (&usbuf, in_data);
1370         }
1371 }
1372
1373
1374 /*
1375  * List one user (this works with cmd_list)
1376  */
1377 void ListThisUser(struct ctdluser *usbuf, void *data)
1378 {
1379         char *searchstring;
1380
1381         searchstring = (char *)data;
1382         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1383                 return;
1384         }
1385
1386         if (usbuf->axlevel > 0) {
1387                 if ((CC->user.axlevel >= 6)
1388                     || ((usbuf->flags & US_UNLISTED) == 0)
1389                     || ((CC->internal_pgm))) {
1390                         cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1391                                 usbuf->fullname,
1392                                 usbuf->axlevel,
1393                                 usbuf->usernum,
1394                                 (long)usbuf->lastcall,
1395                                 usbuf->timescalled,
1396                                 usbuf->posted);
1397                         if (CC->user.axlevel >= 6)
1398                                 cprintf("%s", usbuf->password);
1399                         cprintf("\n");
1400                 }
1401         }
1402 }
1403
1404 /* 
1405  *  List users (searchstring may be empty to list all users)
1406  */
1407 void cmd_list(char *cmdbuf)
1408 {
1409         char searchstring[256];
1410         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1411         striplt(searchstring);
1412         cprintf("%d \n", LISTING_FOLLOWS);
1413         ForEachUser(ListThisUser, (void *)searchstring );
1414         cprintf("000\n");
1415 }
1416
1417
1418
1419
1420 /*
1421  * assorted info we need to check at login
1422  */
1423 void cmd_chek(void)
1424 {
1425         int mail = 0;
1426         int regis = 0;
1427         int vali = 0;
1428
1429         if (CtdlAccessCheck(ac_logged_in)) {
1430                 return;
1431         }
1432
1433         getuser(&CC->user, CC->curr_user);      /* no lock is needed here */
1434         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1435                 regis = 1;
1436
1437         if (CC->user.axlevel >= 6) {
1438                 get_control();
1439                 if (CitControl.MMflags & MM_VALID)
1440                         vali = 1;
1441         }
1442
1443         /* check for mail */
1444         mail = InitialMailCheck();
1445
1446         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1447 }
1448
1449
1450 /*
1451  * check to see if a user exists
1452  */
1453 void cmd_qusr(char *who)
1454 {
1455         struct ctdluser usbuf;
1456
1457         if (getuser(&usbuf, who) == 0) {
1458                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1459         } else {
1460                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1461         }
1462 }
1463
1464
1465 /*
1466  * Administrative Get User Parameters
1467  */
1468 void cmd_agup(char *cmdbuf)
1469 {
1470         struct ctdluser usbuf;
1471         char requested_user[128];
1472
1473         if (CtdlAccessCheck(ac_aide)) {
1474                 return;
1475         }
1476
1477         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1478         if (getuser(&usbuf, requested_user) != 0) {
1479                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1480                 return;
1481         }
1482         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1483                 CIT_OK,
1484                 usbuf.fullname,
1485                 usbuf.password,
1486                 usbuf.flags,
1487                 usbuf.timescalled,
1488                 usbuf.posted,
1489                 (int) usbuf.axlevel,
1490                 usbuf.usernum,
1491                 (long)usbuf.lastcall,
1492                 usbuf.USuserpurge);
1493 }
1494
1495
1496
1497 /*
1498  * Administrative Set User Parameters
1499  */
1500 void cmd_asup(char *cmdbuf)
1501 {
1502         struct ctdluser usbuf;
1503         char requested_user[128];
1504         char notify[SIZ];
1505         int np;
1506         int newax;
1507         int deleted = 0;
1508
1509         if (CtdlAccessCheck(ac_aide))
1510                 return;
1511
1512         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1513         if (lgetuser(&usbuf, requested_user) != 0) {
1514                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1515                 return;
1516         }
1517         np = num_parms(cmdbuf);
1518         if (np > 1)
1519                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1520         if (np > 2)
1521                 usbuf.flags = extract_int(cmdbuf, 2);
1522         if (np > 3)
1523                 usbuf.timescalled = extract_int(cmdbuf, 3);
1524         if (np > 4)
1525                 usbuf.posted = extract_int(cmdbuf, 4);
1526         if (np > 5) {
1527                 newax = extract_int(cmdbuf, 5);
1528                 if ((newax >= 0) && (newax <= 6)) {
1529                         usbuf.axlevel = extract_int(cmdbuf, 5);
1530                 }
1531         }
1532         if (np > 7) {
1533                 usbuf.lastcall = extract_long(cmdbuf, 7);
1534         }
1535         if (np > 8) {
1536                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1537         }
1538         lputuser(&usbuf);
1539         if (usbuf.axlevel == 0) {
1540                 if (purge_user(requested_user) == 0) {
1541                         deleted = 1;
1542                 }
1543         }
1544
1545         if (deleted) {
1546                 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1547                         usbuf.fullname, CC->user.fullname);
1548                 aide_message(notify);
1549         }
1550
1551         cprintf("%d Ok", CIT_OK);
1552         if (deleted)
1553                 cprintf(" (%s deleted)", requested_user);
1554         cprintf("\n");
1555 }
1556
1557
1558
1559 /*
1560  * Check to see if the user who we just sent mail to is logged in.  If yes,
1561  * bump the 'new mail' counter for their session.  That enables them to
1562  * receive a new mail notification without having to hit the database.
1563  */
1564 void BumpNewMailCounter(long which_user) {
1565         struct CitContext *ptr;
1566
1567         begin_critical_section(S_SESSION_TABLE);
1568
1569         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1570                 if (ptr->user.usernum == which_user) {
1571                         ptr->newmail += 1;
1572                 }
1573         }
1574
1575         end_critical_section(S_SESSION_TABLE);
1576 }
1577
1578
1579 /*
1580  * Count the number of new mail messages the user has
1581  */
1582 int NewMailCount()
1583 {
1584         int num_newmsgs = 0;
1585
1586         num_newmsgs = CC->newmail;
1587         CC->newmail = 0;
1588
1589         return (num_newmsgs);
1590 }
1591
1592
1593 /*
1594  * Count the number of new mail messages the user has
1595  */
1596 int InitialMailCheck()
1597 {
1598         int num_newmsgs = 0;
1599         int a;
1600         char mailboxname[ROOMNAMELEN];
1601         struct ctdlroom mailbox;
1602         struct visit vbuf;
1603         struct cdbdata *cdbfr;
1604         long *msglist = NULL;
1605         int num_msgs = 0;
1606
1607         MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1608         if (getroom(&mailbox, mailboxname) != 0)
1609                 return (0);
1610         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1611
1612         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1613
1614         if (cdbfr != NULL) {
1615                 msglist = malloc(cdbfr->len);
1616                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1617                 num_msgs = cdbfr->len / sizeof(long);
1618                 cdb_free(cdbfr);
1619         }
1620         if (num_msgs > 0)
1621                 for (a = 0; a < num_msgs; ++a) {
1622                         if (msglist[a] > 0L) {
1623                                 if (msglist[a] > vbuf.v_lastseen) {
1624                                         ++num_newmsgs;
1625                                 }
1626                         }
1627                 }
1628         if (msglist != NULL)
1629                 free(msglist);
1630
1631         return (num_newmsgs);
1632 }
1633
1634
1635
1636 /*
1637  * Set the preferred view for the current user/room combination
1638  */
1639 void cmd_view(char *cmdbuf) {
1640         int requested_view;
1641         struct visit vbuf;
1642
1643         if (CtdlAccessCheck(ac_logged_in)) {
1644                 return;
1645         }
1646
1647         requested_view = extract_int(cmdbuf, 0);
1648
1649         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1650         vbuf.v_view = requested_view;
1651         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1652         
1653         cprintf("%d ok\n", CIT_OK);
1654 }