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