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