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