Add some comments to rename_user indicating why the code is bad.
[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         CitContext *CCC = CC;   /* CachedCitContext - performance boost */
786         /*
787          * If there is a download in progress, abort it.
788          */
789         if (CCC->download_fp != NULL) {
790                 fclose(CCC->download_fp);
791                 CCC->download_fp = NULL;
792         }
793
794         /*
795          * If there is an upload in progress, abort it.
796          */
797         if (CCC->upload_fp != NULL) {
798                 abort_upl(CCC);
799         }
800
801         /*
802          * If we were talking to a network node, we're not anymore...
803          */
804         if (!IsEmptyStr(CCC->net_node)) {
805                 network_talking_to(CCC->net_node, NTT_REMOVE);
806         }
807
808         /* Run any hooks registered by modules... */
809         PerformSessionHooks(EVT_LOGOUT);
810         
811         /*
812          * Clear out some session data.  Most likely, the CitContext for this
813          * session is about to get nuked when the session disconnects, but
814          * since it's possible to log in again without reconnecting, we cannot
815          * make that assumption.
816          */
817         strcpy(CCC->fake_username, "");
818         strcpy(CCC->fake_hostname, "");
819         strcpy(CCC->fake_roomname, "");
820         CCC->logged_in = 0;
821
822         /* Check to see if the user was deleted whilst logged in and purge them if necessary */
823         if ((CCC->user.axlevel == 0) && (CCC->user.usernum))
824                 purge_user(CCC->user.fullname);
825
826         /* Free any output buffers */
827         unbuffer_output();
828 }
829
830 /*
831  * Validate a password on the host unix system by talking to the chkpwd daemon
832  */
833 static int validpw(uid_t uid, const char *pass)
834 {
835         char buf[256];
836         int rv = 0;
837
838         if (IsEmptyStr(pass)) {
839                 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
840                 return 0;
841         }
842
843         CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
844
845         begin_critical_section(S_CHKPWD);
846         rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
847         rv = write(chkpwd_write_pipe[1], pass, 256);
848         rv = read(chkpwd_read_pipe[0], buf, 4);
849         end_critical_section(S_CHKPWD);
850
851         if (!strncmp(buf, "PASS", 4)) {
852                 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
853                 return(1);
854         }
855
856         CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
857         return 0;
858 }
859
860 /* 
861  * Start up the chkpwd daemon so validpw() has something to talk to
862  */
863 void start_chkpwd_daemon(void) {
864         pid_t chkpwd_pid;
865         struct stat filestats;
866         int i;
867
868         CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
869
870         if ((stat(file_chkpwd, &filestats)==-1) ||
871             (filestats.st_size==0)){
872                 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
873                 abort();
874         }
875         if (pipe(chkpwd_write_pipe) != 0) {
876                 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
877                 abort();
878         }
879         if (pipe(chkpwd_read_pipe) != 0) {
880                 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
881                 abort();
882         }
883
884         chkpwd_pid = fork();
885         if (chkpwd_pid < 0) {
886                 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
887                 abort();
888         }
889         if (chkpwd_pid == 0) {
890                 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
891                 dup2(chkpwd_write_pipe[0], 0);
892                 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
893                 dup2(chkpwd_read_pipe[1], 1);
894                 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
895                 for (i=2; i<256; ++i) close(i);
896                 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
897                 execl(file_chkpwd, file_chkpwd, NULL);
898                 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
899                 abort();
900                 exit(errno);
901         }
902 }
903
904
905 int CtdlTryPassword(char *password)
906 {
907         int code;
908
909         if ((CC->logged_in)) {
910                 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
911                 return pass_already_logged_in;
912         }
913         if (!strcmp(CC->curr_user, NLI)) {
914                 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
915                 return pass_no_user;
916         }
917         if (CtdlGetUser(&CC->user, CC->curr_user)) {
918                 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
919                 return pass_internal_error;
920         }
921         if (password == NULL) {
922                 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
923                 return pass_wrong_password;
924         }
925         code = (-1);
926
927         if (CC->is_master) {
928                 code = strcmp(password, config.c_master_pass);
929         }
930
931         else if (config.c_auth_mode == AUTHMODE_HOST) {
932
933                 /* host auth mode */
934
935                 if (validpw(CC->user.uid, password)) {
936                         code = 0;
937
938                         /*
939                          * sooper-seekrit hack: populate the password field in the
940                          * citadel database with the password that the user typed,
941                          * if it's correct.  This allows most sites to convert from
942                          * host auth to native auth if they want to.  If you think
943                          * this is a security hazard, comment it out.
944                          */
945
946                         CtdlGetUserLock(&CC->user, CC->curr_user);
947                         safestrncpy(CC->user.password, password, sizeof CC->user.password);
948                         CtdlPutUserLock(&CC->user);
949
950                         /*
951                          * (sooper-seekrit hack ends here)
952                          */
953
954                 }
955                 else {
956                         code = (-1);
957                 }
958         }
959
960 #ifdef HAVE_LDAP
961         else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
962
963                 /* LDAP auth mode */
964
965                 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
966                         code = 0;
967                 }
968                 else {
969                         code = (-1);
970                 }
971         }
972 #endif
973
974         else {
975
976                 /* native auth mode */
977
978                 strproc(password);
979                 strproc(CC->user.password);
980                 code = strcasecmp(CC->user.password, password);
981                 strproc(password);
982                 strproc(CC->user.password);
983                 code = strcasecmp(CC->user.password, password);
984         }
985
986         if (!code) {
987                 do_login();
988                 return pass_ok;
989         } else {
990                 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
991                 return pass_wrong_password;
992         }
993 }
994
995
996 void cmd_pass(char *buf)
997 {
998         char password[256];
999         int a;
1000
1001         memset(password, 0, sizeof(password));
1002         extract_token(password, buf, 0, '|', sizeof password);
1003         a = CtdlTryPassword(password);
1004
1005         switch (a) {
1006         case pass_already_logged_in:
1007                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1008                 return;
1009         case pass_no_user:
1010                 cprintf("%d You must send a name with USER first.\n",
1011                         ERROR + USERNAME_REQUIRED);
1012                 return;
1013         case pass_wrong_password:
1014                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1015                 return;
1016         case pass_ok:
1017                 logged_in_response();
1018                 return;
1019         }
1020 }
1021
1022
1023
1024 /*
1025  * Delete a user record *and* all of its related resources.
1026  */
1027 int purge_user(char pname[])
1028 {
1029         char filename[64];
1030         struct ctdluser usbuf;
1031         char usernamekey[USERNAME_SIZE];
1032         CitContext *ccptr;
1033         int user_is_logged_in = 0;
1034
1035         makeuserkey(usernamekey, pname);
1036
1037         /* If the name is empty we can't find them in the DB any way so just return */
1038         if (IsEmptyStr(pname))
1039                 return (ERROR + NO_SUCH_USER);
1040
1041         if (CtdlGetUser(&usbuf, pname) != 0) {
1042                 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
1043                 return (ERROR + NO_SUCH_USER);
1044         }
1045         /* Don't delete a user who is currently logged in.  Instead, just
1046          * set the access level to 0, and let the account get swept up
1047          * during the next purge.
1048          */
1049         user_is_logged_in = 0;
1050         begin_critical_section(S_SESSION_TABLE);
1051         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
1052                 if (ccptr->user.usernum == usbuf.usernum) {
1053                         user_is_logged_in = 1;
1054                 }
1055         }
1056         end_critical_section(S_SESSION_TABLE);
1057         if (user_is_logged_in == 1) {
1058                 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1059                 usbuf.axlevel = 0;
1060                 CtdlPutUser(&usbuf);
1061                 return (1);
1062         }
1063         CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
1064
1065         /* Perform any purge functions registered by server extensions */
1066         PerformUserHooks(&usbuf, EVT_PURGEUSER);
1067
1068         /* delete any existing user/room relationships */
1069         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1070
1071         /* delete the users-by-number index record */
1072         cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1073
1074         /* delete the userlog entry */
1075         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1076
1077         /* remove the user's bio file */
1078         snprintf(filename, 
1079                          sizeof filename, 
1080                          "%s/%ld",
1081                          ctdl_bio_dir,
1082                          usbuf.usernum);
1083         unlink(filename);
1084
1085         /* remove the user's picture */
1086         snprintf(filename, 
1087                          sizeof filename, 
1088                          "%s/%ld.gif",
1089                          ctdl_image_dir,
1090                          usbuf.usernum);
1091         unlink(filename);
1092
1093         return (0);
1094 }
1095
1096
1097 int internal_create_user (char *username, struct ctdluser *usbuf, uid_t uid)
1098 {
1099         if (!CtdlGetUser(usbuf, username)) {
1100                 return (ERROR + ALREADY_EXISTS);
1101         }
1102
1103         /* Go ahead and initialize a new user record */
1104         memset(usbuf, 0, sizeof(struct ctdluser));
1105         safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1106         strcpy(usbuf->password, "");
1107         usbuf->uid = uid;
1108
1109         /* These are the default flags on new accounts */
1110         usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1111
1112         usbuf->timescalled = 0;
1113         usbuf->posted = 0;
1114         usbuf->axlevel = config.c_initax;
1115         usbuf->USscreenwidth = 80;
1116         usbuf->USscreenheight = 24;
1117         usbuf->lastcall = time(NULL);
1118
1119         /* fetch a new user number */
1120         usbuf->usernum = get_new_user_number();
1121
1122         /* add user to the database */
1123         CtdlPutUser(usbuf);
1124         cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1125
1126         return 0;
1127 }
1128
1129
1130
1131 /*
1132  * create_user()  -  back end processing to create a new user
1133  *
1134  * Set 'newusername' to the desired account name.
1135  * Set 'become_user' to nonzero if this is self-service account creation and we want
1136  * to actually log in as the user we just created, otherwise set it to 0.
1137  */
1138 int create_user(char *newusername, int become_user)
1139 {
1140         struct ctdluser usbuf;
1141         struct ctdlroom qrbuf;
1142         char username[256];
1143         char mailboxname[ROOMNAMELEN];
1144         char buf[SIZ];
1145         int retval;
1146         uid_t uid = (-1);
1147         
1148
1149         safestrncpy(username, newusername, sizeof username);
1150         strproc(username);
1151
1152         
1153         if (config.c_auth_mode == AUTHMODE_HOST) {
1154
1155                 /* host auth mode */
1156
1157                 struct passwd pd;
1158                 struct passwd *tempPwdPtr;
1159                 char pwdbuffer[256];
1160         
1161 #ifdef HAVE_GETPWNAM_R
1162 #ifdef SOLARIS_GETPWUID
1163                 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1164 #else // SOLARIS_GETPWUID
1165                 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1166 #endif // SOLARIS_GETPWUID
1167 #else // HAVE_GETPWNAM_R
1168                 tempPwdPtr = NULL;
1169 #endif // HAVE_GETPWNAM_R
1170                 if (tempPwdPtr != NULL) {
1171                         extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1172                         uid = pd.pw_uid;
1173                         if (IsEmptyStr (username))
1174                         {
1175                                 safestrncpy(username, pd.pw_name, sizeof username);
1176                         }
1177                 }
1178                 else {
1179                         return (ERROR + NO_SUCH_USER);
1180                 }
1181         }
1182
1183 #ifdef HAVE_LDAP
1184         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1185                 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1186                         return(ERROR + NO_SUCH_USER);
1187                 }
1188         }
1189 #endif /* HAVE_LDAP */
1190         
1191         if ((retval = internal_create_user(username, &usbuf, uid)) != 0)
1192                 return retval;
1193         
1194         /*
1195          * Give the user a private mailbox and a configuration room.
1196          * Make the latter an invisible system room.
1197          */
1198         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1199         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1200
1201         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1202         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1203         if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1204                 qrbuf.QRflags2 |= QR2_SYSTEM;
1205                 CtdlPutRoomLock(&qrbuf);
1206         }
1207
1208         /* Perform any create functions registered by server extensions */
1209         PerformUserHooks(&usbuf, EVT_NEWUSER);
1210
1211         /* Everything below this line can be bypassed if administratively
1212          * creating a user, instead of doing self-service account creation
1213          */
1214
1215         if (become_user) {
1216                 /* Now become the user we just created */
1217                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1218                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1219                 do_login();
1220         
1221                 /* Check to make sure we're still who we think we are */
1222                 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1223                         return (ERROR + INTERNAL_ERROR);
1224                 }
1225         }
1226         
1227         snprintf(buf, SIZ, 
1228                 "New user account <%s> has been created, from host %s [%s].\n",
1229                 username,
1230                 CC->cs_host,
1231                 CC->cs_addr
1232         );
1233         CtdlAideMessage(buf, "User Creation Notice");
1234         CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1235         return (0);
1236 }
1237
1238
1239
1240 /*
1241  * cmd_newu()  -  create a new user account and log in as that user
1242  */
1243 void cmd_newu(char *cmdbuf)
1244 {
1245         int a;
1246         char username[26];
1247
1248         if (config.c_auth_mode != AUTHMODE_NATIVE) {
1249                 cprintf("%d This system does not use native mode authentication.\n",
1250                         ERROR + NOT_HERE);
1251                 return;
1252         }
1253
1254         if (config.c_disable_newu) {
1255                 cprintf("%d Self-service user account creation "
1256                         "is disabled on this system.\n", ERROR + NOT_HERE);
1257                 return;
1258         }
1259
1260         if (CC->logged_in) {
1261                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1262                 return;
1263         }
1264         if (CC->nologin) {
1265                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1266                         ERROR + MAX_SESSIONS_EXCEEDED,
1267                         config.c_nodename, config.c_maxsessions);
1268         }
1269         extract_token(username, cmdbuf, 0, '|', sizeof username);
1270         username[25] = 0;
1271         strproc(username);
1272
1273         if (IsEmptyStr(username)) {
1274                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1275                 return;
1276         }
1277
1278         if ((!strcasecmp(username, "bbs")) ||
1279             (!strcasecmp(username, "new")) ||
1280             (!strcasecmp(username, "."))) {
1281                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1282                 return;
1283         }
1284
1285         a = create_user(username, 1);
1286
1287         if (a == 0) {
1288                 logged_in_response();
1289         } else if (a == ERROR + ALREADY_EXISTS) {
1290                 cprintf("%d '%s' already exists.\n",
1291                         ERROR + ALREADY_EXISTS, username);
1292                 return;
1293         } else if (a == ERROR + INTERNAL_ERROR) {
1294                 cprintf("%d Internal error - user record disappeared?\n",
1295                         ERROR + INTERNAL_ERROR);
1296                 return;
1297         } else {
1298                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1299         }
1300 }
1301
1302
1303 /*
1304  * set password - back end api code
1305  */
1306 void CtdlSetPassword(char *new_pw)
1307 {
1308         CtdlGetUserLock(&CC->user, CC->curr_user);
1309         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1310         CtdlPutUserLock(&CC->user);
1311         CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1312         PerformSessionHooks(EVT_SETPASS);
1313 }
1314
1315
1316 /*
1317  * set password - citadel protocol implementation
1318  */
1319 void cmd_setp(char *new_pw)
1320 {
1321         int generate_random_password = 0;
1322
1323         if (CtdlAccessCheck(ac_logged_in)) {
1324                 return;
1325         }
1326         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1327                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
1328                 return;
1329         }
1330         if (CC->is_master) {
1331                 cprintf("%d The master prefix password cannot be changed with this command.\n",
1332                         ERROR + NOT_HERE);
1333                 return;
1334         }
1335
1336         if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1337                 char random_password[17];
1338                 generate_random_password = 1;
1339                 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1340                 CtdlSetPassword(random_password);
1341                 cprintf("%d %s\n", CIT_OK, random_password);
1342         }
1343         else {
1344                 strproc(new_pw);
1345                 if (IsEmptyStr(new_pw)) {
1346                         cprintf("%d Password unchanged.\n", CIT_OK);
1347                         return;
1348                 }
1349                 CtdlSetPassword(new_pw);
1350                 cprintf("%d Password changed.\n", CIT_OK);
1351         }
1352 }
1353
1354
1355 /*
1356  * cmd_creu() - administratively create a new user account (do not log in to it)
1357  */
1358 void cmd_creu(char *cmdbuf)
1359 {
1360         int a;
1361         char username[26];
1362         char password[32];
1363         struct ctdluser tmp;
1364
1365         if (CtdlAccessCheck(ac_aide)) {
1366                 return;
1367         }
1368
1369         extract_token(username, cmdbuf, 0, '|', sizeof username);
1370         extract_token(password, cmdbuf, 1, '|', sizeof password);
1371         username[25] = 0;
1372         password[31] = 0;
1373         strproc(username);
1374         strproc(password);
1375
1376         if (IsEmptyStr(username)) {
1377                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1378                 return;
1379         }
1380
1381         a = create_user(username, 0);
1382
1383         if (a == 0) {
1384                 if (!IsEmptyStr(password)) {
1385                         CtdlGetUserLock(&tmp, username);
1386                         safestrncpy(tmp.password, password, sizeof(tmp.password));
1387                         CtdlPutUserLock(&tmp);
1388                 }
1389                 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1390                                 (!IsEmptyStr(password)) ? "and password set" :
1391                                 "with no password");
1392                 return;
1393         } else if (a == ERROR + ALREADY_EXISTS) {
1394                 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1395                 return;
1396         } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1397                 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1398                         ERROR + NO_SUCH_USER);
1399                 return;
1400         } else {
1401                 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1402         }
1403 }
1404
1405
1406
1407 /*
1408  * get user parameters
1409  */
1410 void cmd_getu(char *cmdbuf)
1411 {
1412
1413         if (CtdlAccessCheck(ac_logged_in))
1414                 return;
1415
1416         CtdlGetUser(&CC->user, CC->curr_user);
1417         cprintf("%d %d|%d|%d|\n",
1418                 CIT_OK,
1419                 CC->user.USscreenwidth,
1420                 CC->user.USscreenheight,
1421                 (CC->user.flags & US_USER_SET)
1422             );
1423 }
1424
1425 /*
1426  * set user parameters
1427  */
1428 void cmd_setu(char *new_parms)
1429 {
1430         if (CtdlAccessCheck(ac_logged_in))
1431                 return;
1432
1433         if (num_parms(new_parms) < 3) {
1434                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1435                 return;
1436         }
1437         CtdlGetUserLock(&CC->user, CC->curr_user);
1438         CC->user.USscreenwidth = extract_int(new_parms, 0);
1439         CC->user.USscreenheight = extract_int(new_parms, 1);
1440         CC->user.flags = CC->user.flags & (~US_USER_SET);
1441         CC->user.flags = CC->user.flags |
1442             (extract_int(new_parms, 2) & US_USER_SET);
1443
1444         CtdlPutUserLock(&CC->user);
1445         cprintf("%d Ok\n", CIT_OK);
1446 }
1447
1448 /*
1449  * set last read pointer
1450  */
1451 void cmd_slrp(char *new_ptr)
1452 {
1453         long newlr;
1454         struct visit vbuf;
1455         struct visit original_vbuf;
1456
1457         if (CtdlAccessCheck(ac_logged_in)) {
1458                 return;
1459         }
1460
1461         if (!strncasecmp(new_ptr, "highest", 7)) {
1462                 newlr = CC->room.QRhighest;
1463         } else {
1464                 newlr = atol(new_ptr);
1465         }
1466
1467         CtdlGetUserLock(&CC->user, CC->curr_user);
1468
1469         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1470         memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1471         vbuf.v_lastseen = newlr;
1472         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1473
1474         /* Only rewrite the record if it changed */
1475         if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1476            || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1477                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1478         }
1479
1480         CtdlPutUserLock(&CC->user);
1481         cprintf("%d %ld\n", CIT_OK, newlr);
1482 }
1483
1484
1485 void cmd_seen(char *argbuf) {
1486         long target_msgnum = 0L;
1487         int target_setting = 0;
1488
1489         if (CtdlAccessCheck(ac_logged_in)) {
1490                 return;
1491         }
1492
1493         if (num_parms(argbuf) != 2) {
1494                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1495                 return;
1496         }
1497
1498         target_msgnum = extract_long(argbuf, 0);
1499         target_setting = extract_int(argbuf, 1);
1500
1501         CtdlSetSeen(&target_msgnum, 1, target_setting,
1502                         ctdlsetseen_seen, NULL, NULL);
1503         cprintf("%d OK\n", CIT_OK);
1504 }
1505
1506
1507 void cmd_gtsn(char *argbuf) {
1508         char buf[SIZ];
1509
1510         if (CtdlAccessCheck(ac_logged_in)) {
1511                 return;
1512         }
1513
1514         CtdlGetSeen(buf, ctdlsetseen_seen);
1515         cprintf("%d %s\n", CIT_OK, buf);
1516 }
1517
1518
1519 /*
1520  * API function for cmd_invt_kick() and anything else that needs to
1521  * invite or kick out a user to/from a room.
1522  * 
1523  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1524  */
1525 int CtdlInvtKick(char *iuser, int op) {
1526         struct ctdluser USscratch;
1527         struct visit vbuf;
1528         char bbb[SIZ];
1529
1530         if (CtdlGetUser(&USscratch, iuser) != 0) {
1531                 return(1);
1532         }
1533
1534         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1535         if (op == 1) {
1536                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1537                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1538         }
1539         if (op == 0) {
1540                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1541                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1542         }
1543         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1544
1545         /* post a message in Aide> saying what we just did */
1546         snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1547                 iuser,
1548                 ((op == 1) ? "invited to" : "kicked out of"),
1549                 CC->room.QRname,
1550                 CC->user.fullname);
1551         CtdlAideMessage(bbb,"User Admin Message");
1552
1553         return(0);
1554 }
1555
1556
1557 /*
1558  * INVT and KICK commands
1559  */
1560 void cmd_invt_kick(char *iuser, int op) {
1561
1562         /*
1563          * These commands are only allowed by aides, room aides,
1564          * and room namespace owners
1565          */
1566         if (is_room_aide()
1567            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1568                 /* access granted */
1569         } else {
1570                 /* access denied */
1571                 cprintf("%d Higher access or room ownership required.\n",
1572                         ERROR + HIGHER_ACCESS_REQUIRED);
1573                 return;
1574         }
1575
1576         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1577                          ROOMNAMELEN)) {
1578                 cprintf("%d Can't add/remove users from this room.\n",
1579                         ERROR + NOT_HERE);
1580                 return;
1581         }
1582
1583         if (CtdlInvtKick(iuser, op) != 0) {
1584                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1585                 return;
1586         }
1587
1588         cprintf("%d %s %s %s.\n",
1589                 CIT_OK, iuser,
1590                 ((op == 1) ? "invited to" : "kicked out of"),
1591                 CC->room.QRname);
1592         return;
1593 }
1594
1595 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1596 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1597
1598 /*
1599  * Forget (Zap) the current room (API call)
1600  * Returns 0 on success
1601  */
1602 int CtdlForgetThisRoom(void) {
1603         struct visit vbuf;
1604
1605         /* On some systems, Aides are not allowed to forget rooms */
1606         if (is_aide() && (config.c_aide_zap == 0)
1607            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1608                 return(1);
1609         }
1610
1611         CtdlGetUserLock(&CC->user, CC->curr_user);
1612         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1613
1614         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1615         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1616
1617         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1618         CtdlPutUserLock(&CC->user);
1619
1620         /* Return to the Lobby, so we don't end up in an undefined room */
1621         CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1622         return(0);
1623
1624 }
1625
1626
1627 /*
1628  * forget (Zap) the current room
1629  */
1630 void cmd_forg(char *argbuf)
1631 {
1632
1633         if (CtdlAccessCheck(ac_logged_in)) {
1634                 return;
1635         }
1636
1637         if (CtdlForgetThisRoom() == 0) {
1638                 cprintf("%d Ok\n", CIT_OK);
1639         }
1640         else {
1641                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1642         }
1643 }
1644
1645 /*
1646  * Get Next Unregistered User
1647  */
1648 void cmd_gnur(char *argbuf)
1649 {
1650         struct cdbdata *cdbus;
1651         struct ctdluser usbuf;
1652
1653         if (CtdlAccessCheck(ac_aide)) {
1654                 return;
1655         }
1656
1657         if ((CitControl.MMflags & MM_VALID) == 0) {
1658                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1659                 return;
1660         }
1661
1662         /* There are unvalidated users.  Traverse the user database,
1663          * and return the first user we find that needs validation.
1664          */
1665         cdb_rewind(CDB_USERS);
1666         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1667                 memset(&usbuf, 0, sizeof(struct ctdluser));
1668                 memcpy(&usbuf, cdbus->ptr,
1669                        ((cdbus->len > sizeof(struct ctdluser)) ?
1670                         sizeof(struct ctdluser) : cdbus->len));
1671                 cdb_free(cdbus);
1672                 if ((usbuf.flags & US_NEEDVALID)
1673                     && (usbuf.axlevel > 0)) {
1674                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1675                         cdb_close_cursor(CDB_USERS);
1676                         return;
1677                 }
1678         }
1679
1680         /* If we get to this point, there are no more unvalidated users.
1681          * Therefore we clear the "users need validation" flag.
1682          */
1683
1684         begin_critical_section(S_CONTROL);
1685         get_control();
1686         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1687         put_control();
1688         end_critical_section(S_CONTROL);
1689         cprintf("%d *** End of registration.\n", CIT_OK);
1690
1691
1692 }
1693
1694
1695 /*
1696  * validate a user
1697  */
1698 void cmd_vali(char *v_args)
1699 {
1700         char user[128];
1701         int newax;
1702         struct ctdluser userbuf;
1703
1704         extract_token(user, v_args, 0, '|', sizeof user);
1705         newax = extract_int(v_args, 1);
1706
1707         if (CtdlAccessCheck(ac_aide)) {
1708                 return;
1709         }
1710
1711         if (CtdlGetUserLock(&userbuf, user) != 0) {
1712                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1713                 return;
1714         }
1715
1716         userbuf.axlevel = newax;
1717         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1718
1719         CtdlPutUserLock(&userbuf);
1720
1721         /* If the access level was set to zero, delete the user */
1722         if (newax == 0) {
1723                 if (purge_user(user) == 0) {
1724                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1725                         return;
1726                 }
1727         }
1728         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1729 }
1730
1731
1732
1733 /* 
1734  *  Traverse the user file...
1735  */
1736 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1737                  void *in_data)
1738 {
1739         struct ctdluser usbuf;
1740         struct cdbdata *cdbus;
1741
1742         cdb_rewind(CDB_USERS);
1743
1744         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1745                 memset(&usbuf, 0, sizeof(struct ctdluser));
1746                 memcpy(&usbuf, cdbus->ptr,
1747                        ((cdbus->len > sizeof(struct ctdluser)) ?
1748                         sizeof(struct ctdluser) : cdbus->len));
1749                 cdb_free(cdbus);
1750                 (*CallBack) (&usbuf, in_data);
1751         }
1752 }
1753
1754
1755 /*
1756  * List one user (this works with cmd_list)
1757  */
1758 void ListThisUser(struct ctdluser *usbuf, void *data)
1759 {
1760         char *searchstring;
1761
1762         searchstring = (char *)data;
1763         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1764                 return;
1765         }
1766
1767         if (usbuf->axlevel > 0) {
1768                 if ((CC->user.axlevel >= 6)
1769                     || ((usbuf->flags & US_UNLISTED) == 0)
1770                     || ((CC->internal_pgm))) {
1771                         cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1772                                 usbuf->fullname,
1773                                 usbuf->axlevel,
1774                                 usbuf->usernum,
1775                                 (long)usbuf->lastcall,
1776                                 usbuf->timescalled,
1777                                 usbuf->posted);
1778                 }
1779         }
1780 }
1781
1782 /* 
1783  *  List users (searchstring may be empty to list all users)
1784  */
1785 void cmd_list(char *cmdbuf)
1786 {
1787         char searchstring[256];
1788         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1789         striplt(searchstring);
1790         cprintf("%d \n", LISTING_FOLLOWS);
1791         ForEachUser(ListThisUser, (void *)searchstring );
1792         cprintf("000\n");
1793 }
1794
1795
1796
1797
1798 /*
1799  * assorted info we need to check at login
1800  */
1801 void cmd_chek(char *argbuf)
1802 {
1803         int mail = 0;
1804         int regis = 0;
1805         int vali = 0;
1806
1807         if (CtdlAccessCheck(ac_logged_in)) {
1808                 return;
1809         }
1810
1811         CtdlGetUser(&CC->user, CC->curr_user);  /* no lock is needed here */
1812         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1813                 regis = 1;
1814
1815         if (CC->user.axlevel >= 6) {
1816                 get_control();
1817                 if (CitControl.MMflags & MM_VALID)
1818                         vali = 1;
1819         }
1820
1821         /* check for mail */
1822         mail = InitialMailCheck();
1823
1824         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1825 }
1826
1827
1828 /*
1829  * check to see if a user exists
1830  */
1831 void cmd_qusr(char *who)
1832 {
1833         struct ctdluser usbuf;
1834
1835         if (CtdlGetUser(&usbuf, who) == 0) {
1836                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1837         } else {
1838                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1839         }
1840 }
1841
1842
1843 /*
1844  * Administrative Get User Parameters
1845  */
1846 void cmd_agup(char *cmdbuf)
1847 {
1848         struct ctdluser usbuf;
1849         char requested_user[128];
1850
1851         if (CtdlAccessCheck(ac_aide)) {
1852                 return;
1853         }
1854
1855         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1856         if (CtdlGetUser(&usbuf, requested_user) != 0) {
1857                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1858                 return;
1859         }
1860         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1861                 CIT_OK,
1862                 usbuf.fullname,
1863                 usbuf.password,
1864                 usbuf.flags,
1865                 usbuf.timescalled,
1866                 usbuf.posted,
1867                 (int) usbuf.axlevel,
1868                 usbuf.usernum,
1869                 (long)usbuf.lastcall,
1870                 usbuf.USuserpurge);
1871 }
1872
1873
1874
1875 /*
1876  * Administrative Set User Parameters
1877  */
1878 void cmd_asup(char *cmdbuf)
1879 {
1880         struct ctdluser usbuf;
1881         char requested_user[128];
1882         char notify[SIZ];
1883         int np;
1884         int newax;
1885         int deleted = 0;
1886
1887         if (CtdlAccessCheck(ac_aide))
1888                 return;
1889
1890         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1891         if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1892                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1893                 return;
1894         }
1895         np = num_parms(cmdbuf);
1896         if (np > 1)
1897                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1898         if (np > 2)
1899                 usbuf.flags = extract_int(cmdbuf, 2);
1900         if (np > 3)
1901                 usbuf.timescalled = extract_int(cmdbuf, 3);
1902         if (np > 4)
1903                 usbuf.posted = extract_int(cmdbuf, 4);
1904         if (np > 5) {
1905                 newax = extract_int(cmdbuf, 5);
1906                 if ((newax >= 0) && (newax <= 6)) {
1907                         usbuf.axlevel = extract_int(cmdbuf, 5);
1908                 }
1909         }
1910         if (np > 7) {
1911                 usbuf.lastcall = extract_long(cmdbuf, 7);
1912         }
1913         if (np > 8) {
1914                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1915         }
1916         CtdlPutUserLock(&usbuf);
1917         if (usbuf.axlevel == 0) {
1918                 if (purge_user(requested_user) == 0) {
1919                         deleted = 1;
1920                 }
1921         }
1922
1923         if (deleted) {
1924                 snprintf(notify, SIZ, 
1925                          "User \"%s\" has been deleted by %s.\n",
1926                          usbuf.fullname, CC->user.fullname);
1927                 CtdlAideMessage(notify, "User Deletion Message");
1928         }
1929
1930         cprintf("%d Ok", CIT_OK);
1931         if (deleted)
1932                 cprintf(" (%s deleted)", requested_user);
1933         cprintf("\n");
1934 }
1935
1936
1937
1938 /*
1939  * Check to see if the user who we just sent mail to is logged in.  If yes,
1940  * bump the 'new mail' counter for their session.  That enables them to
1941  * receive a new mail notification without having to hit the database.
1942  */
1943 void BumpNewMailCounter(long which_user) {
1944         CitContext *ptr;
1945
1946         begin_critical_section(S_SESSION_TABLE);
1947
1948         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1949                 if (ptr->user.usernum == which_user) {
1950                         ptr->newmail += 1;
1951                 }
1952         }
1953
1954         end_critical_section(S_SESSION_TABLE);
1955 }
1956
1957
1958 /*
1959  * Count the number of new mail messages the user has
1960  */
1961 int NewMailCount()
1962 {
1963         int num_newmsgs = 0;
1964
1965         num_newmsgs = CC->newmail;
1966         CC->newmail = 0;
1967
1968         return (num_newmsgs);
1969 }
1970
1971
1972 /*
1973  * Count the number of new mail messages the user has
1974  */
1975 int InitialMailCheck()
1976 {
1977         int num_newmsgs = 0;
1978         int a;
1979         char mailboxname[ROOMNAMELEN];
1980         struct ctdlroom mailbox;
1981         struct visit vbuf;
1982         struct cdbdata *cdbfr;
1983         long *msglist = NULL;
1984         int num_msgs = 0;
1985
1986         CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1987         if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1988                 return (0);
1989         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1990
1991         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1992
1993         if (cdbfr != NULL) {
1994                 msglist = malloc(cdbfr->len);
1995                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1996                 num_msgs = cdbfr->len / sizeof(long);
1997                 cdb_free(cdbfr);
1998         }
1999         if (num_msgs > 0)
2000                 for (a = 0; a < num_msgs; ++a) {
2001                         if (msglist[a] > 0L) {
2002                                 if (msglist[a] > vbuf.v_lastseen) {
2003                                         ++num_newmsgs;
2004                                 }
2005                         }
2006                 }
2007         if (msglist != NULL)
2008                 free(msglist);
2009
2010         return (num_newmsgs);
2011 }
2012
2013
2014
2015 /*
2016  * Set the preferred view for the current user/room combination
2017  */
2018 void cmd_view(char *cmdbuf) {
2019         int requested_view;
2020         struct visit vbuf;
2021
2022         if (CtdlAccessCheck(ac_logged_in)) {
2023                 return;
2024         }
2025
2026         requested_view = extract_int(cmdbuf, 0);
2027
2028         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2029         vbuf.v_view = requested_view;
2030         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2031         
2032         cprintf("%d ok\n", CIT_OK);
2033 }
2034
2035
2036 /*
2037  * Rename a user
2038  */
2039 void cmd_renu(char *cmdbuf)
2040 {
2041         int retcode;
2042         char oldname[USERNAME_SIZE];
2043         char newname[USERNAME_SIZE];
2044
2045         if (CtdlAccessCheck(ac_aide)) {
2046                 return;
2047         }
2048
2049         extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2050         extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2051
2052         retcode = rename_user(oldname, newname);
2053         switch(retcode) {
2054                 case RENAMEUSER_OK:
2055                         cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2056                         return;
2057                 case RENAMEUSER_LOGGED_IN:
2058                         cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2059                                 ERROR + ALREADY_LOGGED_IN , oldname);
2060                         return;
2061                 case RENAMEUSER_NOT_FOUND:
2062                         cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2063                         return;
2064                 case RENAMEUSER_ALREADY_EXISTS:
2065                         cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2066                         return;
2067         }
2068
2069         cprintf("%d An unknown error occurred.\n", ERROR);
2070 }
2071
2072
2073
2074 /*****************************************************************************/
2075 /*                      MODULE INITIALIZATION STUFF                          */
2076 /*****************************************************************************/
2077
2078
2079 CTDL_MODULE_INIT(user_ops)
2080 {
2081         if (!threading) {
2082                 CtdlRegisterProtoHook(cmd_user, "USER", "Autoconverted. TODO: document me.");
2083                 CtdlRegisterProtoHook(cmd_pass, "PASS", "Autoconverted. TODO: document me.");
2084                 CtdlRegisterProtoHook(cmd_creu, "CREU", "Autoconverted. TODO: document me.");
2085                 CtdlRegisterProtoHook(cmd_setp, "SETP", "Autoconverted. TODO: document me.");
2086                 CtdlRegisterProtoHook(cmd_getu, "GETU", "Autoconverted. TODO: document me.");
2087                 CtdlRegisterProtoHook(cmd_setu, "SETU", "Autoconverted. TODO: document me.");
2088                 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Autoconverted. TODO: document me.");
2089                 CtdlRegisterProtoHook(cmd_invt, "INVT", "Autoconverted. TODO: document me.");
2090                 CtdlRegisterProtoHook(cmd_kick, "KICK", "Autoconverted. TODO: document me.");
2091                 CtdlRegisterProtoHook(cmd_forg, "FORG", "Autoconverted. TODO: document me.");
2092                 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2093                 CtdlRegisterProtoHook(cmd_vali, "VALI", "Autoconverted. TODO: document me.");
2094                 CtdlRegisterProtoHook(cmd_list, "LIST", "Autoconverted. TODO: document me.");
2095                 CtdlRegisterProtoHook(cmd_chek, "CHEK", "Autoconverted. TODO: document me.");
2096                 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "Autoconverted. TODO: document me.");
2097                 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Autoconverted. TODO: document me.");
2098                 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Autoconverted. TODO: document me.");
2099                 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Autoconverted. TODO: document me.");
2100                 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Autoconverted. TODO: document me.");
2101                 CtdlRegisterProtoHook(cmd_view, "VIEW", "Autoconverted. TODO: document me.");
2102                 CtdlRegisterProtoHook(cmd_renu, "RENU", "Autoconverted. TODO: document me.");
2103                 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Autoconverted. TODO: document me.");
2104         }
2105         /* return our Subversion id for the Log */
2106         return "$Id$";
2107 }