/* needed to properly enable crypt() stuff on some systems */
#define _XOPEN_SOURCE
+/* needed for str[n]casecmp() on some systems if the above is defined */
+#define _XOPEN_SOURCE_EXTENDED
+/* needed to enable threads on some systems if the above are defined */
+#define _POSIX_C_SOURCE 199506L
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <syslog.h>
+#include <limits.h>
#include <pthread.h>
#include "citadel.h"
#include "server.h"
-#include "proto.h"
-
-extern struct config config;
-
-
-/*
- * hash() - hash table function for user lookup
- */
-int hash(char *str)
-{
- int h = 0;
- int i;
-
- for (i=0; i<strlen(str); ++i) h=h+((i+1)*tolower(str[i]));
- return(h);
- }
+#include "database.h"
+#include "user_ops.h"
+#include "sysdep_decls.h"
+#include "support.h"
+#include "room_ops.h"
+#include "logging.h"
+#include "file_ops.h"
+#include "control.h"
+#include "msgbase.h"
+#include "config.h"
+#include "dynloader.h"
+#include "sysdep.h"
/*
}
cdbus = cdb_fetch(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
- if (cdbus == NULL) { /* not found */
- return(1);
+ if (cdbus == NULL) {
+ return(1); /* user not found */
}
- memcpy(usbuf, cdbus->ptr, cdbus->len);
+ memcpy(usbuf, cdbus->ptr,
+ ( (cdbus->len > sizeof(struct usersupp)) ?
+ sizeof(struct usersupp) : cdbus->len) );
cdb_free(cdbus);
return(0);
}
lowercase_name[a] = tolower(name[a]);
}
- cdb_store(CDB_USERSUPP, lowercase_name, strlen(lowercase_name),
+ cdb_store(CDB_USERSUPP,
+ lowercase_name, strlen(lowercase_name),
usbuf, sizeof(struct usersupp));
}
/*
* lputuser() - same as putuser() but locks the record
*/
-void lputuser(struct usersupp *usbuf, char *name)
-{
+void lputuser(struct usersupp *usbuf, char *name) {
putuser(usbuf,name);
end_critical_section(S_USERSUPP);
}
+/*
+ * Define a relationship between a user and a room
+ */
+void CtdlSetRelationship(struct visit *newvisit,
+ struct usersupp *rel_user,
+ struct quickroom *rel_room) {
+
+ struct cdbdata *cdbvisit;
+ struct visit *visits;
+ int num_visits;
+ int a;
+ int replaced = 0;
+
+ cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
+ if (cdbvisit != NULL) {
+ num_visits = cdbvisit->len / sizeof(struct visit);
+ visits = (struct visit *)
+ malloc(num_visits * sizeof(struct visit));
+ memcpy(visits, cdbvisit->ptr,
+ (num_visits * sizeof(struct visit)));
+ cdb_free(cdbvisit);
+ }
+ else {
+ num_visits = 0;
+ visits = NULL;
+ }
+
+ /* Replace an existing relationship if possible */
+ if (num_visits > 0) for (a=0; a<num_visits; ++a) {
+ if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
+ && (visits[a].v_generation == rel_room->QRgen) ) {
+ memcpy(&visits[a], newvisit, sizeof(struct visit));
+ replaced = 1;
+ }
+ }
+
+ /* Otherwise, define a new one */
+ if (replaced == 0) {
+ ++num_visits;
+ visits = realloc(visits,
+ (num_visits * sizeof(struct visit)));
+ memcpy(&visits[num_visits-1], newvisit, sizeof(struct visit));
+ }
+
+ /* Now write the relationship back to disk */
+ cdb_store(CDB_VISIT,
+ &rel_user->usernum, sizeof(long),
+ visits,
+ (num_visits * sizeof(struct visit)));
+ free(visits);
+ }
+
+/*
+ * Locate a relationship between a user and a room
+ */
+void CtdlGetRelationship(struct visit *vbuf,
+ struct usersupp *rel_user,
+ struct quickroom *rel_room) {
+
+ struct cdbdata *cdbvisit;
+ struct visit *visits;
+ int num_visits;
+ int a;
+
+ bzero(vbuf, sizeof(struct visit));
+ strcpy(vbuf->v_roomname, rel_room->QRname);
+ vbuf->v_generation = rel_room->QRgen;
+
+ cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
+ if (cdbvisit != NULL) {
+ if ((num_visits = cdbvisit->len / sizeof(struct visit)) == 0) {
+ cdb_free(cdbvisit);
+ return;
+ }
+ visits = (struct visit *)
+ malloc(num_visits * sizeof(struct visit));
+ memcpy(visits, cdbvisit->ptr,
+ (num_visits * sizeof(struct visit)));
+ cdb_free(cdbvisit);
+ }
+ else return;
+
+ for (a=0; a<num_visits; ++a) {
+
+ if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
+ && (visits[a].v_generation == rel_room->QRgen) ) {
+ memcpy(vbuf, &visits[a], sizeof(struct visit));
+ }
+ }
+
+ free(visits);
+ }
+
+
+void MailboxName(char *buf, struct usersupp *who, char *prefix) {
+ sprintf(buf, "%010ld.%s", who->usernum, prefix);
+ }
+
+
/*
* Is the user currently logged in an Aide?
*/
*/
int is_room_aide(void) {
if ( (CC->usersupp.axlevel >= 6)
- || (CC->quickroom.QRroomaide == CC->usersupp.usernum) ) return(1);
- else return(0);
+ || (CC->quickroom.QRroomaide == CC->usersupp.usernum) ) {
+ return(1);
+ }
+ else {
+ return(0);
+ }
}
/*
while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
bzero(usbuf, sizeof(struct usersupp));
- memcpy(usbuf, cdbus->ptr, cdbus->len);
+ memcpy(usbuf, cdbus->ptr,
+ ( (cdbus->len > sizeof(struct usersupp)) ?
+ sizeof(struct usersupp) : cdbus->len) );
cdb_free(cdbus);
if (usbuf->usernum == number) {
return(0);
* session startup code which is common to both cmd_pass() and cmd_newu()
*/
void session_startup(void) {
- int a;
- struct quickroom qr;
-
syslog(LOG_NOTICE,"user <%s> logged in",CC->curr_user);
- hook_user_login(CC->cs_pid, CC->curr_user);
+
lgetuser(&CC->usersupp,CC->curr_user);
++(CC->usersupp.timescalled);
CC->fake_username[0] = '\0';
/* If this user's name is the name of the system administrator
* (as specified in setup), automatically assign access level 6.
*/
- if (!strucmp(CC->usersupp.fullname, config.c_sysadm)) {
+ if (!strcasecmp(CC->usersupp.fullname, config.c_sysadm)) {
CC->usersupp.axlevel = 6;
}
-/* A room's generation number changes each time it is recycled. Users are kept
- * out of private rooms or forget rooms by matching the generation numbers. To
- * avoid an accidental matchup, unmatched numbers are set to -1 here.
- */
- for (a=0; a<MAXROOMS; ++a) {
- getroom(&qr,a);
- if (CC->usersupp.generation[a] != qr.QRgen)
- CC->usersupp.generation[a]=(-1);
- if (CC->usersupp.forget[a] != qr.QRgen)
- CC->usersupp.forget[a]=(-1);
- }
-
lputuser(&CC->usersupp,CC->curr_user);
+ /* Run any cleanup routines registered by loadable modules */
+ PerformSessionHooks(EVT_LOGIN);
+
cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
CC->usersupp.usernum);
- usergoto(0,0); /* Enter the lobby */
+ usergoto(BASEROOM,0); /* Enter the lobby */
rec_log(CL_LOGIN,CC->curr_user);
}
-
/*
* misc things to be taken care of when a user is logged out
*/
if (who->upload_fp != NULL) {
abort_upl(who);
}
+
+ /* Do modular stuff... */
+ PerformSessionHooks(EVT_LOGOUT);
}
if (CC->usersupp.USuid == BBSUID) {
strproc(password);
strproc(CC->usersupp.password);
- code = strucmp(CC->usersupp.password,password);
+ code = strcasecmp(CC->usersupp.password,password);
}
else {
p = (struct passwd *)getpwuid(CC->usersupp.USuid);
/*
- * purge related files when removing or overwriting a user record
+ * Delete a user record *and* all of its related resources.
*/
-void purge_user(char *pname) {
+int purge_user(char *pname) {
char filename[64];
struct usersupp usbuf;
int a;
if (getuser(&usbuf, pname) != 0) {
lprintf(5, "Cannot purge user <%s> - not found\n", pname);
- return;
+ return(ERROR+NO_SUCH_USER);
}
+ /* FIX Don't delete a user who is currently logged in. */
+
+ /* Perform any purge functions registered by server extensions */
+ PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
+
/* delete any messages in the user's mailbox */
cdbmb = cdb_fetch(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
if (cdbmb != NULL) {
cdb_delete(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
}
+ /* delete any existing user/room relationships */
+ cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
/* delete the userlog entry */
cdb_delete(CDB_USERSUPP, pname, strlen(pname));
/* remove the user's picture */
sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
unlink(filename);
-
+
+ return(0);
}
int a;
struct passwd *p = NULL;
char username[64];
+ char mailboxname[ROOMNAMELEN];
strcpy(username, newusername);
strproc(username);
strcpy(CC->curr_user,username);
strcpy(CC->usersupp.fullname,username);
- (CC->logged_in) = 1;
-
- for (a=0; a<MAXROOMS; ++a) {
- CC->usersupp.lastseen[a]=0L;
- CC->usersupp.generation[a]=(-1);
- CC->usersupp.forget[a]=(-1);
- }
strcpy(CC->usersupp.password,"");
+ (CC->logged_in) = 1;
/* These are the default flags on new accounts */
CC->usersupp.flags =
CC->usersupp.timescalled = 0;
CC->usersupp.posted = 0;
- CC->usersupp.axlevel = INITAX;
+ CC->usersupp.axlevel = config.c_initax;
CC->usersupp.USscreenwidth = 80;
CC->usersupp.USscreenheight = 24;
time(&CC->usersupp.lastcall);
if (getuser(&CC->usersupp,CC->curr_user)) {
return(ERROR+INTERNAL_ERROR);
}
+
+ /* give the user a private mailbox */
+ MailboxName(mailboxname, &CC->usersupp, MAILROOM);
+ create_room(mailboxname, 4, "", 0);
+
rec_log(CL_NEWUSER,CC->curr_user);
return(0);
}
}
a = create_user(username);
- if ((!strucmp(username, "bbs")) ||
- (!strucmp(username, "new")) ||
- (!strucmp(username, ".")))
+ if ((!strcasecmp(username, "bbs")) ||
+ (!strcasecmp(username, "new")) ||
+ (!strcasecmp(username, ".")))
{
cprintf("%d '%s' is an invalid login name.\n", ERROR);
return;
lputuser(&CC->usersupp,CC->curr_user);
cprintf("%d Password changed.\n",OK);
rec_log(CL_PWCHANGE,CC->curr_user);
+ PerformSessionHooks(EVT_SETPASS);
}
/*
return;
}
getuser(&CC->usersupp,CC->curr_user);
- cprintf("%d %d|%d|%d\n",OK,CC->usersupp.USscreenwidth,
- CC->usersupp.USscreenheight,(CC->usersupp.flags & US_USER_SET));
+ cprintf("%d %d|%d|%d\n",
+ OK,
+ CC->usersupp.USscreenwidth,
+ CC->usersupp.USscreenheight,
+ (CC->usersupp.flags & US_USER_SET)
+ );
}
/*
void cmd_slrp(char *new_ptr)
{
long newlr;
+ struct visit vbuf;
if (!(CC->logged_in)) {
cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
return;
}
- if (CC->curr_rm < 0) {
- cprintf("%d No current room.\n",ERROR);
- return;
- }
-
- if (!struncmp(new_ptr,"highest",7)) {
+ if (!strncasecmp(new_ptr,"highest",7)) {
newlr = CC->quickroom.QRhighest;
}
else {
}
lgetuser(&CC->usersupp, CC->curr_user);
- CC->usersupp.lastseen[CC->curr_rm] = newlr;
+
+ CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+ vbuf.v_lastseen = newlr;
+ CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
lputuser(&CC->usersupp, CC->curr_user);
cprintf("%d %ld\n",OK,newlr);
}
{ /* 1 = invite, 0 = kick out */
struct usersupp USscratch;
char bbb[256];
+ struct visit vbuf;
if (!(CC->logged_in)) {
cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
return;
}
- if (CC->curr_rm < 0) {
- cprintf("%d No current room.\n",ERROR);
- return;
- }
-
if (is_room_aide()==0) {
cprintf("%d Higher access required.\n",
ERROR+HIGHER_ACCESS_REQUIRED);
return;
}
+ /* FIX - with the new relationships scheme we can lock users out,
+ so it'll make sense to remove this routine */
if ( (op==1) && ((CC->quickroom.QRflags&QR_PRIVATE)==0) ) {
cprintf("%d Not a private room.\n",ERROR+NOT_HERE);
return;
return;
}
+ CtdlGetRelationship(&vbuf, &USscratch, &CC->quickroom);
+
if (op==1) {
- USscratch.generation[CC->curr_rm]=CC->quickroom.QRgen;
- USscratch.forget[CC->curr_rm]=(-1);
+ vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
+ vbuf.v_flags = vbuf.v_flags | V_ACCESS;
}
if (op==0) {
- USscratch.generation[CC->curr_rm]=(-1);
- USscratch.forget[CC->curr_rm]=CC->quickroom.QRgen;
+ vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
+ vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
}
+ CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
+
lputuser(&USscratch,iuser);
/* post a message in Aide> saying what we just did */
* forget (Zap) the current room
*/
void cmd_forg(void) {
+ struct visit vbuf;
+
if (!(CC->logged_in)) {
cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
return;
}
- if (CC->curr_rm < 0) {
- cprintf("%d No current room.\n",ERROR);
- return;
- }
-
- if (CC->curr_rm < 3) {
- cprintf("%d You cannot forget this room.\n",ERROR+NOT_HERE);
- return;
- }
-
if (is_aide()) {
cprintf("%d Aides cannot forget rooms.\n",ERROR);
return;
}
lgetuser(&CC->usersupp,CC->curr_user);
- CC->usersupp.forget[CC->curr_rm] = CC->quickroom.QRgen;
- CC->usersupp.generation[CC->curr_rm] = (-1);
+ CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+
+ vbuf.v_flags = vbuf.v_flags | V_FORGET;
+
+ CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
lputuser(&CC->usersupp,CC->curr_user);
cprintf("%d Ok\n",OK);
- CC->curr_rm = (-1);
+ usergoto(BASEROOM, 0);
}
/*
cdb_rewind(CDB_USERSUPP);
while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
bzero(&usbuf, sizeof(struct usersupp));
- memcpy(&usbuf, cdbus->ptr, cdbus->len);
+ memcpy(&usbuf, cdbus->ptr,
+ ( (cdbus->len > sizeof(struct usersupp)) ?
+ sizeof(struct usersupp) : cdbus->len) );
cdb_free(cdbus);
if ((usbuf.flags & US_NEEDVALID)
&&(usbuf.axlevel > 0)) {
return;
}
- if (!strucmp(who,"_SELF_")) strcpy(who,CC->curr_user);
+ if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
- if ((CC->usersupp.axlevel < 6) && (strucmp(who,CC->curr_user))) {
+ if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
cprintf("%d Higher access required.\n",
ERROR+HIGHER_ACCESS_REQUIRED);
return;
/* If the access level was set to zero, delete the user */
if (newax == 0) {
- purge_user(user);
- cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
- return;
+ if (purge_user(user)==0) {
+ cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
+ return;
+ }
}
cprintf("%d ok\n",OK);
/*
- * List users
+ * Traverse the user file...
*/
-void cmd_list(void) {
+void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
struct usersupp usbuf;
struct cdbdata *cdbus;
cdb_rewind(CDB_USERSUPP);
- cprintf("%d \n",LISTING_FOLLOWS);
while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
bzero(&usbuf, sizeof(struct usersupp));
- memcpy(&usbuf, cdbus->ptr, cdbus->len);
+ memcpy(&usbuf, cdbus->ptr,
+ ( (cdbus->len > sizeof(struct usersupp)) ?
+ sizeof(struct usersupp) : cdbus->len) );
cdb_free(cdbus);
+ (*CallBack)(&usbuf);
+ }
+ }
- if (usbuf.axlevel > 0) {
+
+/*
+ * List one user (this works with cmd_list)
+ */
+void ListThisUser(struct usersupp *usbuf) {
+ if (usbuf->axlevel > 0) {
if ((CC->usersupp.axlevel>=6)
- ||((usbuf.flags&US_UNLISTED)==0)
+ ||((usbuf->flags&US_UNLISTED)==0)
||((CC->internal_pgm))) {
cprintf("%s|%d|%ld|%ld|%d|%d|",
- usbuf.fullname,
- usbuf.axlevel,
- usbuf.usernum,
- usbuf.lastcall,
- usbuf.timescalled,
- usbuf.posted);
- if (CC->usersupp.axlevel >= 6) cprintf("%s",usbuf.password);
+ usbuf->fullname,
+ usbuf->axlevel,
+ usbuf->usernum,
+ usbuf->lastcall,
+ usbuf->timescalled,
+ usbuf->posted);
+ if (CC->usersupp.axlevel >= 6)
+ cprintf("%s",usbuf->password);
cprintf("\n");
}
- }
}
+ }
+
+/*
+ * List users
+ */
+void cmd_list(void) {
+ cprintf("%d \n",LISTING_FOLLOWS);
+ ForEachUser(ListThisUser);
cprintf("000\n");
}
+
/*
* enter registration info
*/
int mail = 0;
int regis = 0;
int vali = 0;
- int a;
- struct cdbdata *cdbmb;
- long *mailbox;
- int num_mails;
-
if (!(CC->logged_in)) {
cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
return;
/* check for mail */
- mail = 0;
- cdbmb = cdb_fetch(CDB_MAILBOXES, &CC->usersupp.usernum, sizeof(long));
- if (cdbmb != NULL) {
- num_mails = cdbmb->len / sizeof(long);
- mailbox = (long *) cdbmb->ptr;
- if (num_mails > 0) for (a=0; a<num_mails; ++a) {
- if (mailbox[a] > (CC->usersupp.lastseen[1])) ++mail;
- }
- cdb_free(cdbmb);
- }
-
+ mail = NewMailCount();
cprintf("%d %d|%d|%d\n",OK,mail,regis,vali);
}
pclose(ls);
cprintf("000\n");
}
+
+
+/*
+ * Administrative Get User Parameters
+ */
+void cmd_agup(char *cmdbuf) {
+ struct usersupp usbuf;
+ char requested_user[256];
+
+ if ( (CC->internal_pgm==0)
+ && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
+ cprintf("%d Higher access required.\n",
+ ERROR + HIGHER_ACCESS_REQUIRED);
+ return;
+ }
+
+ extract(requested_user, cmdbuf, 0);
+ if (getuser(&usbuf, requested_user) != 0) {
+ cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
+ return;
+ }
+
+ cprintf("%d %s|%s|%u|%d|%d|%d|%ld\n",
+ OK,
+ usbuf.fullname,
+ usbuf.password,
+ usbuf.flags,
+ usbuf.timescalled,
+ usbuf.posted,
+ (int)usbuf.axlevel,
+ usbuf.usernum);
+
+ }
+
+
+
+/*
+ * Administrative Set User Parameters
+ */
+void cmd_asup(char *cmdbuf) {
+ struct usersupp usbuf;
+ char requested_user[256];
+ int np;
+ int newax;
+
+ if ( (CC->internal_pgm==0)
+ && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
+ cprintf("%d Higher access required.\n",
+ ERROR + HIGHER_ACCESS_REQUIRED);
+ return;
+ }
+
+ extract(requested_user, cmdbuf, 0);
+ if (lgetuser(&usbuf, requested_user) != 0) {
+ cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
+ return;
+ }
+
+ np = num_parms(cmdbuf);
+ if (np > 1) extract(usbuf.password, cmdbuf, 1);
+ if (np > 2) usbuf.flags = extract_int(cmdbuf, 2);
+ if (np > 3) usbuf.timescalled = extract_int(cmdbuf, 3);
+ if (np > 4) usbuf.posted = extract_int(cmdbuf, 4);
+ if (np > 5) {
+ newax = extract_int(cmdbuf, 5);
+ if ((newax >=0) && (newax <= 6)) {
+ usbuf.axlevel = extract_int(cmdbuf, 5);
+ }
+ }
+
+ lputuser(&usbuf, requested_user);
+ if (usbuf.axlevel == 0) {
+ if (purge_user(requested_user)==0) {
+ cprintf("%d %s deleted.\n", OK, requested_user);
+ }
+ }
+ cprintf("%d Ok\n", OK);
+ }
+
+
+/*
+ * Count the number of new mail messages the user has
+ */
+int NewMailCount() {
+ int num_newmsgs = 0;
+ struct cdbdata *cdbmb;
+ int num_mails;
+ long *mailbox;
+ int a;
+ char mailboxname[32];
+
+ MailboxName(mailboxname, &CC->usersupp, MAILROOM);
+ for (a=0; a<=strlen(mailboxname); ++a) {
+ mailboxname[a] = tolower(mailboxname[a]);
+ }
+
+ cdbmb = cdb_fetch(CDB_MAILBOXES, mailboxname, strlen(mailboxname));
+ if (cdbmb != NULL) {
+ num_mails = cdbmb->len / sizeof(long);
+ mailbox = (long *) cdbmb->ptr;
+ if (num_mails > 0) for (a=0; a<num_mails; ++a) {
+ /*
+ if (message is new FIX FIX FIX)
+ ++num_newmsgs;
+ */
+ }
+ cdb_free(cdbmb);
+ }
+ return(num_newmsgs);
+ }