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