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