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