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