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