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