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