user access level helper moved to the other accesslevel helpers: user_ops.c
[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, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <ctype.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <syslog.h>
27 #ifdef HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # if HAVE_SYS_TIME_H
36 #  include <sys/time.h>
37 # else
38 #  include <time.h>
39 # endif
40 #endif
41
42 #include <string.h>
43 #include <limits.h>
44 #include <libcitadel.h>
45 #include "auth.h"
46 #include "citadel.h"
47 #include "server.h"
48 #include "database.h"
49 #include "sysdep_decls.h"
50 #include "support.h"
51 #include "room_ops.h"
52 #include "control.h"
53 #include "msgbase.h"
54 #include "config.h"
55 #include "citserver.h"
56 #include "citadel_dirs.h"
57 #include "genstamp.h"
58 #include "threads.h"
59 #include "citadel_ldap.h"
60 #include "context.h"
61 #include "ctdl_module.h"
62 #include "user_ops.h"
63 #include "internet_addressing.h"
64
65 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
66 int chkpwd_write_pipe[2];
67 int chkpwd_read_pipe[2];
68
69
70
71 /*
72  * getuser()  -  retrieve named user into supplied buffer.
73  *             returns 0 on success
74  */
75 int getuser(struct ctdluser *usbuf, char name[])
76 {
77         return CtdlGetUser(usbuf, name);
78 }
79
80
81 /*
82  * CtdlGetUser()  -  retrieve named user into supplied buffer.
83  *             returns 0 on success
84  */
85 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
86 {
87
88         char usernamekey[USERNAME_SIZE];
89         struct cdbdata *cdbus;
90
91         if (usbuf != NULL) {
92                 memset(usbuf, 0, sizeof(struct ctdluser));
93         }
94
95         makeuserkey(usernamekey, name, len);
96         cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
97
98         if (cdbus == NULL) {    /* user not found */
99                 return(1);
100         }
101         if (usbuf != NULL) {
102                 memcpy(usbuf, cdbus->ptr,
103                         ((cdbus->len > sizeof(struct ctdluser)) ?
104                          sizeof(struct ctdluser) : cdbus->len));
105         }
106         cdb_free(cdbus);
107
108         return (0);
109 }
110
111
112 int CtdlGetUser(struct ctdluser *usbuf, char *name)
113 {
114         return CtdlGetUserLen(usbuf, name, cutuserkey(name));
115 }
116
117
118 /*
119  * CtdlGetUserLock()  -  same as getuser() but locks the record
120  */
121 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
122 {
123         int retcode;
124
125         retcode = CtdlGetUser(usbuf, name);
126         if (retcode == 0) {
127                 begin_critical_section(S_USERS);
128         }
129         return (retcode);
130 }
131
132
133 /*
134  * lgetuser()  -  same as getuser() but locks the record
135  */
136 int lgetuser(struct ctdluser *usbuf, char *name)
137 {
138         return CtdlGetUserLock(usbuf, name);
139 }
140
141
142 /*
143  * CtdlPutUser()  -  write user buffer into the correct place on disk
144  */
145 void CtdlPutUser(struct ctdluser *usbuf)
146 {
147         char usernamekey[USERNAME_SIZE];
148
149         makeuserkey(usernamekey, 
150                     usbuf->fullname, 
151                     cutuserkey(usbuf->fullname));
152
153         usbuf->version = REV_LEVEL;
154         cdb_store(CDB_USERS,
155                   usernamekey, strlen(usernamekey),
156                   usbuf, sizeof(struct ctdluser));
157
158 }
159
160
161 /*
162  * putuser()  -  write user buffer into the correct place on disk
163  */
164 void putuser(struct ctdluser *usbuf)
165 {
166         CtdlPutUser(usbuf);
167 }
168
169
170 /*
171  * CtdlPutUserLock()  -  same as putuser() but locks the record
172  */
173 void CtdlPutUserLock(struct ctdluser *usbuf)
174 {
175         CtdlPutUser(usbuf);
176         end_critical_section(S_USERS);
177 }
178
179
180 /*
181  * lputuser()  -  same as putuser() but locks the record
182  */
183 void lputuser(struct ctdluser *usbuf)
184 {
185         CtdlPutUserLock(usbuf);
186 }
187
188
189 /*
190  * rename_user()  -  this is tricky because the user's display name is the database key
191  *
192  * Returns 0 on success or nonzero if there was an error...
193  *
194  */
195 int rename_user(char *oldname, char *newname) {
196         int retcode = RENAMEUSER_OK;
197         struct ctdluser usbuf;
198
199         char oldnamekey[USERNAME_SIZE];
200         char newnamekey[USERNAME_SIZE];
201
202         /* Create the database keys... */
203         makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
204         makeuserkey(newnamekey, newname, cutuserkey(newname));
205
206         /* Lock up and get going */
207         begin_critical_section(S_USERS);
208
209         /* We cannot rename a user who is currently logged in */
210         if (CtdlIsUserLoggedIn(oldname)) {
211                 end_critical_section(S_USERS);
212                 return RENAMEUSER_LOGGED_IN;
213         }
214
215         if (CtdlGetUser(&usbuf, newname) == 0) {
216                 retcode = RENAMEUSER_ALREADY_EXISTS;
217         }
218         else {
219
220                 if (CtdlGetUser(&usbuf, oldname) != 0) {
221                         retcode = RENAMEUSER_NOT_FOUND;
222                 }
223
224                 else {          /* Sanity checks succeeded.  Now rename the user. */
225                         if (usbuf.usernum == 0)
226                         {
227                                 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
228                                 retcode = RENAMEUSER_NOT_FOUND;
229                         } else {
230                                 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
231                                 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
232                                 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
233                                 CtdlPutUser(&usbuf);
234                                 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
235                                         usbuf.fullname, strlen(usbuf.fullname)+1 );
236
237                                 retcode = RENAMEUSER_OK;
238                         }
239                 }
240         
241         }
242
243         end_critical_section(S_USERS);
244         return(retcode);
245 }
246
247
248
249 /*
250  * Index-generating function used by Ctdl[Get|Set]Relationship
251  */
252 int GenerateRelationshipIndex(char *IndexBuf,
253                               long RoomID,
254                               long RoomGen,
255                               long UserID)
256 {
257
258         struct {
259                 long iRoomID;
260                 long iRoomGen;
261                 long iUserID;
262         } TheIndex;
263
264         TheIndex.iRoomID = RoomID;
265         TheIndex.iRoomGen = RoomGen;
266         TheIndex.iUserID = UserID;
267
268         memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
269         return (sizeof(TheIndex));
270 }
271
272
273
274 /*
275  * Back end for CtdlSetRelationship()
276  */
277 void put_visit(visit *newvisit)
278 {
279         char IndexBuf[32];
280         int IndexLen = 0;
281
282         memset (IndexBuf, 0, sizeof (IndexBuf));
283         /* Generate an index */
284         IndexLen = GenerateRelationshipIndex(IndexBuf,
285                                              newvisit->v_roomnum,
286                                              newvisit->v_roomgen,
287                                              newvisit->v_usernum);
288
289         /* Store the record */
290         cdb_store(CDB_VISIT, IndexBuf, IndexLen,
291                   newvisit, sizeof(visit)
292         );
293 }
294
295
296
297
298 /*
299  * Define a relationship between a user and a room
300  */
301 void CtdlSetRelationship(visit *newvisit,
302                          struct ctdluser *rel_user,
303                          struct ctdlroom *rel_room)
304 {
305
306
307         /* We don't use these in Citadel because they're implicit by the
308          * index, but they must be present if the database is exported.
309          */
310         newvisit->v_roomnum = rel_room->QRnumber;
311         newvisit->v_roomgen = rel_room->QRgen;
312         newvisit->v_usernum = rel_user->usernum;
313
314         put_visit(newvisit);
315 }
316
317 /*
318  * Locate a relationship between a user and a room
319  */
320 void CtdlGetRelationship(visit *vbuf,
321                          struct ctdluser *rel_user,
322                          struct ctdlroom *rel_room)
323 {
324
325         char IndexBuf[32];
326         int IndexLen;
327         struct cdbdata *cdbvisit;
328
329         /* Generate an index */
330         IndexLen = GenerateRelationshipIndex(IndexBuf,
331                                              rel_room->QRnumber,
332                                              rel_room->QRgen,
333                                              rel_user->usernum);
334
335         /* Clear out the buffer */
336         memset(vbuf, 0, sizeof(visit));
337
338         cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
339         if (cdbvisit != NULL) {
340                 memcpy(vbuf, cdbvisit->ptr,
341                        ((cdbvisit->len > sizeof(visit)) ?
342                         sizeof(visit) : cdbvisit->len));
343                 cdb_free(cdbvisit);
344         }
345         else {
346                 /* If this is the first time the user has seen this room,
347                  * set the view to be the default for the room.
348                  */
349                 vbuf->v_view = rel_room->QRdefaultview;
350         }
351
352         /* Set v_seen if necessary */
353         if (vbuf->v_seen[0] == 0) {
354                 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
355         }
356 }
357
358
359 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
360 {
361         snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
362 }
363
364
365 void MailboxName(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 /*
372  * Check to see if the specified user has Internet mail permission
373  * (returns nonzero if permission is granted)
374  */
375 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
376
377         /* Do not allow twits to send Internet mail */
378         if (who->axlevel <= AxProbU) return(0);
379
380         /* Globally enabled? */
381         if (config.c_restrict == 0) return(1);
382
383         /* User flagged ok? */
384         if (who->flags & US_INTERNET) return(2);
385
386         /* Admin level access? */
387         if (who->axlevel >= AxAideU) return(3);
388
389         /* No mail for you! */
390         return(0);
391 }
392
393 /*
394  * Convenience function.
395  */
396 int CtdlAccessCheck(int required_level)
397 {
398         if (CC->internal_pgm) return(0);
399         if (required_level >= ac_internal) {
400                 cprintf("%d This is not a user-level command.\n",
401                         ERROR + HIGHER_ACCESS_REQUIRED);
402                 return(-1);
403         }
404
405         if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (!config.c_guest_logins)) {
406                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
407                 return(-1);
408         }
409
410         if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
411                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
412                 return(-1);
413         }
414
415         if (CC->user.axlevel >= AxAideU) return(0);
416         if (required_level >= ac_aide) {
417                 cprintf("%d This command requires Admin access.\n",
418                         ERROR + HIGHER_ACCESS_REQUIRED);
419                 return(-1);
420         }
421
422         if (is_room_aide()) return(0);
423         if (required_level >= ac_room_aide) {
424                 cprintf("%d This command requires Admin or Room Admin access.\n",
425                         ERROR + HIGHER_ACCESS_REQUIRED);
426                 return(-1);
427         }
428
429         /* shhh ... succeed quietly */
430         return(0);
431 }
432
433
434
435 /*
436  * Is the user currently logged in an Admin?
437  */
438 int is_aide(void)
439 {
440         if (CC->user.axlevel >= AxAideU)
441                 return (1);
442         else
443                 return (0);
444 }
445
446
447 /*
448  * Is the user currently logged in an Admin *or* the room Admin for this room?
449  */
450 int is_room_aide(void)
451 {
452
453         if (!CC->logged_in) {
454                 return (0);
455         }
456
457         if ((CC->user.axlevel >= AxAideU)
458             || (CC->room.QRroomaide == CC->user.usernum)) {
459                 return (1);
460         } else {
461                 return (0);
462         }
463 }
464
465 /*
466  * CtdlGetUserByNumber() -      get user by number
467  *                      returns 0 if user was found
468  *
469  * Note: fetching a user this way requires one additional database operation.
470  */
471 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
472 {
473         struct cdbdata *cdbun;
474         int r;
475
476         cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
477         if (cdbun == NULL) {
478                 CON_syslog(LOG_INFO, "User %ld not found\n", number);
479                 return(-1);
480         }
481
482         CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
483         r = CtdlGetUser(usbuf, cdbun->ptr);
484         cdb_free(cdbun);
485         return(r);
486 }
487
488 /*
489  * getuserbynumber() -  get user by number
490  *                      returns 0 if user was found
491  *
492  * Note: fetching a user this way requires one additional database operation.
493  */
494 int getuserbynumber(struct ctdluser *usbuf, long number)
495 {
496         return CtdlGetUserByNumber(usbuf, number);
497 }
498
499
500
501 /*
502  * Helper function for rebuild_usersbynumber()
503  */
504 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
505
506         struct ubnlist {
507                 struct ubnlist *next;
508                 char username[USERNAME_SIZE];
509                 long usernum;
510         };
511
512         static struct ubnlist *u = NULL;
513         struct ubnlist *ptr = NULL;
514
515         /* Lazy programming here.  Call this function as a ForEachUser backend
516          * in order to queue up the room names, or call it with a null user
517          * to make it do the processing.
518          */
519         if (usbuf != NULL) {
520                 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
521                 if (ptr == NULL) return;
522
523                 ptr->usernum = usbuf->usernum;
524                 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
525                 ptr->next = u;
526                 u = ptr;
527                 return;
528         }
529
530         while (u != NULL) {
531                 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
532                         u->usernum, u->username);
533                 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
534
535                 ptr = u;
536                 u = u->next;
537                 free(ptr);
538         }
539 }
540
541
542
543 /*
544  * Rebuild the users-by-number index
545  */
546 void rebuild_usersbynumber(void) {
547         cdb_trunc(CDB_USERSBYNUMBER);                   /* delete the old indices */
548         ForEachUser(rebuild_ubn_for_user, NULL);        /* enumerate the users */
549         rebuild_ubn_for_user(NULL, NULL);               /* and index them */
550 }
551
552
553
554 /*
555  * getuserbyuid()  -     get user by system uid (for PAM mode authentication)
556  *                     returns 0 if user was found
557  *
558  * WARNING: don't use this function unless you absolutely have to.  It does
559  *        a sequential search and therefore is computationally expensive.
560  */
561 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
562 {
563         struct cdbdata *cdbus;
564
565         cdb_rewind(CDB_USERS);
566
567         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
568                 memset(usbuf, 0, sizeof(struct ctdluser));
569                 memcpy(usbuf, cdbus->ptr,
570                        ((cdbus->len > sizeof(struct ctdluser)) ?
571                         sizeof(struct ctdluser) : cdbus->len));
572                 cdb_free(cdbus);
573                 if (usbuf->uid == number) {
574                         cdb_close_cursor(CDB_USERS);
575                         return (0);
576                 }
577         }
578         return (-1);
579 }
580
581 /*
582  * Back end for cmd_user() and its ilk
583  *
584  * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
585  */
586 int CtdlLoginExistingUser(char *authname, const char *trythisname)
587 {
588         char username[SIZ];
589         int found_user;
590         long len;
591
592         CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
593
594         if ((CC->logged_in)) {
595                 return login_already_logged_in;
596         }
597
598         if (trythisname == NULL) return login_not_found;
599         
600         if (!strncasecmp(trythisname, "SYS_", 4))
601         {
602                 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
603                 return login_not_found;
604         }
605
606         /* If a "master user" is defined, handle its authentication if specified */
607         CC->is_master = 0;
608         if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
609                 if (!strcasecmp(authname, config.c_master_user)) {
610                         CC->is_master = 1;
611                 }
612         }
613
614         /* Continue attempting user validation... */
615         safestrncpy(username, trythisname, sizeof (username));
616         striplt(username);
617         len = cutuserkey(username);
618
619         if (IsEmptyStr(username)) {
620                 return login_not_found;
621         }
622
623         if (config.c_auth_mode == AUTHMODE_HOST) {
624
625                 /* host auth mode */
626
627                 struct passwd pd;
628                 struct passwd *tempPwdPtr;
629                 char pwdbuffer[256];
630         
631                 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
632 #ifdef HAVE_GETPWNAM_R
633 #ifdef SOLARIS_GETPWUID
634                 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
635                 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
636 #else // SOLARIS_GETPWUID
637                 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
638                 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
639 #endif // SOLARIS_GETPWUID
640 #else // HAVE_GETPWNAM_R
641                 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
642                 tempPwdPtr = NULL;
643 #endif // HAVE_GETPWNAM_R
644                 if (tempPwdPtr == NULL) {
645                         CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
646                         return login_not_found;
647                 }
648         
649                 /* Locate the associated Citadel account.
650                  * If not found, make one attempt to create it.
651                  */
652                 found_user = getuserbyuid(&CC->user, pd.pw_uid);
653                 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
654                         (long)pd.pw_uid, pd.pw_gecos, found_user);
655                 if (found_user != 0) {
656                         len = cutuserkey(username);
657                         create_user(username, len, 0);
658                         found_user = getuserbyuid(&CC->user, pd.pw_uid);
659                 }
660
661         }
662
663 #ifdef HAVE_LDAP
664         else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
665         
666                 /* LDAP auth mode */
667
668                 uid_t ldap_uid;
669                 char ldap_cn[256];
670                 char ldap_dn[256];
671
672                 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
673                 if (found_user != 0) {
674                         return login_not_found;
675                 }
676
677                 found_user = getuserbyuid(&CC->user, ldap_uid);
678                 if (found_user != 0) {
679                         create_user(username, len, 0);
680                         found_user = getuserbyuid(&CC->user, ldap_uid);
681                 }
682
683                 if (found_user == 0) {
684                         if (CC->ldap_dn != NULL) free(CC->ldap_dn);
685                         CC->ldap_dn = strdup(ldap_dn);
686                 }
687
688         }
689 #endif
690
691         else {
692                 /* native auth mode */
693
694                 struct recptypes *valid = NULL;
695         
696                 /* First, try to log in as if the supplied name is a display name */
697                 found_user = CtdlGetUser(&CC->user, username);
698         
699                 /* If that didn't work, try to log in as if the supplied name
700                 * is an e-mail address
701                 */
702                 if (found_user != 0) {
703                         valid = validate_recipients(username, NULL, 0);
704                         if (valid != NULL) {
705                                 if (valid->num_local == 1) {
706                                         found_user = CtdlGetUser(&CC->user, valid->recp_local);
707                                 }
708                                 free_recipients(valid);
709                         }
710                 }
711         }
712
713         /* Did we find something? */
714         if (found_user == 0) {
715                 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
716                         return login_too_many_users;
717                 } else {
718                         safestrncpy(CC->curr_user, CC->user.fullname,
719                                         sizeof CC->curr_user);
720                         return login_ok;
721                 }
722         }
723         return login_not_found;
724 }
725
726
727
728
729
730 /*
731  * session startup code which is common to both cmd_pass() and cmd_newu()
732  */
733 void do_login(void)
734 {
735         struct CitContext *CCC = CC;
736
737         CCC->logged_in = 1;
738         CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
739
740         CtdlGetUserLock(&CCC->user, CCC->curr_user);
741         ++(CCC->user.timescalled);
742         CCC->previous_login = CCC->user.lastcall;
743         time(&CCC->user.lastcall);
744
745         /* If this user's name is the name of the system administrator
746          * (as specified in setup), automatically assign access level 6.
747          */
748         if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
749                 CCC->user.axlevel = AxAideU;
750         }
751
752         /* If we're authenticating off the host system, automatically give
753          * root the highest level of access.
754          */
755         if (config.c_auth_mode == AUTHMODE_HOST) {
756                 if (CCC->user.uid == 0) {
757                         CCC->user.axlevel = AxAideU;
758                 }
759         }
760
761         CtdlPutUserLock(&CCC->user);
762
763         /*
764          * Populate CCC->cs_inet_email with a default address.  This will be
765          * overwritten with the user's directory address, if one exists, when
766          * the vCard module's login hook runs.
767          */
768         snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
769                 CCC->user.fullname, config.c_fqdn);
770         convert_spaces_to_underscores(CCC->cs_inet_email);
771
772         /* Create any personal rooms required by the system.
773          * (Technically, MAILROOM should be there already, but just in case...)
774          */
775         CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
776         CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
777         CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
778         CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
779
780         /* Run any startup routines registered by loadable modules */
781         PerformSessionHooks(EVT_LOGIN);
782
783         /* Enter the lobby */
784         CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
785 }
786
787
788 void logged_in_response(void)
789 {
790         cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
791                 CIT_OK, CC->user.fullname, CC->user.axlevel,
792                 CC->user.timescalled, CC->user.posted,
793                 CC->user.flags, CC->user.usernum,
794                 CC->previous_login);
795 }
796
797
798
799 void CtdlUserLogout(void)
800 {
801         CitContext *CCC = MyContext();
802
803         CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
804                    CCC->curr_user, CCC->cs_pid
805         );
806
807         /* Run any hooks registered by modules... */
808         PerformSessionHooks(EVT_LOGOUT);
809         
810         /*
811          * Clear out some session data.  Most likely, the CitContext for this
812          * session is about to get nuked when the session disconnects, but
813          * since it's possible to log in again without reconnecting, we cannot
814          * make that assumption.
815          */
816         strcpy(CCC->fake_username, "");
817         strcpy(CCC->fake_hostname, "");
818         strcpy(CCC->fake_roomname, "");
819         CCC->logged_in = 0;
820
821         /* Check to see if the user was deleted whilst logged in and purge them if necessary */
822         if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
823                 purge_user(CCC->user.fullname);
824
825         /* Clear out the user record in memory so we don't behave like a ghost */
826         memset(&CCC->user, 0, sizeof(struct ctdluser));
827         CCC->curr_user[0] = 0;
828         CCC->is_master = 0;
829         CCC->cs_inet_email[0] = 0;
830         CCC->cs_inet_other_emails[0] = 0;
831         CCC->cs_inet_fn[0] = 0;
832         CCC->fake_username[0] = 0;
833         CCC->fake_hostname[0] = 0;
834         CCC->fake_roomname[0] = 0;
835         
836
837         /* Free any output buffers */
838         unbuffer_output();
839 }
840
841
842 /*
843  * Validate a password on the host unix system by talking to the chkpwd daemon
844  */
845 static int validpw(uid_t uid, const char *pass)
846 {
847         char buf[256];
848         int rv = 0;
849
850         if (IsEmptyStr(pass)) {
851                 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
852                 return 0;
853         }
854
855         CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
856
857         begin_critical_section(S_CHKPWD);
858         rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
859         if (rv == -1) {
860                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
861                 end_critical_section(S_CHKPWD);
862                 return 0;
863         }
864         rv = write(chkpwd_write_pipe[1], pass, 256);
865         if (rv == -1) {
866                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
867                 end_critical_section(S_CHKPWD);
868                 return 0;
869         }
870         rv = read(chkpwd_read_pipe[0], buf, 4);
871         if (rv == -1) {
872                 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
873                 end_critical_section(S_CHKPWD);
874                 return 0;
875         }
876         end_critical_section(S_CHKPWD);
877
878         if (!strncmp(buf, "PASS", 4)) {
879                 CONM_syslog(LOG_DEBUG, "...pass\n");
880                 return(1);
881         }
882
883         CONM_syslog(LOG_DEBUG, "...fail\n");
884         return 0;
885 }
886
887 /* 
888  * Start up the chkpwd daemon so validpw() has something to talk to
889  */
890 void start_chkpwd_daemon(void) {
891         pid_t chkpwd_pid;
892         struct stat filestats;
893         int i;
894
895         CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
896
897         if ((stat(file_chkpwd, &filestats)==-1) ||
898             (filestats.st_size==0)){
899                 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
900                 abort();
901         }
902         if (pipe(chkpwd_write_pipe) != 0) {
903                 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
904                 abort();
905         }
906         if (pipe(chkpwd_read_pipe) != 0) {
907                 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
908                 abort();
909         }
910
911         chkpwd_pid = fork();
912         if (chkpwd_pid < 0) {
913                 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
914                 abort();
915         }
916         if (chkpwd_pid == 0) {
917                 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
918                 dup2(chkpwd_write_pipe[0], 0);
919                 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
920                 dup2(chkpwd_read_pipe[1], 1);
921                 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
922                 for (i=2; i<256; ++i) close(i);
923                 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
924                 execl(file_chkpwd, file_chkpwd, NULL);
925                 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
926                 abort();
927                 exit(errno);
928         }
929 }
930
931
932 int CtdlTryPassword(const char *password, long len)
933 {
934         int code;
935         CitContext *CCC = CC;
936
937         if ((CCC->logged_in)) {
938                 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
939                 return pass_already_logged_in;
940         }
941         if (!strcmp(CCC->curr_user, NLI)) {
942                 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
943                 return pass_no_user;
944         }
945         if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
946                 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
947                 return pass_internal_error;
948         }
949         if (password == NULL) {
950                 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
951                 return pass_wrong_password;
952         }
953
954         if (CCC->is_master) {
955                 code = strcmp(password, config.c_master_pass);
956         }
957
958         else if (config.c_auth_mode == AUTHMODE_HOST) {
959
960                 /* host auth mode */
961
962                 if (validpw(CCC->user.uid, password)) {
963                         code = 0;
964
965                         /*
966                          * sooper-seekrit hack: populate the password field in the
967                          * citadel database with the password that the user typed,
968                          * if it's correct.  This allows most sites to convert from
969                          * host auth to native auth if they want to.  If you think
970                          * this is a security hazard, comment it out.
971                          */
972
973                         CtdlGetUserLock(&CCC->user, CCC->curr_user);
974                         safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
975                         CtdlPutUserLock(&CCC->user);
976
977                         /*
978                          * (sooper-seekrit hack ends here)
979                          */
980
981                 }
982                 else {
983                         code = (-1);
984                 }
985         }
986
987 #ifdef HAVE_LDAP
988         else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
989
990                 /* LDAP auth mode */
991
992                 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
993                         code = 0;
994                 }
995                 else {
996                         code = (-1);
997                 }
998         }
999 #endif
1000
1001         else {
1002
1003                 /* native auth mode */
1004                 char *pw;
1005
1006                 pw = (char*) malloc(len + 1);
1007                 memcpy(pw, password, len + 1);
1008                 strproc(pw);
1009                 strproc(CCC->user.password);
1010                 code = strcasecmp(CCC->user.password, pw);
1011                 if (code != 0) {
1012                         strproc(pw);
1013                         strproc(CCC->user.password);
1014                         code = strcasecmp(CCC->user.password, pw);
1015                 }
1016                 free (pw);
1017         }
1018
1019         if (!code) {
1020                 do_login();
1021                 return pass_ok;
1022         } else {
1023                 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
1024                            CCC->curr_user,
1025                            CCC->ServiceName,
1026                            CCC->tcp_port,
1027                            CCC->cs_host,
1028                            CCC->cs_addr);
1029
1030
1031 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
1032
1033                 return pass_wrong_password;
1034         }
1035 }
1036
1037
1038
1039
1040 /*
1041  * Delete a user record *and* all of its related resources.
1042  */
1043 int purge_user(char pname[])
1044 {
1045         char filename[64];
1046         struct ctdluser usbuf;
1047         char usernamekey[USERNAME_SIZE];
1048
1049         makeuserkey(usernamekey, pname, cutuserkey(pname));
1050
1051         /* If the name is empty we can't find them in the DB any way so just return */
1052         if (IsEmptyStr(pname))
1053                 return (ERROR + NO_SUCH_USER);
1054
1055         if (CtdlGetUser(&usbuf, pname) != 0) {
1056                 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1057                 return (ERROR + NO_SUCH_USER);
1058         }
1059         /* Don't delete a user who is currently logged in.  Instead, just
1060          * set the access level to 0, and let the account get swept up
1061          * during the next purge.
1062          */
1063         if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1064                 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1065                 usbuf.axlevel = AxDeleted;
1066                 CtdlPutUser(&usbuf);
1067                 return (1);
1068         }
1069         CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1070
1071 /*
1072  * FIXME:
1073  * This should all be wrapped in a S_USERS mutex.
1074  * Without the mutex the user could log in before we get to the next function
1075  * That would truly mess things up :-(
1076  * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1077  * and end after the user has been deleted from the database, below.
1078  * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1079  */
1080
1081         /* Perform any purge functions registered by server extensions */
1082         PerformUserHooks(&usbuf, EVT_PURGEUSER);
1083
1084         /* delete any existing user/room relationships */
1085         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1086
1087         /* delete the users-by-number index record */
1088         cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1089
1090         /* delete the userlog entry */
1091         cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1092
1093         /* remove the user's bio file */
1094         snprintf(filename, 
1095                          sizeof filename, 
1096                          "%s/%ld",
1097                          ctdl_bio_dir,
1098                          usbuf.usernum);
1099         unlink(filename);
1100
1101         /* remove the user's picture */
1102         snprintf(filename, 
1103                          sizeof filename, 
1104                          "%s/%ld.gif",
1105                          ctdl_image_dir,
1106                          usbuf.usernum);
1107         unlink(filename);
1108
1109         return (0);
1110 }
1111
1112
1113 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1114 {
1115         if (!CtdlGetUserLen(usbuf, username, len)) {
1116                 return (ERROR + ALREADY_EXISTS);
1117         }
1118
1119         /* Go ahead and initialize a new user record */
1120         memset(usbuf, 0, sizeof(struct ctdluser));
1121         safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1122         strcpy(usbuf->password, "");
1123         usbuf->uid = uid;
1124
1125         /* These are the default flags on new accounts */
1126         usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1127
1128         usbuf->timescalled = 0;
1129         usbuf->posted = 0;
1130         usbuf->axlevel = config.c_initax;
1131         usbuf->lastcall = time(NULL);
1132
1133         /* fetch a new user number */
1134         usbuf->usernum = get_new_user_number();
1135
1136         /* add user to the database */
1137         CtdlPutUser(usbuf);
1138         cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1139
1140         return 0;
1141 }
1142
1143
1144
1145 /*
1146  * create_user()  -  back end processing to create a new user
1147  *
1148  * Set 'newusername' to the desired account name.
1149  * Set 'become_user' to nonzero if this is self-service account creation and we want
1150  * to actually log in as the user we just created, otherwise set it to 0.
1151  */
1152 int create_user(const char *newusername, long len, int become_user)
1153 {
1154         struct ctdluser usbuf;
1155         struct ctdlroom qrbuf;
1156         char username[256];
1157         char mailboxname[ROOMNAMELEN];
1158         char buf[SIZ];
1159         int retval;
1160         uid_t uid = (-1);
1161         
1162
1163         safestrncpy(username, newusername, sizeof username);
1164         strproc(username);
1165
1166         
1167         if (config.c_auth_mode == AUTHMODE_HOST) {
1168
1169                 /* host auth mode */
1170
1171                 struct passwd pd;
1172                 struct passwd *tempPwdPtr;
1173                 char pwdbuffer[SIZ];
1174         
1175 #ifdef HAVE_GETPWNAM_R
1176 #ifdef SOLARIS_GETPWUID
1177                 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1178 #else // SOLARIS_GETPWUID
1179                 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1180 #endif // SOLARIS_GETPWUID
1181 #else // HAVE_GETPWNAM_R
1182                 tempPwdPtr = NULL;
1183 #endif // HAVE_GETPWNAM_R
1184                 if (tempPwdPtr != NULL) {
1185                         extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1186                         uid = pd.pw_uid;
1187                         if (IsEmptyStr (username))
1188                         {
1189                                 safestrncpy(username, pd.pw_name, sizeof username);
1190                                 len = cutuserkey(username);
1191                         }
1192                 }
1193                 else {
1194                         return (ERROR + NO_SUCH_USER);
1195                 }
1196         }
1197
1198 #ifdef HAVE_LDAP
1199         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1200                 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1201                         return(ERROR + NO_SUCH_USER);
1202                 }
1203         }
1204 #endif /* HAVE_LDAP */
1205         
1206         if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1207                 return retval;
1208         
1209         /*
1210          * Give the user a private mailbox and a configuration room.
1211          * Make the latter an invisible system room.
1212          */
1213         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1214         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1215
1216         CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1217         CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1218         if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1219                 qrbuf.QRflags2 |= QR2_SYSTEM;
1220                 CtdlPutRoomLock(&qrbuf);
1221         }
1222
1223         /* Perform any create functions registered by server extensions */
1224         PerformUserHooks(&usbuf, EVT_NEWUSER);
1225
1226         /* Everything below this line can be bypassed if administratively
1227          * creating a user, instead of doing self-service account creation
1228          */
1229
1230         if (become_user) {
1231                 /* Now become the user we just created */
1232                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1233                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1234                 do_login();
1235         
1236                 /* Check to make sure we're still who we think we are */
1237                 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1238                         return (ERROR + INTERNAL_ERROR);
1239                 }
1240         }
1241         
1242         snprintf(buf, SIZ, 
1243                 "New user account <%s> has been created, from host %s [%s].\n",
1244                 username,
1245                 CC->cs_host,
1246                 CC->cs_addr
1247         );
1248         CtdlAideMessage(buf, "User Creation Notice");
1249         CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1250         return (0);
1251 }
1252
1253
1254
1255 /*
1256  * set password - back end api code
1257  */
1258 void CtdlSetPassword(char *new_pw)
1259 {
1260         CtdlGetUserLock(&CC->user, CC->curr_user);
1261         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1262         CtdlPutUserLock(&CC->user);
1263         CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1264         PerformSessionHooks(EVT_SETPASS);
1265 }
1266
1267
1268
1269
1270 /*
1271  * API function for cmd_invt_kick() and anything else that needs to
1272  * invite or kick out a user to/from a room.
1273  * 
1274  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1275  */
1276 int CtdlInvtKick(char *iuser, int op) {
1277         struct ctdluser USscratch;
1278         visit vbuf;
1279         char bbb[SIZ];
1280
1281         if (CtdlGetUser(&USscratch, iuser) != 0) {
1282                 return(1);
1283         }
1284
1285         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1286         if (op == 1) {
1287                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1288                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1289         }
1290         if (op == 0) {
1291                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1292                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1293         }
1294         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1295
1296         /* post a message in Aide> saying what we just did */
1297         snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1298                 iuser,
1299                 ((op == 1) ? "invited to" : "kicked out of"),
1300                 CC->room.QRname,
1301                 (CC->logged_in ? CC->user.fullname : "an administrator")
1302         );
1303         CtdlAideMessage(bbb,"User Admin Message");
1304
1305         return(0);
1306 }
1307
1308
1309 /*
1310  * Forget (Zap) the current room (API call)
1311  * Returns 0 on success
1312  */
1313 int CtdlForgetThisRoom(void) {
1314         visit vbuf;
1315
1316         /* On some systems, Admins are not allowed to forget rooms */
1317         if (is_aide() && (config.c_aide_zap == 0)
1318            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1319                 return(1);
1320         }
1321
1322         CtdlGetUserLock(&CC->user, CC->curr_user);
1323         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1324
1325         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1326         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1327
1328         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1329         CtdlPutUserLock(&CC->user);
1330
1331         /* Return to the Lobby, so we don't end up in an undefined room */
1332         CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1333         return(0);
1334
1335 }
1336
1337
1338
1339
1340 /* 
1341  *  Traverse the user file...
1342  */
1343 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1344                  void *in_data)
1345 {
1346         struct ctdluser usbuf;
1347         struct cdbdata *cdbus;
1348
1349         cdb_rewind(CDB_USERS);
1350
1351         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1352                 memset(&usbuf, 0, sizeof(struct ctdluser));
1353                 memcpy(&usbuf, cdbus->ptr,
1354                        ((cdbus->len > sizeof(struct ctdluser)) ?
1355                         sizeof(struct ctdluser) : cdbus->len));
1356                 cdb_free(cdbus);
1357                 (*CallBack) (&usbuf, in_data);
1358         }
1359 }
1360
1361
1362 /*
1363  * List one user (this works with cmd_list)
1364  */
1365 void ListThisUser(struct ctdluser *usbuf, void *data)
1366 {
1367         char *searchstring;
1368
1369         searchstring = (char *)data;
1370         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1371                 return;
1372         }
1373
1374         if (usbuf->axlevel > AxDeleted) {
1375                 if ((CC->user.axlevel >= AxAideU)
1376                     || ((usbuf->flags & US_UNLISTED) == 0)
1377                     || ((CC->internal_pgm))) {
1378                         cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1379                                 usbuf->fullname,
1380                                 usbuf->axlevel,
1381                                 usbuf->usernum,
1382                                 (long)usbuf->lastcall,
1383                                 usbuf->timescalled,
1384                                 usbuf->posted);
1385                 }
1386         }
1387 }
1388
1389
1390
1391
1392 /*
1393  * Count the number of new mail messages the user has
1394  */
1395 int NewMailCount()
1396 {
1397         int num_newmsgs = 0;
1398
1399         num_newmsgs = CC->newmail;
1400         CC->newmail = 0;
1401
1402         return (num_newmsgs);
1403 }
1404
1405
1406 /*
1407  * Count the number of new mail messages the user has
1408  */
1409 int InitialMailCheck()
1410 {
1411         int num_newmsgs = 0;
1412         int a;
1413         char mailboxname[ROOMNAMELEN];
1414         struct ctdlroom mailbox;
1415         visit vbuf;
1416         struct cdbdata *cdbfr;
1417         long *msglist = NULL;
1418         int num_msgs = 0;
1419
1420         CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1421         if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1422                 return (0);
1423         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1424
1425         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1426
1427         if (cdbfr != NULL) {
1428                 msglist = malloc(cdbfr->len);
1429                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1430                 num_msgs = cdbfr->len / sizeof(long);
1431                 cdb_free(cdbfr);
1432         }
1433         if (num_msgs > 0)
1434                 for (a = 0; a < num_msgs; ++a) {
1435                         if (msglist[a] > 0L) {
1436                                 if (msglist[a] > vbuf.v_lastseen) {
1437                                         ++num_newmsgs;
1438                                 }
1439                         }
1440                 }
1441         if (msglist != NULL)
1442                 free(msglist);
1443
1444         return (num_newmsgs);
1445 }
1446
1447
1448
1449