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