diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-06-12 20:22:07 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-06-12 20:22:07 +0800 |
commit | 2f6959aecaaaed4980f7c8740f2fae2c8fc326ca (patch) | |
tree | d3d59847cadac9939bc0e9945530cfd1b7a5caeb | |
parent | c5f8e88cde8ad8120fd5e2bdaddd2f5591502aa8 (diff) | |
download | pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar.gz pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar.bz2 pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar.lz pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar.xz pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.tar.zst pttbbs-2f6959aecaaaed4980f7c8740f2fae2c8fc326ca.zip |
* refine cmbbs: move util_passwd.o to cmbbs/passwd.c.
* note: in order to workaround the buggy/dirty synchronization hacks, changed 'passwd_query' and 'passwd_update' to passwd_sync_*.
* util/* and daemon/* use directly passwd_query/update, and mbbsd should use passwd_sync_*.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4574 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | common/bbs/cache.c | 18 | ||||
-rw-r--r-- | common/bbs/passwd.c | 135 | ||||
-rw-r--r-- | daemon/logind/Makefile | 2 | ||||
-rw-r--r-- | daemon/logind/logind.c | 15 | ||||
-rw-r--r-- | include/cmbbs.h | 14 | ||||
-rw-r--r-- | include/proto.h | 24 | ||||
-rw-r--r-- | innbbsd/Makefile | 2 | ||||
-rw-r--r-- | mbbsd/admin.c | 4 | ||||
-rw-r--r-- | mbbsd/angel.c | 2 | ||||
-rw-r--r-- | mbbsd/assess.c | 6 | ||||
-rw-r--r-- | mbbsd/bbs.c | 10 | ||||
-rw-r--r-- | mbbsd/cache.c | 14 | ||||
-rw-r--r-- | mbbsd/cal.c | 4 | ||||
-rw-r--r-- | mbbsd/chc.c | 6 | ||||
-rw-r--r-- | mbbsd/go.c | 6 | ||||
-rw-r--r-- | mbbsd/gomo.c | 6 | ||||
-rw-r--r-- | mbbsd/mail.c | 2 | ||||
-rw-r--r-- | mbbsd/mbbsd.c | 6 | ||||
-rw-r--r-- | mbbsd/passwd.c | 243 | ||||
-rw-r--r-- | mbbsd/register.c | 12 | ||||
-rw-r--r-- | mbbsd/user.c | 16 | ||||
-rw-r--r-- | trans/Makefile | 4 | ||||
-rw-r--r-- | util/Makefile | 4 | ||||
-rw-r--r-- | util/bbsmail.c | 2 | ||||
-rw-r--r-- | util/chkhbf.c | 2 | ||||
-rw-r--r-- | util/toplazyBM.c | 4 |
26 files changed, 187 insertions, 376 deletions
diff --git a/common/bbs/cache.c b/common/bbs/cache.c index b61dec86..1166a85b 100644 --- a/common/bbs/cache.c +++ b/common/bbs/cache.c @@ -19,11 +19,6 @@ #include "modes.h" // for DEBUGSLEEPING -// TODO some APIs currently in util_passwd.o and should be moved to -// common/bbs/passwd.c... -int passwd_query(int num, userec_t *buf); -int passwd_update_money(int num); - ////////////////////////////////////////////////////////////////////////// // This is shared by utility library and core BBS, // so do not put code using currutmp/cuser here. @@ -215,19 +210,6 @@ searchuser(const char *userid, char *rightid) return dosearchuser(userid, rightid); } -// XXX check this: can we have it here? -int -getuser(const char *userid, userec_t *xuser) -{ - int uid; - - if ((uid = searchuser(userid, NULL))) { - passwd_query(uid, xuser); - xuser->money = moneyof(uid); - } - return uid; -} - char * getuserid(int num) { diff --git a/common/bbs/passwd.c b/common/bbs/passwd.c index 3c41793d..3e55d4d2 100644 --- a/common/bbs/passwd.c +++ b/common/bbs/passwd.c @@ -1,12 +1,31 @@ /* $Id$ */ +#include <assert.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <sys/types.h> #include <sys/ipc.h> +#include <sys/shm.h> #include <sys/sem.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include "common.h" +#include "var.h" #include "cmbbs.h" -#if 0 +////////////////////////////////////////////////////////////////////////// +// This is shared by utility library and core BBS, +// so do not put code using currutmp/cuser here. +////////////////////////////////////////////////////////////////////////// + +// these cannot be used! +#define currutmp YOU_FAILED +#define usernum YOU_FAILED +#define cuser YOU_FAILED +#define abort_bbs YOU_FAILED +#define log_usies YOU_FAILED static int semid = -1; @@ -28,6 +47,8 @@ union semun { }; #endif +// semaphore based PASSWD locking + int passwd_init(void) { @@ -56,6 +77,30 @@ passwd_init(void) return 0; } +void +passwd_lock(void) +{ + struct sembuf buf = {0, -1, SEM_UNDO}; + + if (semop(semid, &buf, 1)) { + perror("semop"); + exit(1); + } +} + +void +passwd_unlock(void) +{ + struct sembuf buf = {0, 1, SEM_UNDO}; + + if (semop(semid, &buf, 1)) { + perror("semop"); + exit(1); + } +} + +// updateing passwd/userec_t + int passwd_update_money(int num) /* update money only @@ -76,60 +121,19 @@ passwd_update_money(int num) return 0; } -void -passwd_force_update(int flag) -{ - if(!currutmp || (currutmp->alerts & ALERT_PWD) == 0) - return; - currutmp->alerts &= ~flag; -} - int passwd_update(int num, userec_t * buf) { int pwdfd; if (num < 1 || num > MAX_USERS) return -1; - buf->money = moneyof(num); - if(usernum == num && currutmp && ((pwdfd = currutmp->alerts) & ALERT_PWD)) - { - userec_t u; - passwd_query(num, &u); - if(pwdfd & ALERT_PWD_BADPOST) - cuser.badpost = buf->badpost = u.badpost; - if(pwdfd & ALERT_PWD_GOODPOST) - cuser.goodpost = buf->goodpost = u.goodpost; - if(pwdfd & ALERT_PWD_PERM) - cuser.userlevel = buf->userlevel = u.userlevel; - if(pwdfd & ALERT_PWD_JUSTIFY) - { - memcpy(buf->justify, u.justify, sizeof(u.justify)); - memcpy(cuser.justify, u.justify, sizeof(u.justify)); - memcpy(buf->email, u.email, sizeof(u.email)); - memcpy(cuser.email, u.email, sizeof(u.email)); - } - cuser.numposts += u.numposts - latest_numposts; - currutmp->alerts &= ~ALERT_PWD; - - // ALERT_PWD_RELOAD: reload all! No need to write. - if (pwdfd & ALERT_PWD_RELOAD) - { - memcpy(&cuser, &u, sizeof(u)); - return 0; - } - } + if ((pwdfd = open(fn_passwd, O_WRONLY)) < 0) exit(1); lseek(pwdfd, sizeof(userec_t) * (num - 1), SEEK_SET); write(pwdfd, buf, sizeof(userec_t)); close(pwdfd); -#ifndef _BBS_UTIL_C_ - if (latest_numposts != cuser.numposts) { - sendalert_uid(usernum, ALERT_PWD_POSTS); - latest_numposts = cuser.numposts; - } -#endif return 0; } @@ -145,20 +149,24 @@ passwd_query(int num, userec_t * buf) read(pwdfd, buf, sizeof(userec_t)); close(pwdfd); - if (buf == &cuser) - latest_numposts = cuser.numposts; - return 0; } -int initcuser(const char *userid) +int +passwd_load_user(const char *userid, userec_t *buf) { - // Ptt: setup cuser and usernum here - if(userid[0]=='\0' || - !(usernum = searchuser(userid, NULL)) || usernum > MAX_USERS) + int unum = 0; + + if( !userid || + !userid[0] || + !(unum = searchuser(userid, NULL)) || + unum > MAX_USERS) return -1; - passwd_query(usernum, &cuser); - return usernum; + + if (passwd_query(unum, buf) != 0) + return -1; + + return unum; } int @@ -174,28 +182,6 @@ passwd_apply(void *ctx, int (*fptr) (void *ctx, int, userec_t *)) return 0; } -void -passwd_lock(void) -{ - struct sembuf buf = {0, -1, SEM_UNDO}; - - if (semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} - -void -passwd_unlock(void) -{ - struct sembuf buf = {0, 1, SEM_UNDO}; - - if (semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} - // XXX NOTE: string in plain will be destroyed. int checkpasswd(const char *passwd, char *plain) @@ -265,4 +251,3 @@ logattempt(const char *uid, char type, time4_t now, const char *loghost) } } -#endif diff --git a/daemon/logind/Makefile b/daemon/logind/Makefile index 246b6c5e..7666cf95 100644 --- a/daemon/logind/Makefile +++ b/daemon/logind/Makefile @@ -4,7 +4,7 @@ SRCROOT= ../.. PROGRAMS= logind loginc UTILDIR= $(SRCROOT)/util -UTILOBJ= $(UTILDIR)/util_var.o $(UTILDIR)/util_passwd.o +UTILOBJ= $(UTILDIR)/util_var.o LDLIBS+=$(SRCROOT)/common/bbs/libcmbbs.a \ $(SRCROOT)/common/sys/libcmsys.a \ diff --git a/daemon/logind/logind.c b/daemon/logind/logind.c index 8d7a9e16..4c30a49d 100644 --- a/daemon/logind/logind.c +++ b/daemon/logind/logind.c @@ -832,8 +832,8 @@ auth_user_challenge(login_ctx *ctx) { char *uid = ctx->userid, *passbuf = ctx->passwd; - const char *free_uid = auth_is_free_userid(uid); + userec_t user = {0}; if (free_uid) { @@ -841,18 +841,17 @@ auth_user_challenge(login_ctx *ctx) return AUTH_RESULT_FREEID; } - // reuse cuser - memset(&cuser, 0, sizeof(cuser)); - if( initcuser(uid) < 1 || !cuser.userid[0] || - !checkpasswd(cuser.passwd, passbuf) ) + if (passwd_load_user(uid, &user) < 1 || + !user.userid[0] || + !checkpasswd(user.passwd, passbuf) ) { - if (cuser.userid[0]) - strcpy(uid, cuser.userid); + if (user.userid[0]) + strcpy(uid, user.userid); return AUTH_RESULT_FAIL; } // normalize user id - strcpy(uid, cuser.userid); + strcpy(uid, user.userid); return AUTH_RESULT_OK; } diff --git a/include/cmbbs.h b/include/cmbbs.h index 48d36ed3..7ea98e7a 100644 --- a/include/cmbbs.h +++ b/include/cmbbs.h @@ -51,7 +51,6 @@ extern void remove_from_uhash(int n); extern int dosearchuser(const char *userid, char *rightid); extern int searchuser(const char *userid, char *rightid); extern void setuserid(int num, const char *userid); -extern int getuser(const char *userid, userec_t *xuser); extern userinfo_t *search_ulistn(int uid, int unum); extern userinfo_t *search_ulist_pid(int pid); extern userinfo_t *search_ulist_userid(const char *userid); @@ -81,4 +80,17 @@ extern void add_cooldowntime(int uid, int min); extern void add_posttimes(int uid, int times); # endif +/* passwd */ +extern int passwd_init (void); +extern void passwd_lock (void); +extern void passwd_unlock(void); +extern int passwd_update_money(int num); +extern int passwd_update(int num, userec_t *buf); +extern int passwd_query (int num, userec_t *buf); +extern int passwd_load_user(const char *userid, userec_t *buf); +extern int passwd_apply (void *data, int (*fptr)(void *, int, userec_t *)); +extern int checkpasswd (const char *passwd, char *test); // test will be destroyed +extern void logattempt (const char *uid, char type, time4_t now, const char *fromhost); +extern char*genpasswd (char *pw); + #endif diff --git a/include/proto.h b/include/proto.h index 6716aa30..66226d9d 100644 --- a/include/proto.h +++ b/include/proto.h @@ -115,8 +115,9 @@ unsigned int getutmpmode(void); void setutmpmode(unsigned int mode); void purge_utmp(userinfo_t *uentp); void getnewutmpent(const userinfo_t *up); -int apply_ulist(int (*fptr)(const userinfo_t *)); -char *getuserid(int num); +int apply_ulist(int (*fptr)(const userinfo_t *)); +char*getuserid(int num); +int getuser(const char *userid, userec_t *xuser); int searchnewuser(int mode); int count_logins(int uid, int show); int is_BM_cache(int); @@ -683,27 +684,18 @@ int RcyAddDir (const fileheader_t *fhdr, int bid, const char *direct); int RcyRecycleBin(void); /* passwd */ -int passwd_init(void); -int passwd_update(int num, userec_t *buf); -int passwd_query(int num, userec_t *buf); -int passwd_apply(void *data, int (*fptr)(void *, int, userec_t *)); -void passwd_lock(void); -void passwd_unlock(void); -int passwd_update_money(int num); +int initcuser (const char *userid); void passwd_force_update(int flag); -int initcuser(const char *userid); -int freecuser(void); -int passwd_add_my_numpost(int diff); // temporary hack before new account system ready. -char* genpasswd (char *pw); -int checkpasswd(const char *passwd, char *test); // test will be destroyed -void logattempt (const char *uid, char type, time4_t now, const char *fromhost); +int passwd_sync_update (int num, userec_t * buf); +int passwd_sync_query (int num, userec_t * buf); + +// int passwd_add_my_numpost(int diff); // temporary hack before new account system ready. // current user help utilities int pwcuSetSignature (unsigned char newsig); int pwcuBitSetLevel (unsigned int mask); int pwcuBitUnsetLevel (unsigned int mask); - /* calendar */ int calendar(void); int ParseDate(const char *date, int *year, int *month, int *day); diff --git a/innbbsd/Makefile b/innbbsd/Makefile index ff4497f5..7b368c99 100644 --- a/innbbsd/Makefile +++ b/innbbsd/Makefile @@ -29,7 +29,7 @@ all: ${PROGS} # bbs util UTIL_DIR= $(SRCROOT)/util -UTIL_OBJS= util_passwd.o util_var.o +UTIL_OBJS= util_var.o .for fn in ${UTIL_OBJS} LINK_UTIL_OBJS+= ${UTIL_DIR}/${fn} diff --git a/mbbsd/admin.c b/mbbsd/admin.c index 90676799..ad986097 100644 --- a/mbbsd/admin.c +++ b/mbbsd/admin.c @@ -63,10 +63,10 @@ static int retrieve_backup(userec_t *user) if ((uid = searchuser(user->userid, user->userid))) { userec_t orig; - passwd_query(uid, &orig); + passwd_sync_query(uid, &orig); strlcpy(user->passwd, orig.passwd, sizeof(orig.passwd)); setumoney(uid, user->money); - passwd_update(uid, user); + passwd_sync_update(uid, user); return 0; } diff --git a/mbbsd/angel.c b/mbbsd/angel.c index fa53240f..e2eef9ec 100644 --- a/mbbsd/angel.c +++ b/mbbsd/angel.c @@ -270,7 +270,7 @@ FindAngel(void){ !angel_reject_me(u) && u->userid[0]){ strlcpy(cuser.myangel, u->userid, sizeof(cuser.myangel)); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); return 1; } }while(++trial < 5); diff --git a/mbbsd/assess.c b/mbbsd/assess.c index deacabf2..5fa25f0f 100644 --- a/mbbsd/assess.c +++ b/mbbsd/assess.c @@ -21,7 +21,7 @@ int inc_##_attr(const char *userid, int num) \ int uid = getuser(userid, &xuser);\ if( uid > 0 ){ \ inc(&xuser._attr, num); \ - passwd_update(uid, &xuser); \ + passwd_sync_update(uid, &xuser); \ return xuser._attr; }\ return 0;\ } @@ -51,7 +51,7 @@ void set_assess(const char *userid, unsigned char num, int type) xuser.badsale = num; break; } - passwd_update(uid, &xuser); + passwd_sync_update(uid, &xuser); } #endif @@ -260,7 +260,7 @@ u_fixgoodpost(void) cuser.goodpost = newgp; // update passwd file here? passwd_force_update(ALERT_PWD_GOODPOST); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); vmsgf("更新優文數目為%d。", newgp); } diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c index d9ed7a86..549b46c7 100644 --- a/mbbsd/bbs.c +++ b/mbbsd/bbs.c @@ -232,7 +232,7 @@ save_violatelaw(void) // force overriding alerts if(currutmp) currutmp->alerts &= ~ALERT_PWD_PERM; - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); sendalert(cuser.userid, ALERT_PWD_PERM); log_filef("log/violation", LOG_CREAT, "%s %s pay-violation: $%d complete.\n", @@ -3256,12 +3256,12 @@ del_post(int ent, fileheader_t * fhdr, char *direct) mail_violatelaw(userid, BBSMNAME " 系統警察", "劣文累計 5 篇", "罰單一張"); kick_all(userid); - passwd_query(tusernum, &xuser); + passwd_sync_query(tusernum, &xuser); xuser.money = moneyof(tusernum); xuser.vl_count++; xuser.userlevel |= PERM_VIOLATELAW; xuser.timeviolatelaw = now; - passwd_update(tusernum, &xuser); + passwd_sync_update(tusernum, &xuser); } sendalert(userid, ALERT_PWD_BADPOST); mail_id(userid, genbuf, newpath, cuser.userid); @@ -3323,9 +3323,9 @@ del_post(int ent, fileheader_t * fhdr, char *direct) if (tusernum) { userec_t xuser; - passwd_query(tusernum, &xuser); + passwd_sync_query(tusernum, &xuser); xuser.numposts--; - passwd_update(tusernum, &xuser); + passwd_sync_update(tusernum, &xuser); sendalert_uid(tusernum, ALERT_PWD_POSTS); // TODO alert user? diff --git a/mbbsd/cache.c b/mbbsd/cache.c index ebbf164f..3860dd1f 100644 --- a/mbbsd/cache.c +++ b/mbbsd/cache.c @@ -6,9 +6,21 @@ #endif /* - * section - utmp cache + * section - user & utmp cache */ +int +getuser(const char *userid, userec_t *xuser) +{ + int uid; + + if ((uid = searchuser(userid, NULL))) { + passwd_sync_query(uid, xuser); + xuser->money = moneyof(uid); + } + return uid; +} + void getnewutmpent(const userinfo_t * up) { diff --git a/mbbsd/cal.c b/mbbsd/cal.c index 23520a33..75ba8857 100644 --- a/mbbsd/cal.c +++ b/mbbsd/cal.c @@ -259,9 +259,9 @@ static int inmailbox(int m) { userec_t xuser; - passwd_query(usernum, &xuser); + passwd_sync_query(usernum, &xuser); cuser.exmailbox = xuser.exmailbox + m; - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); return cuser.exmailbox; } diff --git a/mbbsd/chc.c b/mbbsd/chc.c index 4df8e2d7..1b490699 100644 --- a/mbbsd/chc.c +++ b/mbbsd/chc.c @@ -848,9 +848,9 @@ chc(int s, ChessGameMode mode) /* Assume that info->user1 is me. */ info->user1.lose++; count_chess_elo_rating(&info->user1, &info->user2, 0.0); - passwd_query(usernum, &cuser); + passwd_sync_query(usernum, &cuser); chcusr_put(&cuser, &info->user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } if (mode == CHESS_MODE_WATCH) @@ -905,7 +905,7 @@ chc_gameend(ChessInfo* info, ChessGameResult result) } currutmp->chess_elo_rating = user1->rating; chcusr_put(&cuser, user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } else if (info->mode == CHESS_MODE_REPLAY) { free(info->board); free(info->tag); @@ -821,7 +821,7 @@ go_gameend(ChessInfo* info, ChessGameResult result) go_usr_put(&cuser, user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } else if (info->mode == CHESS_MODE_REPLAY) { free(info->board); free(info->tag); @@ -936,9 +936,9 @@ gochess(int s, ChessGameMode mode) if (info->mode == CHESS_MODE_VERSUS) { /* Assume that info->user1 is me. */ info->user1.lose++; - passwd_query(usernum, &cuser); + passwd_sync_query(usernum, &cuser); go_usr_put(&cuser, &info->user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } if (mode == CHESS_MODE_WATCH) diff --git a/mbbsd/gomo.c b/mbbsd/gomo.c index d68a61eb..575d4aee 100644 --- a/mbbsd/gomo.c +++ b/mbbsd/gomo.c @@ -412,7 +412,7 @@ gomo_gameend(ChessInfo* info, ChessGameResult result) gomoku_usr_put(&cuser, user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } else if (info->mode == CHESS_MODE_REPLAY) { free(info->board); free(info->tag); @@ -535,9 +535,9 @@ gomoku(int s, ChessGameMode mode) if (info->mode == CHESS_MODE_VERSUS) { /* Assume that info->user1 is me. */ info->user1.lose++; - passwd_query(usernum, &cuser); + passwd_sync_query(usernum, &cuser); gomoku_usr_put(&cuser, &info->user1); - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } if (mode == CHESS_MODE_WATCH) diff --git a/mbbsd/mail.c b/mbbsd/mail.c index c006bac6..e0185e4a 100644 --- a/mbbsd/mail.c +++ b/mbbsd/mail.c @@ -1385,7 +1385,7 @@ static int mail_nooutmail(int ent GCC_UNUSED, fileheader_t * fhdr GCC_UNUSED, const char *direct GCC_UNUSED) { cuser.uflag2 ^= REJ_OUTTAMAIL; - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); return FULLUPDATE; } diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index 5f27d0bd..4cdf284d 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -227,7 +227,7 @@ u_exit(const char *mode) cuser.badsale = currutmp->badsale; */ - // no need because in later passwd_update will reload money from SHM. + // no need because in later passwd_sync_update will reload money from SHM. // reload_money(); setflags(PAGER_FLAG, currutmp->pager != PAGER_ON); @@ -250,7 +250,7 @@ u_exit(const char *mode) cuser.numlogins = --cuser.numlogins; /* Leeym 上站停留時間限制式 */ } - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); purge_utmp(currutmp); log_usies(mode, NULL); } @@ -1335,7 +1335,7 @@ user_login(void) foreign_warning(); #endif - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); if(cuser.uflag2 & FAVNEW_FLAG) { fav_load(); diff --git a/mbbsd/passwd.c b/mbbsd/passwd.c index 86a347ba..f4b7a654 100644 --- a/mbbsd/passwd.c +++ b/mbbsd/passwd.c @@ -1,74 +1,10 @@ /* $Id$ */ #include "bbs.h" -static int semid = -1; - -#ifndef SEM_R -#define SEM_R 0400 -#endif - -#ifndef SEM_A -#define SEM_A 0200 -#endif - -#ifndef __FreeBSD__ -#include <sys/sem.h> -union semun { - int val; /* value for SETVAL */ - struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ - unsigned short *array; /* array for GETALL & SETALL */ - struct seminfo *__buf; /* buffer for IPC_INFO */ -}; +#ifdef _BBS_UTIL_C_ +#error sorry, mbbsd/passwd.c does not support utility mode anymore. please use libcmbbs instead. #endif -int -passwd_init(void) -{ - semid = semget(PASSWDSEM_KEY, 1, SEM_R | SEM_A | IPC_CREAT | IPC_EXCL); - if (semid == -1) { - if (errno == EEXIST) { - semid = semget(PASSWDSEM_KEY, 1, SEM_R | SEM_A); - if (semid == -1) { - perror("semget"); - exit(1); - } - } else { - perror("semget"); - exit(1); - } - } else { - union semun s; - - s.val = 1; - if (semctl(semid, 0, SETVAL, s) == -1) { - perror("semctl"); - exit(1); - } - } - - return 0; -} - -int -passwd_update_money(int num) -/* update money only - Ptt: don't call it directly, call deumoney() */ -{ - int pwdfd; - int money=moneyof(num); - userec_t u; - if (num < 1 || num > MAX_USERS) - return -1; - - if ((pwdfd = open(fn_passwd, O_WRONLY)) < 0) - exit(1); - lseek(pwdfd, sizeof(userec_t) * (num - 1) + - ((char *)&u.money - (char *)&u), SEEK_SET); - write(pwdfd, &money, sizeof(int)); - close(pwdfd); - return 0; -} - void passwd_force_update(int flag) { @@ -77,24 +13,37 @@ passwd_force_update(int flag) currutmp->alerts &= ~flag; } +int +initcuser(const char *userid) +{ + usernum = passwd_load_user(userid, &cuser); + return usernum; +} + +// XXX I don't like the stupid synchronization here, +// but simply following previous work here... int -passwd_update(int num, userec_t * buf) +passwd_sync_update(int num, userec_t * buf) { - int pwdfd; + int alerts; + if (num < 1 || num > MAX_USERS) return -1; - buf->money = moneyof(num); - if(usernum == num && currutmp && ((pwdfd = currutmp->alerts) & ALERT_PWD)) + + if(usernum == num && currutmp && ((alerts = currutmp->alerts) & ALERT_PWD)) { userec_t u; - passwd_query(num, &u); - if(pwdfd & ALERT_PWD_BADPOST) + buf->money = moneyof(num); + if (passwd_sync_query(num, &u) != 0) + return -1; + + if(alerts & ALERT_PWD_BADPOST) cuser.badpost = buf->badpost = u.badpost; - if(pwdfd & ALERT_PWD_GOODPOST) + if(alerts & ALERT_PWD_GOODPOST) cuser.goodpost = buf->goodpost = u.goodpost; - if(pwdfd & ALERT_PWD_PERM) + if(alerts & ALERT_PWD_PERM) cuser.userlevel = buf->userlevel = u.userlevel; - if(pwdfd & ALERT_PWD_JUSTIFY) + if(alerts & ALERT_PWD_JUSTIFY) { memcpy(buf->justify, u.justify, sizeof(u.justify)); memcpy(cuser.justify, u.justify, sizeof(u.justify)); @@ -105,156 +54,36 @@ passwd_update(int num, userec_t * buf) currutmp->alerts &= ~ALERT_PWD; // ALERT_PWD_RELOAD: reload all! No need to write. - if (pwdfd & ALERT_PWD_RELOAD) + if (alerts & ALERT_PWD_RELOAD) { memcpy(&cuser, &u, sizeof(u)); return 0; } } - if ((pwdfd = open(fn_passwd, O_WRONLY)) < 0) - exit(1); - lseek(pwdfd, sizeof(userec_t) * (num - 1), SEEK_SET); - write(pwdfd, buf, sizeof(userec_t)); - close(pwdfd); -#ifndef _BBS_UTIL_C_ - if (latest_numposts != cuser.numposts) { + if (passwd_sync_update(num, buf) != 0) + return -1; + + if (currutmp && usernum > 0 && + latest_numposts != cuser.numposts) + { sendalert_uid(usernum, ALERT_PWD_POSTS); latest_numposts = cuser.numposts; } -#endif + return 0; } +// XXX I don't like the stupid synchronization here, +// but simply following previous work here... int -passwd_query(int num, userec_t * buf) +passwd_sync_query(int num, userec_t * buf) { - int pwdfd; - if (num < 1 || num > MAX_USERS) + if (passwd_sync_query(num, buf) < 0) return -1; - if ((pwdfd = open(fn_passwd, O_RDONLY)) < 0) - exit(1); - lseek(pwdfd, sizeof(userec_t) * (num - 1), SEEK_SET); - read(pwdfd, buf, sizeof(userec_t)); - close(pwdfd); if (buf == &cuser) latest_numposts = cuser.numposts; return 0; } - -int initcuser(const char *userid) -{ - // Ptt: setup cuser and usernum here - if(userid[0]=='\0' || - !(usernum = searchuser(userid, NULL)) || usernum > MAX_USERS) - return -1; - passwd_query(usernum, &cuser); - return usernum; -} - -int -passwd_apply(void *ctx, int (*fptr) (void *ctx, int, userec_t *)) -{ - int i; - userec_t user; - for (i = 0; i < MAX_USERS; i++) { - passwd_query(i + 1, &user); - if ((*fptr) (ctx, i, &user) < 0) - return -1; - } - return 0; -} - -void -passwd_lock(void) -{ - struct sembuf buf = {0, -1, SEM_UNDO}; - - if (semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} - -void -passwd_unlock(void) -{ - struct sembuf buf = {0, 1, SEM_UNDO}; - - if (semop(semid, &buf, 1)) { - perror("semop"); - exit(1); - } -} - -// XXX NOTE: string in plain will be destroyed. -int -checkpasswd(const char *passwd, char *plain) -{ - int ok; - char *pw; - - ok = 0; - pw = fcrypt(plain, passwd); - if(pw && strcmp(pw, passwd)==0) - ok = 1; - memset(plain, 0, strlen(plain)); - - return ok; -} - -char * -genpasswd(char *pw) -{ - if (pw[0]) { - char saltc[2], c; - int i; - - i = 9 * getpid(); - saltc[0] = i & 077; - saltc[1] = (i >> 6) & 077; - - for (i = 0; i < 2; i++) { - c = saltc[i] + '.'; - if (c > '9') - c += 7; - if (c > 'Z') - c += 6; - saltc[i] = c; - } - return fcrypt(pw, saltc); - } - return ""; -} - - -void -logattempt(const char *uid, char type, time4_t now, const char *loghost) -{ - char fname[PATHLEN]; - int fd, len; - char genbuf[200]; - - snprintf(genbuf, sizeof(genbuf), "%c%-12s[%s] ?@%s\n", type, uid, - Cdate(&now), loghost); - len = strlen(genbuf); - // log to public (BBSHOME) - if ((fd = open(FN_BADLOGIN, O_WRONLY | O_CREAT | O_APPEND, 0644)) > 0) { - write(fd, genbuf, len); - close(fd); - } - // log to user private log - if (type == '-') { - snprintf(genbuf, sizeof(genbuf), - "[%s] %s\n", Cdate(&now), loghost); - len = strlen(genbuf); - sethomefile(fname, uid, FN_BADLOGIN); - if ((fd = open(fname, O_WRONLY | O_CREAT | O_APPEND, 0644)) > 0) { - write(fd, genbuf, len); - close(fd); - } - } -} - diff --git a/mbbsd/register.c b/mbbsd/register.c index 473f2f57..eaa85fb2 100644 --- a/mbbsd/register.c +++ b/mbbsd/register.c @@ -585,7 +585,7 @@ setupnewuser(const userec_t *user) /* 不曉得為什麼要從 2 開始... Ptt:因為SYSOP在1 */ for (uid = 2; uid <= MAX_USERS; uid++) { - passwd_query(uid, &utmp); + passwd_sync_query(uid, &utmp); // tolerate for one year. check_and_expire_account(uid, &utmp, 365*12*60); } @@ -611,7 +611,7 @@ setupnewuser(const userec_t *user) SHM->money[uid - 1] = user->money; - if (passwd_update(uid, (userec_t *)user) == -1) { + if (passwd_sync_update(uid, (userec_t *)user) == -1) { passwd_unlock(); vmsg("客滿了,再見!"); exit(1); @@ -1030,7 +1030,7 @@ check_register(void) { xun = atoi(buf+1); if (xun < 0 || xun >= MAX_USERS || - passwd_query(xun, &u) < 0 || + passwd_sync_query(xun, &u) < 0 || !(u.userlevel & (PERM_ACCOUNTS | PERM_ACCTREG))) memset(&u, 0, sizeof(u)); // now u is valid only if reference is loaded with account sysop. @@ -1532,7 +1532,7 @@ u_register(void) toregister(email); // update cuser - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); return FULLUPDATE; } @@ -1738,7 +1738,7 @@ regform_accept(const char *userid, const char *justify) unlink(buf); // update password file - passwd_update(unum, &muser); + passwd_sync_update(unum, &muser); // alert online users? if (search_ulist(unum)) @@ -1777,7 +1777,7 @@ regform_reject(const char *userid, const char *reason, const RegformEntry *pre) // handle files // update password file - passwd_update(unum, &muser); + passwd_sync_update(unum, &muser); // alert online users? if (search_ulist(unum)) diff --git a/mbbsd/user.c b/mbbsd/user.c index 7ef3578f..0111869c 100644 --- a/mbbsd/user.c +++ b/mbbsd/user.c @@ -35,7 +35,7 @@ kill_user(int num, const char *userid) memset(&u, 0, sizeof(userec_t)); log_usies("KILL", getuserid(num)); setuserid(num, ""); - passwd_update(num, &u); + passwd_sync_update(num, &u); return 0; } int @@ -67,7 +67,7 @@ u_loginview(void) if (pbits != cuser.loginview) { cuser.loginview = pbits; - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); } return 0; } @@ -80,7 +80,7 @@ int u_cancelbadpost(void) if(search_ulistn(usernum,2)) {vmsg("請登出其他視窗, 否則不受理."); return 0;} - passwd_query(usernum, &cuser); + passwd_sync_query(usernum, &cuser); if (currutmp && (currutmp->alerts & ALERT_PWD)) currutmp->alerts &= ~ALERT_PWD; @@ -105,7 +105,7 @@ int u_cancelbadpost(void) { int prev = cuser.badpost--; cuser.timeremovebadpost = now; - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); log_filef("log/cancelbadpost.log", LOG_CREAT, "%s %s 刪除一篇劣文 (%d -> %d 篇)\n", Cdate(&now), cuser.userid, prev, cuser.badpost); @@ -340,7 +340,7 @@ violate_law(userec_t * u, int unum) u->userlevel |= PERM_VIOLATELAW; u->timeviolatelaw = now; u->vl_count++; - passwd_update(unum, u); + passwd_sync_update(unum, u); post_violatelaw(u->userid, cuser.userid, reason, "罰單處份"); mail_violatelaw(u->userid, "站務警察", reason, "罰單處份"); } @@ -527,7 +527,7 @@ void Customize(void) if(dirty) { - passwd_update(usernum, &cuser); + passwd_sync_update(usernum, &cuser); outs("設定已儲存。\n"); } else { outs("結束設定。\n"); @@ -942,7 +942,7 @@ uinfo_query(userec_t *u, int adminmode, int unum) break; } else { userec_t atuser; - passwd_query(uid, &atuser); + passwd_sync_query(uid, &atuser); if (now - atuser.firstlogin < 6 * 30 * 24 * 60 * 60) { outs("\n註冊未超過半年,請重新輸入\n"); i--; @@ -1101,7 +1101,7 @@ uinfo_query(userec_t *u, int adminmode, int unum) setumoney(unum, x.money); } - passwd_update(unum, &x); + passwd_sync_update(unum, &x); if (adminmode) { diff --git a/trans/Makefile b/trans/Makefile index a9622cbd..0ab85855 100644 --- a/trans/Makefile +++ b/trans/Makefile @@ -7,9 +7,9 @@ CFLAGS+= -DPTTBBS_UTIL BBSBASE= $(SRCROOT)/include/var.h -UTIL_OBJS= util_passwd.o util_var.o +UTIL_OBJS= util_var.o -MBBSD_OBJS= record passwd var +MBBSD_OBJS= record var # 下面這些程式, 會被 compile 並且和 $(UTIL_OBJS) 聯結 CPROG_WITH_UTIL= \ diff --git a/util/Makefile b/util/Makefile index 0bbfc0bd..f6f9fb99 100644 --- a/util/Makefile +++ b/util/Makefile @@ -7,9 +7,9 @@ CFLAGS+= -DPTTBBS_UTIL BBSBASE= $(SRCROOT)/include/var.h -UTIL_OBJS= util_passwd.o util_var.o +UTIL_OBJS= util_var.o -MBBSD_OBJS= record passwd var +MBBSD_OBJS= record var # 下面這些程式, 會被 compile 並且和 $(UTIL_OBJS) 聯結 CPROG_WITH_UTIL= \ diff --git a/util/bbsmail.c b/util/bbsmail.c index fa14b493..c2cc732d 100644 --- a/util/bbsmail.c +++ b/util/bbsmail.c @@ -52,7 +52,7 @@ int mail2bbs(char *userid) userec_t xuser; /* check if the userid is in our bbs now */ - if( !(uid = getuser(userid, &xuser)) ){ + if( (uid = passwd_load_user(userid, &xuser)) < 1 ){ sprintf(genbuf, "BBS user <%s> not existed", userid); puts(genbuf); mailog(genbuf); diff --git a/util/chkhbf.c b/util/chkhbf.c index 90c28d56..3574fe1a 100644 --- a/util/chkhbf.c +++ b/util/chkhbf.c @@ -94,7 +94,7 @@ void chkhbf(boardheader_t *bptr) chkuser[i] = 0; break; } - if( !getuser(chkuser, &xuser) || strcmp(chkuser, "guest") == 0 ){ + if( passwd_load_user(chkuser, &xuser) < 1 || strcasecmp(chkuser, STR_GUEST) == 0 ){ strcpy(explist[nEXP].userid, chkuser); explist[nEXP].expire = -1; ++nEXP; diff --git a/util/toplazyBM.c b/util/toplazyBM.c index b9a66c26..e2bc0a6b 100644 --- a/util/toplazyBM.c +++ b/util/toplazyBM.c @@ -86,12 +86,12 @@ int main(int argc, char *argv[]) p = strtok(p,"/ ]"); for(index=0; p && index<5; index++) { int diff; - if(!p[0]) { + // XXX what if bmid is invalid? + if(!p[0] || (bmid = passwd_load_user(p, &xuser)) < 1) { index--; p=strtok(NULL,"/ ]"); continue; } - bmid=getuser(p, &xuser); strlcpy(bms[index].bmname, p, sizeof(bms[index].bmname)); bms[index].flag = 0; |