作者itoc.bbs@cpu.tfcis.org (核心動力) 看板: plan
標題Re: [問題] 大量建帳號的程式
時間動力核心 (2005/08/12 Fri 09:15:45) Updated: 2005/08/12
※ 引述《shadowofsoul (Shadow Of Soul)》之銘言:
> 有人有寫過大量建帳號的function嗎?
> 類似一次建立一個list的帳號及密碼這樣的function..
> 要用於新生的帳號..
: src/util/Makefile
EXE = ... addacct
: src/util/addacct.c 新增此程式
/*-------------------------------------------------------*/
/* util/addacct.c ( NTHU CS MapleBBS Ver 3.10 ) */
/*-------------------------------------------------------*/
/* target : 大量建帳號 */
/* create : 05/08/12 */
/* update : / / */
/* author : itoc.bbs@bbs.tnfsh.tn.edu.tw */
/*-------------------------------------------------------*/
#if 0 /* 使用方式 */
建一個 ~bbs/list 的文字檔,格式為「帳號+空白+密碼」,密碼不可以有空白,例如
z000001 passwd1
z000002 passwd2
zabc passwd3
#endif
#include "bbs.h"
static int
uniq_userno(fd)
int fd;
{
char buf[4096];
int userno, size;
SCHEMA *sp; /* record length 16 可整除 4096 */
userno = 1;
while ((size = read(fd, buf, sizeof(buf))) > 0)
{
sp = (SCHEMA *) buf;
do
{
if (sp->userid[0] == '\0')
{
lseek(fd, -size, SEEK_CUR);
return userno;
}
userno++;
size -= sizeof(SCHEMA);
sp++;
} while (size);
}
return userno;
}
static void
acct_add(userid, passwd)
char *userid, *passwd;
{
int fd;
SCHEMA slot;
char fpath[64];
ACCT acct;
if (!*userid || !*passwd)
return;
if (strlen(userid) > IDLEN || strlen(passwd) > PSWDLEN)
{
printf("ID:%s Pass:%s 帳號或密碼過長\n", userid, passwd);
return;
}
memset(&acct, 0, sizeof(ACCT));
str_ncpy(acct.userid, userid, sizeof(acct.userid));
str_ncpy(acct.passwd, genpasswd(passwd), sizeof(acct.passwd));
str_ncpy(acct.username, userid, sizeof(acct.username));/* userid 當暱稱 */
str_ncpy(acct.realname, userid, sizeof(acct.realname));/* userid 當姓名 */
acct.userlevel = PERM_DEFAULT;
acct.ufo = UFO_DEFAULT_NEW;
acct.numlogins = 1;
sprintf(acct.email, "%s.bbs@%s", acct.userid, MYHOSTNAME);
usr_fpath(fpath, userid, NULL);
if (dashd(fpath))
{
printf("已有此ID: %s\n", userid);
return;
}
/* dispatch unique userno */
acct.tvalid = acct.firstlogin = acct.lastlogin =
acct.tcheck = slot.uptime = time(NULL);
memcpy(slot.userid, userid, IDLEN);
fd = open(FN_SCHEMA, O_RDWR | O_CREAT, 0600);
{
f_exlock(fd);
acct.userno = uniq_userno(fd);
write(fd, &slot, sizeof(slot));
f_unlock(fd);
}
close(fd);
/* create directory */
/* usr_fpath(fpath, userid, NULL); */
mkdir(fpath, 0700);
strcat(fpath, "/@");
mkdir(fpath, 0700);
usr_fpath(fpath, userid, "gem");
mak_links(fpath);
#ifdef MY_FAVORITE
usr_fpath(fpath, userid, "MF");
mkdir(fpath, 0700);
#endif
usr_fpath(fpath, userid, FN_ACCT);
fd = open(fpath, O_WRONLY | O_CREAT, 0600);
write(fd, &acct, sizeof(ACCT));
close(fd);
printf("ID:%s Pass:%s 已建置完成\n", userid, passwd);
}
int
main()
{
char *userid, *passwd, buf[256];
FILE *fp;
chdir(BBSHOME);
if (fp = fopen("list", "r"))
{
while (fgets(buf, sizeof(buf), fp))
{
if (userid = strchr(buf, '\n'))
*userid = '\0';
if (userid = strchr(buf, ' '))
{
*userid = '\0';
passwd = userid + 1;
if (userid = strrchr(passwd, ' '))
passwd = userid + 1;
userid = buf;
acct_add(userid, passwd);
}
}
}
return 0;
}
--
=[﹎:�摃�◣��:﹎ Origin ]|[ ���O���� cpu.tfcis.org ]|[�搟說�﹎:]=
=[﹊:�摃�╱��:﹊ Author ]|[ itoc.Dorm11.NCTU.edu.tw ]|[���插�﹊:]=