fd8056559c4ba479eaec5eb8570c63ebf11ca47c
[citadel.git] / citadel / user_ops.c
1 /* 
2  * Server functions which perform operations on user objects.
3  *
4  * Copyright (c) 1987-2011 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <ctype.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <syslog.h>
27 #ifdef HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # if HAVE_SYS_TIME_H
36 #  include <sys/time.h>
37 # else
38 #  include <time.h>
39 # endif
40 #endif
41
42 #include <string.h>
43 #include <limits.h>
44 #include <libcitadel.h>
45 #include "auth.h"
46 #include "citadel.h"
47 #include "server.h"
48 #include "database.h"
49 #include "sysdep_decls.h"
50 #include "support.h"
51 #include "room_ops.h"
52 #include "file_ops.h"
53 #include "control.h"
54 #include "msgbase.h"
55 #include "config.h"
56 #include "citserver.h"
57 #include "citadel_dirs.h"
58 #include "genstamp.h"
59 #include "threads.h"
60 #include "citadel_ldap.h"
61 #include "context.h"
62 #include "ctdl_module.h"
63 #include "user_ops.h"
64
65 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
66 int chkpwd_write_pipe[2];
67 int chkpwd_read_pipe[2];
68
69
70
71 /*
72  * getuser()  -  retrieve named user into supplied buffer.
73  *             returns 0 on success
74  */
75 int getuser(struct ctdluser *usbuf, char name[])
76 {
77         return CtdlGetUser(usbuf, name);
78 }
79
80
81 /*
82  * CtdlGetUser()  -  retrieve named user into supplied buffer.
83  *             returns 0 on success
84  */
85 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
86 {
87
88         char usernamekey[USERNAME_SIZE];
89         struct cdbdata *cdbus;
90
91         if (usbuf != NULL) {
92                 memset(usbuf, 0, sizeof(struct ctdluser));
93         }
94
95         makeuserkey(usernamekey, name, len);
96         cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
97
98         if (cdbus == NULL) {    /* user not found */
99                 return(1);
100         }
101         if (usbuf != NULL) {
102                 memcpy(usbuf, cdbus->ptr,
103                         ((cdbus->len > sizeof(struct ctdluser)) ?
104                          sizeof(struct ctdluser) : cdbus->len));
105         }
106         cdb_free(cdbus);
107
108         return (0);
109 }
110
111
112 int CtdlGetUser(struct ctdluser *usbuf, char *name)
113 {
114         return CtdlGetUserLen(usbuf, name, cutuserkey(name));
115 }
116
117
118 /*
119  * CtdlGetUserLock()  -  same as getuser() but locks the record
120  */
121 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
122 {
123         int retcode;
124
125         retcode = CtdlGetUser(usbuf, name);
126         if (retcode == 0) {
127                 begin_critical_section(S_USERS);
128         }
129         return (retcode);
130 }
131
132
133 /*
134  * lgetuser()  -  same as getuser() but locks the record
135  */
136 int lgetuser(struct ctdluser *usbuf, char *name)
137 {
138         return CtdlGetUserLock(usbuf, name);
139 }
140
141
142 /*
143  * CtdlPutUser()  -  write user buffer into the correct place on disk
144  */
145 void CtdlPutUser(struct ctdluser *usbuf)
146 {
147         char usernamekey[USERNAME_SIZE];
148
149         makeuserkey(usernamekey, 
150                     usbuf->fullname, 
151                     cutuserkey(usbuf->fullname));
152
153         usbuf->version = REV_LEVEL;
154         cdb_store(CDB_USERS,
155                   usernamekey, strlen(usernamekey),
156                   usbuf, sizeof(struct ctdluser));
157
158 }
159
160
161 /*
162  * putuser()  -  write user buffer into the correct place on disk
163  */
164 void putuser(struct ctdluser *usbuf)
165 {
166         CtdlPutUser(usbuf);
167 }
168
169
170 /*
171  * CtdlPutUserLock()  -  same as putuser() but locks the record
172  */
173 void CtdlPutUserLock(struct ctdluser *usbuf)
174 {
175         CtdlPutUser(usbuf);
176         end_critical_section(S_USERS);
177 }
178
179
180 /*
181  * lputuser()  -  same as putuser() but locks the record
182  */
183 void lputuser(struct ctdluser *usbuf)
184 {
185         CtdlPutUserLock(usbuf);
186 }
187
188
189 /*
190  * rename_user()  -  this is tricky because the user's display name is the database key
191  *
192  * Returns 0 on success or nonzero if there was an error...
193  *
194  */
195 int rename_user(char *oldname, char *newname) {
196         int retcode = RENAMEUSER_OK;
197         struct ctdluser usbuf;
198
199         char oldnamekey[USERNAME_SIZE];
200         char newnamekey[USERNAME_SIZE];
201
202         /* Create the database keys... */
203         makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
204         makeuserkey(newnamekey, newname, cutuserkey(newname));
205
206         /* Lock up and get going */
207         begin_critical_section(S_USERS);
208
209         /* We cannot rename a user who is currently logged in */
210         if (CtdlIsUserLoggedIn(oldname)) {
211                 end_critical_section(S_USERS);
212                 return RENAMEUSER_LOGGED_IN;
213         }
214
215         if (CtdlGetUser(&usbuf, newname) == 0) {
216                 retcode = RENAMEUSER_ALREADY_EXISTS;
217         }
218         else {
219
220                 if (CtdlGetUser(&usbuf, oldname) != 0) {
221                         retcode = RENAMEUSER_NOT_FOUND;
222                 }
223
224                 else {          /* Sanity checks succeeded.  Now rename the user. */
225                         if (usbuf.usernum == 0)
226                         {
227                                 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
228                                 retcode = RENAMEUSER_NOT_FOUND;
229                         } else {
230                                 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
231                                 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
232                                 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
233                                 CtdlPutUser(&usbuf);
234                                 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
235                                         usbuf.fullname, strlen(usbuf.fullname)+1 );
236
237                                 retcode = RENAMEUSER_OK;
238                         }
239                 }
240         
241         }
242
243         end_critical_section(S_USERS);
244         return(retcode);
245 }
246
247
248
249 /*
250  * Index-generating function used by Ctdl[Get|Set]Relationship
251  */
252 int GenerateRelationshipIndex(char *IndexBuf,
253                               long RoomID,
254                               long RoomGen,
255                               long UserID)
256 {
257
258         struct {
259                 long iRoomID;
260                 long iRoomGen;
261                 long iUserID;
262         } TheIndex;
263
264         TheIndex.iRoomID = RoomID;
265         TheIndex.iRoomGen = RoomGen;
266         TheIndex.iUserID = UserID;
267
268         memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
269         return (sizeof(TheIndex));
270 }
271
272
273
274 /*
275  * Back end for CtdlSetRelationship()
276  */
277 void put_visit(visit *newvisit)
278 {
279         char IndexBuf[32];
280         int IndexLen = 0;
281
282         memset (IndexBuf, 0, sizeof (IndexBuf));
283         /* Generate an index */
284         IndexLen = GenerateRelationshipIndex(IndexBuf,
285                                              newvisit->v_roomnum,
286                                              newvisit->v_roomgen,
287                                              newvisit->v_usernum);
288
289         /* Store the record */
290         cdb_store(CDB_VISIT, IndexBuf, IndexLen,
291                   newvisit, sizeof(visit)
292         );
293 }
294
295
296
297
298 /*
299  * Define a relationship between a user and a room
300  */
301 void CtdlSetRelationship(visit *newvisit,
302                          struct ctdluser *rel_user,
303                          struct ctdlroom *rel_room)
304 {
305
306
307         /* We don't use these in Citadel because they're implicit by the
308          * index, but they must be present if the database is exported.
309          */
310         newvisit->v_roomnum = rel_room->QRnumber;
311         newvisit->v_roomgen = rel_room->QRgen;
312         newvisit->v_usernum = rel_user->usernum;
313
314         put_visit(newvisit);
315 }
316
317 /*
318  * Locate a relationship between a user and a room
319  */
320 void CtdlGetRelationship(visit *vbuf,
321                          struct ctdluser *rel_user,
322                          struct ctdlroom *rel_room)
323 {
324
325         char IndexBuf[32];
326         int IndexLen;
327         struct cdbdata *cdbvisit;
328
329         /* Generate an index */
330         IndexLen = GenerateRelationshipIndex(IndexBuf,
331                                              rel_room->QRnumber,
332                                              rel_room->QRgen,
333                                              rel_user->usernum);
334
335         /* Clear out the buffer */
336         memset(vbuf, 0, sizeof(visit));
337
338         cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
339         if (cdbvisit != NULL) {
340                 memcpy(vbuf, cdbvisit->ptr,
341                        ((cdbvisit->len > sizeof(visit)) ?
342                         sizeof(visit) : cdbvisit->len));
343                 cdb_free(cdbvisit);
344         }
345         else {
346                 /* If this is the first time the user has seen this room,
347                  * set the view to be the default for the room.
348                  */
349                 vbuf->v_view = rel_room->QRdefaultview;
350         }
351
352         /* Set v_seen if necessary */
353         if (vbuf->v_seen[0] == 0) {
354                 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
355         }
356 }
357
358
359 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
360 {
361         snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
362 }
363
364
365 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
366 {
367         snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
368 }
369
370
371 /*
372  * Is the user currently logged in an Admin?
373  */
374 int is_aide(void)
375 {
376         if (CC->user.axlevel >= AxAideU)
377                 return (1);
378         else
379                 return (0);
380 }
381
382
383 /*
384  * Is the user currently logged in an Admin *or* the room Admin for this room?
385  */
386 int is_room_aide(void)
387 {
388
389         if (!CC->logged_in) {
390                 return (0);
391         }
392
393         if ((CC->user.axlevel >= AxAideU)
394             || (CC->room.QRroomaide == CC->user.usernum)) {
395                 return (1);
396         } else {
397                 return (0);
398         }
399 }
400
401 /*
402  * CtdlGetUserByNumber() -      get user by number
403  *                      returns 0 if user was found
404  *
405  * Note: fetching a user this way requires one additional database operation.
406  */
407 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
408 {
409         struct cdbdata *cdbun;
410         int r;
411
412         cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
413         if (cdbun == NULL) {
414                 CON_syslog(LOG_INFO, "User %ld not found\n", number);
415                 return(-1);
416         }
417
418         CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
419         r = CtdlGetUser(usbuf, cdbun->ptr);
420         cdb_free(cdbun);
421         return(r);
422 }
423
424 /*
425  * getuserbynumber() -  get user by number
426  *                      returns 0 if user was found
427  *
428  * Note: fetching a user this way requires one additional database operation.
429  */
430 int getuserbynumber(struct ctdluser *usbuf, long number)
431 {
432         return CtdlGetUserByNumber(usbuf, number);
433 }
434
435
436
437 /*
438  * Helper function for rebuild_usersbynumber()
439  */
440 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
441
442         struct ubnlist {
443                 struct ubnlist *next;
444                 char username[USERNAME_SIZE];
445                 long usernum;
446         };
447
448         static struct ubnlist *u = NULL;
449         struct ubnlist *ptr = NULL;
450
451         /* Lazy programming here.  Call this function as a ForEachUser backend
452          * in order to queue up the room names, or call it with a null user
453          * to make it do the processing.
454          */
455         if (usbuf != NULL) {
456                 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
457                 if (ptr == NULL) return;
458
459                 ptr->usernum = usbuf->usernum;
460                 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
461                 ptr->next = u;
462                 u = ptr;
463                 return;
464         }
465
466         while (u != NULL) {
467                 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
468                         u->usernum, u->username);
469                 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
470
471                 ptr = u;
472                 u = u->next;
473                 free(ptr);
474         }
475 }
476
477
478
479 /*
480  * Rebuild the users-by-number index
481  */
482 void rebuild_usersbynumber(void) {
483         cdb_trunc(CDB_USERSBYNUMBER);                   /* delete the old indices */
484         ForEachUser(rebuild_ubn_for_user, NULL);        /* enumerate the users */
485         rebuild_ubn_for_user(NULL, NULL);               /* and index them */
486 }
487
488
489
490 /*
491  * getuserbyuid()  -     get user by system uid (for PAM mode authentication)
492  *                     returns 0 if user was found
493  *
494  * WARNING: don't use this function unless you absolutely have to.  It does
495  *        a sequential search and therefore is computationally expensive.
496  */
497 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
498 {
499         struct cdbdata *cdbus;
500
501         cdb_rewind(CDB_USERS);
502
503         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
504                 memset(usbuf, 0, sizeof(struct ctdluser));
505                 memcpy(usbuf, cdbus->ptr,
506                        ((cdbus->len > sizeof(struct ctdluser)) ?
507                         sizeof(struct ctdluser) : cdbus->len));
508                 cdb_free(cdbus);
509                 if (usbuf->uid == number) {
510                         cdb_close_cursor(CDB_USERS);
511                         return (0);
512                 }
513         }
514         return (-1);
515 }
516
517 /*
518  * Back end for cmd_user() and its ilk
519  *
520  * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
521  */
522 int CtdlLoginExistingUser(char *authname, const char *trythisname)
523 {
524         char username[SIZ];
525         int found_user;
526         long len;
527
528         CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
529
530         if ((CC->logged_in)) {
531                 return login_already_logged_in;
532         }
533
534         if (trythisname == NULL) return login_not_found;
535         
536         if (!strncasecmp(trythisname, "SYS_", 4))
537         {
538                 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
539                 return login_not_found;
540         }
541
542         /* If a "master user" is defined, handle its authentication if specified */
543         CC->is_master = 0;
544         if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
545                 if (!strcasecmp(authname, config.c_master_user)) {
546                         CC->is_master = 1;
547                 }
548         }
549
550         /* Continue attempting user validation... */
551         safestrncpy(username, trythisname, sizeof (username));
552         striplt(username);
553         len = cutuserkey(username);
554
555         if (IsEmptyStr(username)) {
556                 return login_not_found;
557         }
558
559         if (config.c_auth_mode == AUTHMODE_HOST) {
560
561                 /* host auth mode */
562
563                 struct passwd pd;
564                 struct passwd *tempPwdPtr;
565                 char pwdbuffer[256];
566         
567                 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
568 #ifdef HAVE_GETPWNAM_R
569 #ifdef SOLARIS_GETPWUID
570                 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
571                 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
572 #else // SOLARIS_GETPWUID
573                 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
574                 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
575 #endif // SOLARIS_GETPWUID
576 #else // HAVE_GETPWNAM_R
577                 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
578                 tempPwdPtr = NULL;
579 #endif // HAVE_GETPWNAM_R
580                 if (tempPwdPtr == NULL) {
581                         CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
582                         return login_not_found;
583                 }
584         
585                 /* Locate the associated Citadel account.
586                  * If not found, make one attempt to create it.
587                  */
588                 found_user = getuserbyuid(&CC->user, pd.pw_uid);
589                 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
590                         (long)pd.pw_uid, pd.pw_gecos, found_user);
591                 if (found_user != 0) {
592                         len = cutuserkey(username);
593                         create_user(username, len, 0);
594                         found_user = getuserbyuid(&CC->user, pd.pw_uid);
595                 }
596
597         }
598
599 #ifdef HAVE_LDAP
600         else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
601         
602                 /* LDAP auth mode */
603
604                 uid_t ldap_uid;
605                 char ldap_cn[256];
606                 char ldap_dn[256];
607
608                 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
609                 if (found_user != 0) {
610                         return login_not_found;
611                 }
612
613                 found_user = getuserbyuid(&CC->user, ldap_uid);
614                 if (found_user != 0) {
615                         create_user(username, len, 0);
616                         found_user = getuserbyuid(&CC->user, ldap_uid);
617                 }
618
619                 if (found_user == 0) {
620                         if (CC->ldap_dn != NULL) free(CC->ldap_dn);
621                         CC->ldap_dn = strdup(ldap_dn);
622                 }
623
624         }
625 #endif
626
627         else {
628                 /* native auth mode */
629
630                 struct recptypes *valid = NULL;
631         
632                 /* First, try to log in as if the supplied name is a display name */
633                 found_user = CtdlGetUser(&CC->user, username);
634         
635                 /* If that didn't work, try to log in as if the supplied name
636                 * is an e-mail address
637                 */
638                 if (found_user != 0) {
639                         valid = validate_recipients(username, NULL, 0);
640                         if (valid != NULL) {
641                                 if (valid->num_local == 1) {
642                                         found_user = CtdlGetUser(&CC->user, valid->recp_local);
643                                 }
644                                 free_recipients(valid);
645                         }
646                 }
647         }
648
649         /* Did we find something? */
650         if (found_user == 0) {
651                 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
652                         return login_too_many_users;
653                 } else {
654                         safestrncpy(CC->curr_user, CC->user.fullname,
655                                         sizeof CC->curr_user);
656                         return login_ok;
657                 }
658         }
659         return login_not_found;
660 }
661
662
663
664 /*
665  * USER cmd
666  */
667 void cmd_user(char *cmdbuf)
668 {
669         char username[256];
670         int a;
671
672         CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
673         extract_token(username, cmdbuf, 0, '|', sizeof username);
674         CON_syslog(LOG_DEBUG, "username: %s\n", username);
675         striplt(username);
676         CON_syslog(LOG_DEBUG, "username: %s\n", username);
677
678         a = CtdlLoginExistingUser(NULL, username);
679         switch (a) {
680         case login_already_logged_in:
681                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
682                 return;
683         case login_too_many_users:
684                 cprintf("%d %s: "
685                         "Too many users are already online "
686                         "(maximum is %d)\n",
687                         ERROR + MAX_SESSIONS_EXCEEDED,
688                         config.c_nodename, config.c_maxsessions);
689                 return;
690         case login_ok:
691                 cprintf("%d Password required for %s\n",
692                         MORE_DATA, CC->curr_user);
693                 return;
694         case login_not_found:
695                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
696                 return;
697         default:
698                 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
699         }
700 }
701
702
703
704 /*
705  * session startup code which is common to both cmd_pass() and cmd_newu()
706  */
707 void do_login(void)
708 {
709         struct CitContext *CCC = CC;
710
711         CCC->logged_in = 1;
712         CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
713
714         CtdlGetUserLock(&CCC->user, CCC->curr_user);
715         ++(CCC->user.timescalled);
716         CCC->previous_login = CCC->user.lastcall;
717         time(&CCC->user.lastcall);
718
719         /* If this user's name is the name of the system administrator
720          * (as specified in setup), automatically assign access level 6.
721          */
722         if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
723                 CCC->user.axlevel = AxAideU;
724         }
725
726         /* If we're authenticating off the host system, automatically give
727          * root the highest level of access.
728          */
729         if (config.c_auth_mode == AUTHMODE_HOST) {
730                 if (CCC->user.uid == 0) {
731                         CCC->user.axlevel = AxAideU;
732                 }
733         }
734
735         CtdlPutUserLock(&CCC->user);
736
737         /*
738          * Populate CCC->cs_inet_email with a default address.  This will be
739          * overwritten with the user's directory address, if one exists, when
740          * the vCard module's login hook runs.
741          */
742         snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
743                 CCC->user.fullname, config.c_fqdn);
744         convert_spaces_to_underscores(CCC->cs_inet_email);
745
746         /* Create any personal rooms required by the system.
747          * (Technically, MAILROOM should be there already, but just in case...)
748          */
749         CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
750         CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
751         CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
752         CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
753
754         /* Run any startup routines registered by loadable modules */
755         PerformSessionHooks(EVT_LOGIN);
756
757         /* Enter the lobby */
758         CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
759 }
760
761
762 void logged_in_response(void)
763 {
764         cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
765                 CIT_OK, CC->user.fullname, CC->user.axlevel,
766                 CC->user.timescalled, CC->user.posted,
767                 CC->user.flags, CC->user.usernum,
768                 CC->previous_login);
769 }
770
771
772
773 void CtdlUserLogout(void)
774 {
775         CitContext *CCC = MyContext();
776
777         CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
778                    CCC->curr_user, CCC->cs_pid
779         );
780
781         /*
782          * If there is a download in progress, abort it.
783          */
784         if (CCC->download_fp != NULL) {
785                 fclose(CCC->download_fp);
786                 CCC->download_fp = NULL;
787         }
788
789         /*
790          * If there is an upload in progress, abort it.
791          */
792         if (CCC->upload_fp != NULL) {
793                 abort_upl(CCC);
794         }
795
796         /* Run any hooks registered by modules... */
797         PerformSessionHooks(EVT_LOGOUT);
798         
799         /*
800          * Clear out some session data.  Most likely, the CitContext for this
801          * session is about to get nuked when the session disconnects, but
802          * since it's possible to log in again without reconnecting, we cannot
803          * make that assumption.
804          */
805         strcpy(CCC->fake_username, "");
806         strcpy(CCC->fake_hostname, "");
807         strcpy(CCC->fake_roomname, "");
808         CCC->logged_in = 0;
809
810         /* Check to see if the user was deleted whilst logged in and purge them if necessary */
811         if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
812                 purge_user(CCC->user.fullname);
813
814         /* Clear out the user record in memory so we don't behave like a ghost */
815         memset(&CCC->user, 0, sizeof(struct ctdluser));
816         CCC->curr_user[0] = 0;
817         CCC->is_master = 0;
818         CCC->cs_inet_email[0] = 0;
819         CCC->cs_inet_other_emails[0] = 0;
820         CCC->cs_inet_fn[0] = 0;
821         CCC->fake_username[0] = 0;
822         CCC->fake_hostname[0] = 0;
823         CCC->fake_roomname[0] = 0;
824         
825
826         /* Free any output buffers */
827         unbuffer_output();
828 }
829
830
831 /*
832  * Validate a password on the host unix system by talking to the chkpwd daemon
833  */
834 static int validpw(uid_t uid, const char *pass)
835 {
836         char buf[256];
837         int rv = 0;
838
839         if (IsEmptyStr(pass)) {
840                 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
841                 return 0;
842         }
843
844         CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
845
846         begin_critical_section(S_CHKPWD);
847         rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
848         if (rv == -1) {
849                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
850                 end_critical_section(S_CHKPWD);
851                 return 0;
852         }
853         rv = write(chkpwd_write_pipe[1], pass, 256);
854         if (rv == -1) {
855                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
856                 end_critical_section(S_CHKPWD);
857                 return 0;
858         }
859         rv = read(chkpwd_read_pipe[0], buf, 4);
860         if (rv == -1) {
861                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
862                 end_critical_section(S_CHKPWD);
863                 return 0;
864         }
865         end_critical_section(S_CHKPWD);
866
867         if (!strncmp(buf, "PASS", 4)) {
868                 CONM_syslog(LOG_DEBUG, "...pass\n");
869                 return(1);
870         }
871
872         CONM_syslog(LOG_DEBUG, "...fail\n");
873         return 0;
874 }
875
876 /* 
877  * Start up the chkpwd daemon so validpw() has something to talk to
878  */
879 void start_chkpwd_daemon(void) {
880         pid_t chkpwd_pid;
881         struct stat filestats;
882         int i;
883
884         CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
885
886         if ((stat(file_chkpwd, &filestats)==-1) ||
887             (filestats.st_size==0)){
888                 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
889                 abort();
890         }
891         if (pipe(chkpwd_write_pipe) != 0) {
892                 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
893                 abort();
894         }
895         if (pipe(chkpwd_read_pipe) != 0) {
896                 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
897                 abort();
898         }
899
900         chkpwd_pid = fork();
901         if (chkpwd_pid < 0) {
902                 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
903                 abort();
904         }
905         if (chkpwd_pid == 0) {
906                 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
907                 dup2(chkpwd_write_pipe[0], 0);
908                 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
909                 dup2(chkpwd_read_pipe[1], 1);
910                 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
911                 for (i=2; i<256; ++i) close(i);
912                 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
913                 execl(file_chkpwd, file_chkpwd, NULL);
914                 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
915                 abort();
916                 exit(errno);
917         }
918 }
919
920
921 int CtdlTryPassword(const char *password, long len)
922 {
923         int code;
924         CitContext *CCC = CC;
925
926         if ((CCC->logged_in)) {
927                 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
928                 return pass_already_logged_in;
929         }
930         if (!strcmp(CCC->curr_user, NLI)) {
931                 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
932                 return pass_no_user;
933         }
934         if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
935                 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
936                 return pass_internal_error;
937         }
938         if (password == NULL) {
939                 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
940                 return pass_wrong_password;
941         }
942
943         if (CCC->is_master) {
944                 code = strcmp(password, config.c_master_pass);
945         }
946
947         else if (config.c_auth_mode == AUTHMODE_HOST) {
948
949                 /* host auth mode */
950
951                 if (validpw(CCC->user.uid, password)) {
952                         code = 0;
953
954                         /*
955                          * sooper-seekrit hack: populate the password field in the
956                          * citadel database with the password that the user typed,
957                          * if it's correct.  This allows most sites to convert from
958                          * host auth to native auth if they want to.  If you think
959                          * this is a security hazard, comment it out.
960                          */
961
962                         CtdlGetUserLock(&CCC->user, CCC->curr_user);
963                         safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
964                         CtdlPutUserLock(&CCC->user);
965
966                         /*
967                          * (sooper-seekrit hack ends here)
968                          */
969
970                 }
971                 else {
972                         code = (-1);
973                 }
974         }
975
976 #ifdef HAVE_LDAP
977         else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
978
979                 /* LDAP auth mode */
980
981                 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
982                         code = 0;
983                 }
984                 else {
985                         code = (-1);
986                 }
987         }
988 #endif
989
990         else {
991
992                 /* native auth mode */
993                 char *pw;
994
995                 pw = (char*) malloc(len + 1);
996                 memcpy(pw, password, len + 1);
997                 strproc(pw);
998                 strproc(CCC->user.password);
999                 code = strcasecmp(CCC->user.password, pw);
1000                 if (code != 0) {
1001                         strproc(pw);
1002                         strproc(CCC->user.password);
1003                         code = strcasecmp(CCC->user.password, pw);
1004                 }
1005                 free (pw);
1006         }
1007
1008         if (!code) {
1009                 do_login();
1010                 return pass_ok;
1011         } else {
1012                 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
1013                            CCC->curr_user,
1014                            CCC->ServiceName,
1015                            CCC->tcp_port,
1016                            CCC->cs_host,
1017                            CCC->cs_addr);
1018
1019
1020 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
1021
1022                 return pass_wrong_password;
1023         }
1024 }
1025
1026
1027 void cmd_pass(char *buf)
1028 {
1029         char password[SIZ];
1030         int a;
1031         long len;
1032
1033         memset(password, 0, sizeof(password));
1034         len = extract_token(password, buf, 0, '|', sizeof password);
1035         a = CtdlTryPassword(password, len);
1036
1037         switch (a) {
1038         case pass_already_logged_in:
1039                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1040                 return;
1041         case pass_no_user:
1042                 cprintf("%d You must send a name with USER first.\n",
1043                         ERROR + USERNAME_REQUIRED);
1044                 return;
1045         case pass_wrong_password:
1046                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1047                 return;
1048         case pass_ok:
1049                 logged_in_response();
1050                 return;
1051         }
1052 }
1053
1054
1055
1056 /*
1057  * Delete a user record *and* all of its related resources.
1058  */
1059 int purge_user(char pname[])
1060 {
1061         char filename[64];
1062         struct ctdluser usbuf;
1063         char usernamekey[USERNAME_SIZE];
1064
1065         makeuserkey(usernamekey, pname, cutuserkey(pname));
1066
1067         /* If the name is empty we can't find them in the DB any way so just return */
1068         if (IsEmptyStr(pname))
1069                 return (ERROR + NO_SUCH_USER);
1070
1071         if (CtdlGetUser(&usbuf, pname) != 0) {
1072                 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1073                 return (ERROR + NO_SUCH_USER);
1074         }
1075         /* Don't delete a user who is currently logged in.  Instead, just
1076          * set the access level to 0, and let the account get swept up
1077          * during the next purge.
1078          */
1079         if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1080                 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1081                 usbuf.axlevel = AxDeleted;
1082                 CtdlPutUser(&usbuf);
1083                 return (1);
1084         }
1085         CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1086
1087 /*
1088  * FIXME:
1089  * This should all be wrapped in a S_USERS mutex.
1090  * Without the mutex the user could log in before we get to the next function
1091  * That would truly mess things up :-(
1092  * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1093  * and end after the user has been deleted from the database, below.
1094  * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1095  */
1096
1097         /* Perform any purge functions registered by server extensions */
1098         PerformUserHooks(&usbuf, EVT_PURGEUSER);
1099
1100         /* delete any existing user/room relationships */
1101         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1102
1103         /* delete the users-by-number index record */
1104         cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1105
1106         /* delete the userlog entry */
1107         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1108
1109         /* remove the user's bio file */
1110         snprintf(filename, 
1111                          sizeof filename, 
1112                          "%s/%ld",
1113                          ctdl_bio_dir,
1114                          usbuf.usernum);
1115         unlink(filename);
1116
1117         /* remove the user's picture */
1118         snprintf(filename, 
1119                          sizeof filename, 
1120                          "%s/%ld.gif",
1121                          ctdl_image_dir,
1122                          usbuf.usernum);
1123         unlink(filename);
1124
1125         return (0);
1126 }
1127
1128
1129 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1130 {
1131         if (!CtdlGetUserLen(usbuf, username, len)) {
1132                 return (ERROR + ALREADY_EXISTS);
1133         }
1134
1135         /* Go ahead and initialize a new user record */
1136         memset(usbuf, 0, sizeof(struct ctdluser));
1137         safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1138         strcpy(usbuf->password, "");
1139         usbuf->uid = uid;
1140
1141         /* These are the default flags on new accounts */
1142         usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1143
1144         usbuf->timescalled = 0;
1145         usbuf->posted = 0;
1146         usbuf->axlevel = config.c_initax;
1147         usbuf->lastcall = time(NULL);
1148
1149         /* fetch a new user number */
1150         usbuf->usernum = get_new_user_number();
1151
1152         /* add user to the database */
1153         CtdlPutUser(usbuf);
1154         cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1155
1156         return 0;
1157 }
1158
1159
1160
1161 /*
1162  * create_user()  -  back end processing to create a new user
1163  *
1164  * Set 'newusername' to the desired account name.
1165  * Set 'become_user' to nonzero if this is self-service account creation and we want
1166  * to actually log in as the user we just created, otherwise set it to 0.
1167  */
1168 int create_user(const char *newusername, long len, int become_user)
1169 {
1170         struct ctdluser usbuf;
1171         struct ctdlroom qrbuf;
1172         char username[256];
1173         char mailboxname[ROOMNAMELEN];
1174         char buf[SIZ];
1175         int retval;
1176         uid_t uid = (-1);
1177         
1178
1179         safestrncpy(username, newusername, sizeof username);
1180         strproc(username);
1181
1182         
1183         if (config.c_auth_mode == AUTHMODE_HOST) {
1184
1185                 /* host auth mode */
1186
1187                 struct passwd pd;
1188                 struct passwd *tempPwdPtr;
1189                 char pwdbuffer[SIZ];
1190         
1191 #ifdef HAVE_GETPWNAM_R
1192 #ifdef SOLARIS_GETPWUID
1193                 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1194 #else // SOLARIS_GETPWUID
1195                 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1196 #endif // SOLARIS_GETPWUID
1197 #else // HAVE_GETPWNAM_R
1198                 tempPwdPtr = NULL;
1199 #endif // HAVE_GETPWNAM_R
1200                 if (tempPwdPtr != NULL) {
1201                         extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1202                         uid = pd.pw_uid;
1203                         if (IsEmptyStr (username))
1204                         {
1205                                 safestrncpy(username, pd.pw_name, sizeof username);
1206                                 len = cutuserkey(username);
1207                         }
1208                 }
1209                 else {
1210                         return (ERROR + NO_SUCH_USER);
1211                 }
1212         }
1213
1214 #ifdef HAVE_LDAP
1215         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1216                 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1217                         return(ERROR + NO_SUCH_USER);
1218                 }
1219         }
1220 #endif /* HAVE_LDAP */
1221         
1222         if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1223                 return retval;
1224         
1225         /*
1226          * Give the user a private mailbox and a configuration room.
1227          * Make the latter an invisible system room.
1228          */
1229         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1230         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1231
1232         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1233         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1234         if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1235                 qrbuf.QRflags2 |= QR2_SYSTEM;
1236                 CtdlPutRoomLock(&qrbuf);
1237         }
1238
1239         /* Perform any create functions registered by server extensions */
1240         PerformUserHooks(&usbuf, EVT_NEWUSER);
1241
1242         /* Everything below this line can be bypassed if administratively
1243          * creating a user, instead of doing self-service account creation
1244          */
1245
1246         if (become_user) {
1247                 /* Now become the user we just created */
1248                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1249                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1250                 do_login();
1251         
1252                 /* Check to make sure we're still who we think we are */
1253                 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1254                         return (ERROR + INTERNAL_ERROR);
1255                 }
1256         }
1257         
1258         snprintf(buf, SIZ, 
1259                 "New user account <%s> has been created, from host %s [%s].\n",
1260                 username,
1261                 CC->cs_host,
1262                 CC->cs_addr
1263         );
1264         CtdlAideMessage(buf, "User Creation Notice");
1265         CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1266         return (0);
1267 }
1268
1269
1270
1271 /*
1272  * cmd_newu()  -  create a new user account and log in as that user
1273  */
1274 void cmd_newu(char *cmdbuf)
1275 {
1276         int a;
1277         long len;
1278         char username[SIZ];
1279
1280         if (config.c_auth_mode != AUTHMODE_NATIVE) {
1281                 cprintf("%d This system does not use native mode authentication.\n",
1282                         ERROR + NOT_HERE);
1283                 return;
1284         }
1285
1286         if (config.c_disable_newu) {
1287                 cprintf("%d Self-service user account creation "
1288                         "is disabled on this system.\n", ERROR + NOT_HERE);
1289                 return;
1290         }
1291
1292         if (CC->logged_in) {
1293                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1294                 return;
1295         }
1296         if (CC->nologin) {
1297                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1298                         ERROR + MAX_SESSIONS_EXCEEDED,
1299                         config.c_nodename, config.c_maxsessions);
1300         }
1301         extract_token(username, cmdbuf, 0, '|', sizeof username);
1302         strproc(username);
1303         len = cutuserkey(username);
1304
1305         if (IsEmptyStr(username)) {
1306                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1307                 return;
1308         }
1309
1310         if ((!strcasecmp(username, "bbs")) ||
1311             (!strcasecmp(username, "new")) ||
1312             (!strcasecmp(username, "."))) {
1313                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1314                 return;
1315         }
1316
1317         a = create_user(username, len, 1);
1318
1319         if (a == 0) {
1320                 logged_in_response();
1321         } else if (a == ERROR + ALREADY_EXISTS) {
1322                 cprintf("%d '%s' already exists.\n",
1323                         ERROR + ALREADY_EXISTS, username);
1324                 return;
1325         } else if (a == ERROR + INTERNAL_ERROR) {
1326                 cprintf("%d Internal error - user record disappeared?\n",
1327                         ERROR + INTERNAL_ERROR);
1328                 return;
1329         } else {
1330                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1331         }
1332 }
1333
1334
1335 /*
1336  * set password - back end api code
1337  */
1338 void CtdlSetPassword(char *new_pw)
1339 {
1340         CtdlGetUserLock(&CC->user, CC->curr_user);
1341         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1342         CtdlPutUserLock(&CC->user);
1343         CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1344         PerformSessionHooks(EVT_SETPASS);
1345 }
1346
1347
1348 /*
1349  * set password - citadel protocol implementation
1350  */
1351 void cmd_setp(char *new_pw)
1352 {
1353         if (CtdlAccessCheck(ac_logged_in)) {
1354                 return;
1355         }
1356         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1357                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
1358                 return;
1359         }
1360         if (CC->is_master) {
1361                 cprintf("%d The master prefix password cannot be changed with this command.\n",
1362                         ERROR + NOT_HERE);
1363                 return;
1364         }
1365
1366         if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1367                 char random_password[17];
1368                 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1369                 CtdlSetPassword(random_password);
1370                 cprintf("%d %s\n", CIT_OK, random_password);
1371         }
1372         else {
1373                 strproc(new_pw);
1374                 if (IsEmptyStr(new_pw)) {
1375                         cprintf("%d Password unchanged.\n", CIT_OK);
1376                         return;
1377                 }
1378                 CtdlSetPassword(new_pw);
1379                 cprintf("%d Password changed.\n", CIT_OK);
1380         }
1381 }
1382
1383
1384 /*
1385  * cmd_creu() - administratively create a new user account (do not log in to it)
1386  */
1387 void cmd_creu(char *cmdbuf)
1388 {
1389         int a;
1390         long len;
1391         char username[SIZ];
1392         char password[SIZ];
1393         struct ctdluser tmp;
1394
1395         if (CtdlAccessCheck(ac_aide)) {
1396                 return;
1397         }
1398
1399         extract_token(username, cmdbuf, 0, '|', sizeof username);
1400         strproc(username);
1401         strproc(password);
1402         if (IsEmptyStr(username)) {
1403                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1404                 return;
1405         }
1406         len = cutuserkey(username);
1407
1408
1409         extract_token(password, cmdbuf, 1, '|', sizeof password);
1410
1411         a = create_user(username, len, 0);
1412
1413         if (a == 0) {
1414                 if (!IsEmptyStr(password)) {
1415                         CtdlGetUserLock(&tmp, username);
1416                         safestrncpy(tmp.password, password, sizeof(tmp.password));
1417                         CtdlPutUserLock(&tmp);
1418                 }
1419                 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1420                                 (!IsEmptyStr(password)) ? "and password set" :
1421                                 "with no password");
1422                 return;
1423         } else if (a == ERROR + ALREADY_EXISTS) {
1424                 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1425                 return;
1426         } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1427                 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1428                         ERROR + NO_SUCH_USER);
1429                 return;
1430         } else {
1431                 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1432         }
1433 }
1434
1435
1436
1437 /*
1438  * get user parameters
1439  */
1440 void cmd_getu(char *cmdbuf)
1441 {
1442
1443         if (CtdlAccessCheck(ac_logged_in))
1444                 return;
1445
1446         CtdlGetUser(&CC->user, CC->curr_user);
1447         cprintf("%d 80|24|%d|\n",
1448                 CIT_OK,
1449                 (CC->user.flags & US_USER_SET)
1450         );
1451 }
1452
1453 /*
1454  * set user parameters
1455  */
1456 void cmd_setu(char *new_parms)
1457 {
1458         if (CtdlAccessCheck(ac_logged_in))
1459                 return;
1460
1461         if (num_parms(new_parms) < 3) {
1462                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1463                 return;
1464         }
1465         CtdlGetUserLock(&CC->user, CC->curr_user);
1466         CC->user.flags = CC->user.flags & (~US_USER_SET);
1467         CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
1468         CtdlPutUserLock(&CC->user);
1469         cprintf("%d Ok\n", CIT_OK);
1470 }
1471
1472 /*
1473  * set last read pointer
1474  */
1475 void cmd_slrp(char *new_ptr)
1476 {
1477         long newlr;
1478         visit vbuf;
1479         visit original_vbuf;
1480
1481         if (CtdlAccessCheck(ac_logged_in)) {
1482                 return;
1483         }
1484
1485         if (!strncasecmp(new_ptr, "highest", 7)) {
1486                 newlr = CC->room.QRhighest;
1487         } else {
1488                 newlr = atol(new_ptr);
1489         }
1490
1491         CtdlGetUserLock(&CC->user, CC->curr_user);
1492
1493         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1494         memcpy(&original_vbuf, &vbuf, sizeof(visit));
1495         vbuf.v_lastseen = newlr;
1496         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1497
1498         /* Only rewrite the record if it changed */
1499         if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1500            || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1501                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1502         }
1503
1504         CtdlPutUserLock(&CC->user);
1505         cprintf("%d %ld\n", CIT_OK, newlr);
1506 }
1507
1508
1509 void cmd_seen(char *argbuf) {
1510         long target_msgnum = 0L;
1511         int target_setting = 0;
1512
1513         if (CtdlAccessCheck(ac_logged_in)) {
1514                 return;
1515         }
1516
1517         if (num_parms(argbuf) != 2) {
1518                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1519                 return;
1520         }
1521
1522         target_msgnum = extract_long(argbuf, 0);
1523         target_setting = extract_int(argbuf, 1);
1524
1525         CtdlSetSeen(&target_msgnum, 1, target_setting,
1526                         ctdlsetseen_seen, NULL, NULL);
1527         cprintf("%d OK\n", CIT_OK);
1528 }
1529
1530
1531 void cmd_gtsn(char *argbuf) {
1532         visit vbuf;
1533
1534         if (CtdlAccessCheck(ac_logged_in)) {
1535                 return;
1536         }
1537
1538         /* Learn about the user and room in question */
1539         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1540
1541         cprintf("%d ", CIT_OK);
1542         client_write(vbuf.v_seen, strlen(vbuf.v_seen));
1543         client_write(HKEY("\n"));
1544 }
1545
1546
1547 /*
1548  * API function for cmd_invt_kick() and anything else that needs to
1549  * invite or kick out a user to/from a room.
1550  * 
1551  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1552  */
1553 int CtdlInvtKick(char *iuser, int op) {
1554         struct ctdluser USscratch;
1555         visit vbuf;
1556         char bbb[SIZ];
1557
1558         if (CtdlGetUser(&USscratch, iuser) != 0) {
1559                 return(1);
1560         }
1561
1562         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1563         if (op == 1) {
1564                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1565                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1566         }
1567         if (op == 0) {
1568                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1569                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1570         }
1571         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1572
1573         /* post a message in Aide> saying what we just did */
1574         snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1575                 iuser,
1576                 ((op == 1) ? "invited to" : "kicked out of"),
1577                 CC->room.QRname,
1578                 (CC->logged_in ? CC->user.fullname : "an administrator")
1579         );
1580         CtdlAideMessage(bbb,"User Admin Message");
1581
1582         return(0);
1583 }
1584
1585
1586 /*
1587  * INVT and KICK commands
1588  */
1589 void cmd_invt_kick(char *iuser, int op) {
1590
1591         /*
1592          * These commands are only allowed by admins, room admins,
1593          * and room namespace owners
1594          */
1595         if (is_room_aide()) {
1596                 /* access granted */
1597         } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
1598                 /* access granted */
1599         } else {
1600                 /* access denied */
1601                 cprintf("%d Higher access or room ownership required.\n",
1602                         ERROR + HIGHER_ACCESS_REQUIRED);
1603                 return;
1604         }
1605
1606         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1607                          ROOMNAMELEN)) {
1608                 cprintf("%d Can't add/remove users from this room.\n",
1609                         ERROR + NOT_HERE);
1610                 return;
1611         }
1612
1613         if (CtdlInvtKick(iuser, op) != 0) {
1614                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1615                 return;
1616         }
1617
1618         cprintf("%d %s %s %s.\n",
1619                 CIT_OK, iuser,
1620                 ((op == 1) ? "invited to" : "kicked out of"),
1621                 CC->room.QRname);
1622         return;
1623 }
1624
1625 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1626 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1627
1628 /*
1629  * Forget (Zap) the current room (API call)
1630  * Returns 0 on success
1631  */
1632 int CtdlForgetThisRoom(void) {
1633         visit vbuf;
1634
1635         /* On some systems, Admins are not allowed to forget rooms */
1636         if (is_aide() && (config.c_aide_zap == 0)
1637            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1638                 return(1);
1639         }
1640
1641         CtdlGetUserLock(&CC->user, CC->curr_user);
1642         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1643
1644         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1645         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1646
1647         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1648         CtdlPutUserLock(&CC->user);
1649
1650         /* Return to the Lobby, so we don't end up in an undefined room */
1651         CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1652         return(0);
1653
1654 }
1655
1656
1657 /*
1658  * forget (Zap) the current room
1659  */
1660 void cmd_forg(char *argbuf)
1661 {
1662
1663         if (CtdlAccessCheck(ac_logged_in)) {
1664                 return;
1665         }
1666
1667         if (CtdlForgetThisRoom() == 0) {
1668                 cprintf("%d Ok\n", CIT_OK);
1669         }
1670         else {
1671                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1672         }
1673 }
1674
1675 /*
1676  * Get Next Unregistered User
1677  */
1678 void cmd_gnur(char *argbuf)
1679 {
1680         struct cdbdata *cdbus;
1681         struct ctdluser usbuf;
1682
1683         if (CtdlAccessCheck(ac_aide)) {
1684                 return;
1685         }
1686
1687         if ((CitControl.MMflags & MM_VALID) == 0) {
1688                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1689                 return;
1690         }
1691
1692         /* There are unvalidated users.  Traverse the user database,
1693          * and return the first user we find that needs validation.
1694          */
1695         cdb_rewind(CDB_USERS);
1696         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1697                 memset(&usbuf, 0, sizeof(struct ctdluser));
1698                 memcpy(&usbuf, cdbus->ptr,
1699                        ((cdbus->len > sizeof(struct ctdluser)) ?
1700                         sizeof(struct ctdluser) : cdbus->len));
1701                 cdb_free(cdbus);
1702                 if ((usbuf.flags & US_NEEDVALID)
1703                     && (usbuf.axlevel > AxDeleted)) {
1704                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1705                         cdb_close_cursor(CDB_USERS);
1706                         return;
1707                 }
1708         }
1709
1710         /* If we get to this point, there are no more unvalidated users.
1711          * Therefore we clear the "users need validation" flag.
1712          */
1713
1714         begin_critical_section(S_CONTROL);
1715         get_control();
1716         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1717         put_control();
1718         end_critical_section(S_CONTROL);
1719         cprintf("%d *** End of registration.\n", CIT_OK);
1720
1721
1722 }
1723
1724
1725 /*
1726  * validate a user
1727  */
1728 void cmd_vali(char *v_args)
1729 {
1730         char user[128];
1731         int newax;
1732         struct ctdluser userbuf;
1733
1734         extract_token(user, v_args, 0, '|', sizeof user);
1735         newax = extract_int(v_args, 1);
1736
1737         if (CtdlAccessCheck(ac_aide) || 
1738             (newax > AxAideU) ||
1739             (newax < AxDeleted)) {
1740                 return;
1741         }
1742
1743         if (CtdlGetUserLock(&userbuf, user) != 0) {
1744                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1745                 return;
1746         }
1747
1748         userbuf.axlevel = newax;
1749         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1750
1751         CtdlPutUserLock(&userbuf);
1752
1753         /* If the access level was set to zero, delete the user */
1754         if (newax == 0) {
1755                 if (purge_user(user) == 0) {
1756                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1757                         return;
1758                 }
1759         }
1760         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1761 }
1762
1763
1764
1765 /* 
1766  *  Traverse the user file...
1767  */
1768 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1769                  void *in_data)
1770 {
1771         struct ctdluser usbuf;
1772         struct cdbdata *cdbus;
1773
1774         cdb_rewind(CDB_USERS);
1775
1776         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1777                 memset(&usbuf, 0, sizeof(struct ctdluser));
1778                 memcpy(&usbuf, cdbus->ptr,
1779                        ((cdbus->len > sizeof(struct ctdluser)) ?
1780                         sizeof(struct ctdluser) : cdbus->len));
1781                 cdb_free(cdbus);
1782                 (*CallBack) (&usbuf, in_data);
1783         }
1784 }
1785
1786
1787 /*
1788  * List one user (this works with cmd_list)
1789  */
1790 void ListThisUser(struct ctdluser *usbuf, void *data)
1791 {
1792         char *searchstring;
1793
1794         searchstring = (char *)data;
1795         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1796                 return;
1797         }
1798
1799         if (usbuf->axlevel > AxDeleted) {
1800                 if ((CC->user.axlevel >= AxAideU)
1801                     || ((usbuf->flags & US_UNLISTED) == 0)
1802                     || ((CC->internal_pgm))) {
1803                         cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1804                                 usbuf->fullname,
1805                                 usbuf->axlevel,
1806                                 usbuf->usernum,
1807                                 (long)usbuf->lastcall,
1808                                 usbuf->timescalled,
1809                                 usbuf->posted);
1810                 }
1811         }
1812 }
1813
1814 /* 
1815  *  List users (searchstring may be empty to list all users)
1816  */
1817 void cmd_list(char *cmdbuf)
1818 {
1819         char searchstring[256];
1820         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1821         striplt(searchstring);
1822         cprintf("%d \n", LISTING_FOLLOWS);
1823         ForEachUser(ListThisUser, (void *)searchstring );
1824         cprintf("000\n");
1825 }
1826
1827
1828
1829
1830 /*
1831  * assorted info we need to check at login
1832  */
1833 void cmd_chek(char *argbuf)
1834 {
1835         int mail = 0;
1836         int regis = 0;
1837         int vali = 0;
1838
1839         if (CtdlAccessCheck(ac_logged_in)) {
1840                 return;
1841         }
1842
1843         CtdlGetUser(&CC->user, CC->curr_user);  /* no lock is needed here */
1844         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1845                 regis = 1;
1846
1847         if (CC->user.axlevel >= AxAideU) {
1848                 get_control();
1849                 if (CitControl.MMflags & MM_VALID)
1850                         vali = 1;
1851         }
1852
1853         /* check for mail */
1854         mail = InitialMailCheck();
1855
1856         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1857 }
1858
1859
1860 /*
1861  * check to see if a user exists
1862  */
1863 void cmd_qusr(char *who)
1864 {
1865         struct ctdluser usbuf;
1866
1867         if (CtdlGetUser(&usbuf, who) == 0) {
1868                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1869         } else {
1870                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1871         }
1872 }
1873
1874
1875 /*
1876  * Administrative Get User Parameters
1877  */
1878 void cmd_agup(char *cmdbuf)
1879 {
1880         struct ctdluser usbuf;
1881         char requested_user[128];
1882
1883         if (CtdlAccessCheck(ac_aide)) {
1884                 return;
1885         }
1886
1887         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1888         if (CtdlGetUser(&usbuf, requested_user) != 0) {
1889                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1890                 return;
1891         }
1892         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1893                 CIT_OK,
1894                 usbuf.fullname,
1895                 usbuf.password,
1896                 usbuf.flags,
1897                 usbuf.timescalled,
1898                 usbuf.posted,
1899                 (int) usbuf.axlevel,
1900                 usbuf.usernum,
1901                 (long)usbuf.lastcall,
1902                 usbuf.USuserpurge);
1903 }
1904
1905
1906
1907 /*
1908  * Administrative Set User Parameters
1909  */
1910 void cmd_asup(char *cmdbuf)
1911 {
1912         struct ctdluser usbuf;
1913         char requested_user[128];
1914         char notify[SIZ];
1915         int np;
1916         int newax;
1917         int deleted = 0;
1918
1919         if (CtdlAccessCheck(ac_aide))
1920                 return;
1921
1922         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1923         if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1924                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1925                 return;
1926         }
1927         np = num_parms(cmdbuf);
1928         if (np > 1)
1929                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1930         if (np > 2)
1931                 usbuf.flags = extract_int(cmdbuf, 2);
1932         if (np > 3)
1933                 usbuf.timescalled = extract_int(cmdbuf, 3);
1934         if (np > 4)
1935                 usbuf.posted = extract_int(cmdbuf, 4);
1936         if (np > 5) {
1937                 newax = extract_int(cmdbuf, 5);
1938                 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
1939                         usbuf.axlevel = newax;
1940                 }
1941         }
1942         if (np > 7) {
1943                 usbuf.lastcall = extract_long(cmdbuf, 7);
1944         }
1945         if (np > 8) {
1946                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1947         }
1948         CtdlPutUserLock(&usbuf);
1949         if (usbuf.axlevel == AxDeleted) {
1950                 if (purge_user(requested_user) == 0) {
1951                         deleted = 1;
1952                 }
1953         }
1954
1955         if (deleted) {
1956                 snprintf(notify, SIZ, 
1957                          "User \"%s\" has been deleted by %s.\n",
1958                          usbuf.fullname,
1959                         (CC->logged_in ? CC->user.fullname : "an administrator")
1960                 );
1961                 CtdlAideMessage(notify, "User Deletion Message");
1962         }
1963
1964         cprintf("%d Ok", CIT_OK);
1965         if (deleted)
1966                 cprintf(" (%s deleted)", requested_user);
1967         cprintf("\n");
1968 }
1969
1970
1971
1972 /*
1973  * Count the number of new mail messages the user has
1974  */
1975 int NewMailCount()
1976 {
1977         int num_newmsgs = 0;
1978
1979         num_newmsgs = CC->newmail;
1980         CC->newmail = 0;
1981
1982         return (num_newmsgs);
1983 }
1984
1985
1986 /*
1987  * Count the number of new mail messages the user has
1988  */
1989 int InitialMailCheck()
1990 {
1991         int num_newmsgs = 0;
1992         int a;
1993         char mailboxname[ROOMNAMELEN];
1994         struct ctdlroom mailbox;
1995         visit vbuf;
1996         struct cdbdata *cdbfr;
1997         long *msglist = NULL;
1998         int num_msgs = 0;
1999
2000         CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
2001         if (CtdlGetRoom(&mailbox, mailboxname) != 0)
2002                 return (0);
2003         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
2004
2005         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
2006
2007         if (cdbfr != NULL) {
2008                 msglist = malloc(cdbfr->len);
2009                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
2010                 num_msgs = cdbfr->len / sizeof(long);
2011                 cdb_free(cdbfr);
2012         }
2013         if (num_msgs > 0)
2014                 for (a = 0; a < num_msgs; ++a) {
2015                         if (msglist[a] > 0L) {
2016                                 if (msglist[a] > vbuf.v_lastseen) {
2017                                         ++num_newmsgs;
2018                                 }
2019                         }
2020                 }
2021         if (msglist != NULL)
2022                 free(msglist);
2023
2024         return (num_newmsgs);
2025 }
2026
2027
2028
2029 /*
2030  * Set the preferred view for the current user/room combination
2031  */
2032 void cmd_view(char *cmdbuf) {
2033         int requested_view;
2034         visit vbuf;
2035
2036         if (CtdlAccessCheck(ac_logged_in)) {
2037                 return;
2038         }
2039
2040         requested_view = extract_int(cmdbuf, 0);
2041
2042         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2043         vbuf.v_view = requested_view;
2044         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2045         
2046         cprintf("%d ok\n", CIT_OK);
2047 }
2048
2049
2050 /*
2051  * Rename a user
2052  */
2053 void cmd_renu(char *cmdbuf)
2054 {
2055         int retcode;
2056         char oldname[USERNAME_SIZE];
2057         char newname[USERNAME_SIZE];
2058
2059         if (CtdlAccessCheck(ac_aide)) {
2060                 return;
2061         }
2062
2063         extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2064         extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2065
2066         retcode = rename_user(oldname, newname);
2067         switch(retcode) {
2068                 case RENAMEUSER_OK:
2069                         cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2070                         return;
2071                 case RENAMEUSER_LOGGED_IN:
2072                         cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2073                                 ERROR + ALREADY_LOGGED_IN , oldname);
2074                         return;
2075                 case RENAMEUSER_NOT_FOUND:
2076                         cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2077                         return;
2078                 case RENAMEUSER_ALREADY_EXISTS:
2079                         cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2080                         return;
2081         }
2082
2083         cprintf("%d An unknown error occurred.\n", ERROR);
2084 }
2085
2086
2087
2088 /*****************************************************************************/
2089 /*                      MODULE INITIALIZATION STUFF                          */
2090 /*****************************************************************************/
2091
2092
2093 CTDL_MODULE_INIT(user_ops)
2094 {
2095         if (!threading) {
2096                 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
2097                 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
2098                 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
2099                 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
2100                 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
2101                 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
2102                 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
2103                 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
2104                 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
2105                 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
2106                 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
2107                 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
2108                 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
2109                 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
2110                 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
2111                 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
2112                 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
2113                 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
2114                 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
2115                 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
2116                 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
2117                 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
2118         }
2119         /* return our Subversion id for the Log */
2120         return "user_ops";
2121 }