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