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