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