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