Code cleanup

This commit is contained in:
Michiel Broek 2005-08-19 19:46:55 +00:00
parent 829514d215
commit ccc8390ad3
6 changed files with 474 additions and 466 deletions

View File

@ -8,6 +8,15 @@ v0.71.5 18-Aug-2005
mbdiff: mbdiff:
Code cleanup. Code cleanup.
mball:
Code cleanup.
mbstat:
Code cleanup.
mbuser:
Code cleanup.
v0.71.4 12-Aug-2005 - 18-Aug-2005 v0.71.4 12-Aug-2005 - 18-Aug-2005

View File

@ -438,7 +438,7 @@ void Help(void)
printf(" directory.\n"); printf(" directory.\n");
mbse_colour(LIGHTBLUE, BLACK); mbse_colour(LIGHTBLUE, BLACK);
printf("\n Options are:\n\n"); printf("\n Options are:\n\n");
mbse_colour(CYAN, BLAKC); mbse_colour(CYAN, BLACK);
printf(" -quiet Quiet mode\n"); printf(" -quiet Quiet mode\n");
mbse_colour(LIGHTGRAY, BLACK); mbse_colour(LIGHTGRAY, BLACK);
printf("\n"); printf("\n");

View File

@ -41,6 +41,7 @@ static char *months[]={(char *)"Jan",(char *)"Feb",(char *)"Mar",
void count_download(char *, time_t, off_t, char *); void count_download(char *, time_t, off_t, char *);
extern int do_quiet;
void dlcount(void) void dlcount(void)
@ -84,6 +85,8 @@ void dlcount(void)
/* /*
* Check apache logfile * Check apache logfile
*/ */
if (!do_quiet)
printf("Checking WWW downloads\n");
Syslog('+', "Checking WWW downloads"); Syslog('+', "Checking WWW downloads");
while (fgets(temp, PATH_MAX-1, fp)) { while (fgets(temp, PATH_MAX-1, fp)) {
@ -163,6 +166,8 @@ void dlcount(void)
/* /*
* Check apache logfile * Check apache logfile
*/ */
if (!do_quiet)
printf("Checking FTP downloads\n");
Syslog('+', "Checking FTP downloads"); Syslog('+', "Checking FTP downloads");
while (fgets(temp, PATH_MAX-1, fp)) { while (fgets(temp, PATH_MAX-1, fp)) {

View File

@ -4,7 +4,7 @@
* Purpose ...............: Creates allfiles listings * Purpose ...............: Creates allfiles listings
* *
***************************************************************************** *****************************************************************************
* Copyright (C) 1997-2004 * Copyright (C) 1997-2005
* *
* Michiel Broek FIDO: 2:280/2802 * Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10 * Beekmansbos 10
@ -49,13 +49,13 @@ struct tm *l_date; /* Structure for Date */
void ProgName() void ProgName()
{ {
if (do_quiet) if (do_quiet)
return; return;
mbse_colour(15, 0); mbse_colour(WHITE, BLACK);
printf("\nMBALL: MBSE BBS %s Allfiles Listing Creator\n", VERSION); printf("\nMBALL: MBSE BBS %s Allfiles Listing Creator\n", VERSION);
mbse_colour(14, 0); mbse_colour(YELLOW, BLACK);
printf(" %s\n", COPYRIGHT); printf(" %s\n", COPYRIGHT);
} }
@ -95,7 +95,7 @@ void die(int onsig)
Syslog(' ', "MBALL finished in %s", t_elapsed(t_start, t_end)); Syslog(' ', "MBALL finished in %s", t_elapsed(t_start, t_end));
if (!do_quiet) { if (!do_quiet) {
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
printf("\n"); printf("\n");
} }
ExitClient(onsig); ExitClient(onsig);
@ -105,23 +105,23 @@ void die(int onsig)
void Help() void Help()
{ {
do_quiet = FALSE; do_quiet = FALSE;
ProgName(); ProgName();
mbse_colour(11, 0); mbse_colour(LIGHTCYAN, BLACK);
printf("\nUsage: mball [command] <options>\n\n"); printf("\nUsage: mball [command] <options>\n\n");
mbse_colour(9, 0); mbse_colour(LIGHTBLUE, BLACK);
printf(" Commands are:\n\n"); printf(" Commands are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" l list Create allfiles and newfiles lists\n"); printf(" l list Create allfiles and newfiles lists\n");
mbse_colour(9, 0); mbse_colour(LIGHTBLUE, BLACK);
printf("\n Options are:\n\n"); printf("\n Options are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" -q -quiet Quiet mode\n"); printf(" -q -quiet Quiet mode\n");
printf(" -z -zip Create .zip archives\n"); printf(" -z -zip Create .zip archives\n");
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
printf("\n"); printf("\n");
die(MBERR_COMMANDLINE); die(MBERR_COMMANDLINE);
} }
@ -181,7 +181,7 @@ int main(int argc, char **argv)
free(cmd); free(cmd);
if (!do_quiet) { if (!do_quiet) {
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf("\n"); printf("\n");
} }
@ -210,60 +210,60 @@ int main(int argc, char **argv)
void MidLine(char *txt, FILE *fp, int doit) void MidLine(char *txt, FILE *fp, int doit)
{ {
char temp[81]; char temp[81];
int x, y, z; int x, y, z;
if (!doit) if (!doit)
return; return;
z = strlen(txt); z = strlen(txt);
x = 77 - z; x = 77 - z;
x /= 2; x /= 2;
strcpy(temp, ""); strcpy(temp, "");
for (y = 0; y < x; y++) for (y = 0; y < x; y++)
strcat(temp, " "); strcat(temp, " ");
strcat(temp, txt); strcat(temp, txt);
z = strlen(temp); z = strlen(temp);
x = 77 - z; x = 77 - z;
for (y = 0; y < x; y++) for (y = 0; y < x; y++)
strcat(temp, " "); strcat(temp, " ");
fprintf(fp, "%c", 179); fprintf(fp, "%c", 179);
fprintf(fp, "%s", temp); fprintf(fp, "%s", temp);
fprintf(fp, "%c\r\n", 179); fprintf(fp, "%c\r\n", 179);
} }
void TopBox(FILE *fp, int doit) void TopBox(FILE *fp, int doit)
{ {
int y; int y;
if (!doit) if (!doit)
return; return;
fprintf(fp, "\r\n%c", 213); fprintf(fp, "\r\n%c", 213);
for(y = 0; y < 77; y++) for(y = 0; y < 77; y++)
fprintf(fp, "%c", 205); fprintf(fp, "%c", 205);
fprintf(fp, "%c\r\n", 184); fprintf(fp, "%c\r\n", 184);
} }
void BotBox(FILE *fp, int doit) void BotBox(FILE *fp, int doit)
{ {
int y; int y;
if (!doit) if (!doit)
return; return;
fprintf(fp, "%c", 212); fprintf(fp, "%c", 212);
for (y = 0; y < 77; y++) for (y = 0; y < 77; y++)
fprintf(fp, "%c", 205); fprintf(fp, "%c", 205);
fprintf(fp, "%c\r\n\r\n", 190); fprintf(fp, "%c\r\n\r\n", 190);
} }
@ -273,10 +273,8 @@ void Masterlist()
FILE *fp, *np, *pAreas, *pHeader; FILE *fp, *np, *pAreas, *pHeader;
int AreaNr = 0, z, x = 0, New; int AreaNr = 0, z, x = 0, New;
unsigned long AllFiles = 0, AllKBytes = 0, NewFiles = 0, NewKBytes = 0; unsigned long AllFiles = 0, AllKBytes = 0, NewFiles = 0, NewKBytes = 0;
unsigned long AllAreaFiles, AllAreaBytes, popdown, down; unsigned long AllAreaFiles, AllAreaBytes, popdown, down, NewAreaFiles, NewAreaBytes;
unsigned long NewAreaFiles, NewAreaBytes; char *sAreas, temp[81], pop[81];
char *sAreas;
char temp[81], pop[81];
struct _fdbarea *fdb_area = NULL; struct _fdbarea *fdb_area = NULL;
sAreas = calloc(PATH_MAX, sizeof(char)); sAreas = calloc(PATH_MAX, sizeof(char));
@ -287,7 +285,7 @@ void Masterlist()
if(( pAreas = fopen (sAreas, "r")) == NULL) { if(( pAreas = fopen (sAreas, "r")) == NULL) {
WriteError("Can't open File Areas File: %s", sAreas); WriteError("Can't open File Areas File: %s", sAreas);
mbse_colour(7,0); mbse_colour(LIGHTGRAY, BLACK);
die(MBERR_GENERAL); die(MBERR_GENERAL);
} }
fread(&areahdr, sizeof(areahdr), 1, pAreas); fread(&areahdr, sizeof(areahdr), 1, pAreas);
@ -338,7 +336,7 @@ void Masterlist()
Nopper(); Nopper();
if ((fdb_area = mbsedb_OpenFDB(AreaNr, 30)) == 0) { if ((fdb_area = mbsedb_OpenFDB(AreaNr, 30)) == 0) {
WriteError("$Can't open Area %d (%s)! Skipping ...", AreaNr, area.Name); WriteError("Can't open Area %d (%s)! Skipping ...", AreaNr, area.Name);
} else { } else {
popdown = 0; popdown = 0;
while (fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp) == 1) { while (fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp) == 1) {
@ -480,35 +478,36 @@ void Masterlist()
void MakeArc() void MakeArc()
{ {
char *cmd; char *cmd;
if (!getarchiver((char *)"ZIP")) { if (!getarchiver((char *)"ZIP")) {
WriteError("ZIP Archiver not available"); WriteError("ZIP Archiver not available");
return; return;
} }
cmd = xstrcpy(archiver.farc); cmd = xstrcpy(archiver.farc);
if (cmd == NULL) { if (cmd == NULL) {
WriteError("ZIP archive command not available"); WriteError("ZIP archive command not available");
return; return;
} }
Nopper(); Nopper();
if (!do_quiet) if (!do_quiet)
printf("Creating allfiles.zip\n"); printf("Creating allfiles.zip\n");
if (!execute_str(cmd, (char *)"allfiles.zip allfiles.txt", (char *)NULL, (char *)"/dev/null", if (!execute_str(cmd, (char *)"allfiles.zip allfiles.txt", (char *)NULL, (char *)"/dev/null",
(char *)"/dev/null", (char *)"/dev/null") == 0) (char *)"/dev/null", (char *)"/dev/null") == 0)
WriteError("Create allfiles.zip failed"); WriteError("Create allfiles.zip failed");
Nopper(); Nopper();
if (!do_quiet) if (!do_quiet)
printf("Creating newfiles.zip\n"); printf("Creating newfiles.zip\n");
if (!execute_str(cmd, (char *)"newfiles.zip newfiles.txt", (char *)NULL, (char *)"/dev/null", if (!execute_str(cmd, (char *)"newfiles.zip newfiles.txt", (char *)NULL, (char *)"/dev/null",
(char *)"/dev/null", (char *)"/dev/null") == 0) (char *)"/dev/null", (char *)"/dev/null") == 0)
WriteError("Create newfiles.zip failed"); WriteError("Create newfiles.zip failed");
free(cmd);
cmd = NULL; free(cmd);
cmd = NULL;
} }

View File

@ -4,7 +4,7 @@
* Purpose ...............: Change BBS status * Purpose ...............: Change BBS status
* *
***************************************************************************** *****************************************************************************
* Copyright (C) 1997-2004 * Copyright (C) 1997-2005
* *
* Michiel Broek FIDO: 2:280/2802 * Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10 * Beekmansbos 10
@ -41,234 +41,234 @@ time_t t_start, t_end;
void Help(void) void Help(void)
{ {
do_quiet = FALSE; do_quiet = FALSE;
ProgName(); ProgName();
mbse_colour(11, 0); mbse_colour(LIGHTCYAN, BLACK);
printf("\nUsage: mbstat [command] <options>\n\n"); printf("\nUsage: mbstat [command] <options>\n\n");
mbse_colour(9, 0); mbse_colour(LIGHTBLUE, BLACK);
printf(" Commands are:\n\n"); printf(" Commands are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" c close Close the BBS for users\n"); printf(" c close Close the BBS for users\n");
printf(" o open Open the BBS for users\n"); printf(" o open Open the BBS for users\n");
printf(" s set semafore Set named semafore\n"); printf(" s set semafore Set named semafore\n");
printf(" w wait Wait until the BBS is free\n\n"); printf(" w wait Wait until the BBS is free\n\n");
mbse_colour(9,0); mbse_colour(LIGHTBLUE, BLACK);
printf(" Options are:\n\n"); printf(" Options are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" -q -quiet Quiet, no screen output\n"); printf(" -q -quiet Quiet, no screen output\n");
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
die(MBERR_COMMANDLINE); die(MBERR_COMMANDLINE);
} }
void ProgName(void) void ProgName(void)
{ {
if (do_quiet) if (do_quiet)
return; return;
mbse_colour(15, 0); mbse_colour(WHITE, BLACK);
printf("\nMBSTAT: MBSE BBS %s Status Changer\n", VERSION); printf("\nMBSTAT: MBSE BBS %s Status Changer\n", VERSION);
mbse_colour(14, 0); mbse_colour(YELLOW, BLACK);
printf(" %s\n", COPYRIGHT); printf(" %s\n", COPYRIGHT);
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
} }
void die(int onsig) void die(int onsig)
{ {
signal(onsig, SIG_IGN); signal(onsig, SIG_IGN);
if (onsig) if (onsig)
WriteError("Terminated on signal %d", onsig); WriteError("Terminated on signal %d", onsig);
if (!do_quiet) { if (!do_quiet) {
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
printf("\n"); printf("\n");
} }
t_end = time(NULL); t_end = time(NULL);
Syslog(' ', "MBSTAT finished in %s", t_elapsed(t_start, t_end)); Syslog(' ', "MBSTAT finished in %s", t_elapsed(t_start, t_end));
ExitClient(onsig); ExitClient(onsig);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i; int i;
char *cmd, *semafore = NULL; char *cmd, *semafore = NULL;
int do_open = FALSE; int do_open = FALSE;
int do_close = FALSE; int do_close = FALSE;
int do_wait = FALSE; int do_wait = FALSE;
int do_sema = FALSE; int do_sema = FALSE;
struct passwd *pw; struct passwd *pw;
InitConfig(); InitConfig();
mbse_TermInit(1, 80, 24); mbse_TermInit(1, 80, 24);
t_start = time(NULL); t_start = time(NULL);
/* /*
* Catch or ignore signals * Catch or ignore signals
*/ */
for (i = 0; i < NSIG; i++) { for (i = 0; i < NSIG; i++) {
if ((i == SIGHUP) || (i == SIGINT) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV) || (i == SIGTERM)) if ((i == SIGHUP) || (i == SIGINT) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV) || (i == SIGTERM))
signal(i, (void (*))die); signal(i, (void (*))die);
else if ((i != SIGKILL) && (i != SIGSTOP)) else if ((i != SIGKILL) && (i != SIGSTOP))
signal(i, SIG_IGN); signal(i, SIG_IGN);
}
cmd = xstrcpy((char *)"Command line: mbstat");
for (i = 1; i < argc; i++) {
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
if (!strncmp(tl(argv[i]), "w", 1))
do_wait = TRUE;
if (!strncmp(tl(argv[i]), "o", 1))
do_open = TRUE;
if (!strncmp(tl(argv[i]), "c", 1))
do_close = TRUE;
if (!strncmp(tl(argv[i]), "s", 1)) {
do_sema = TRUE;
i++;
semafore = xstrcpy(tl(argv[i]));
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
} }
if (!strncmp(tl(argv[i]), "-q", 2))
do_quiet = TRUE;
}
cmd = xstrcpy((char *)"Command line: mbstat"); ProgName();
pw = getpwuid(getuid());
for (i = 1; i < argc; i++) { InitClient(pw->pw_name, (char *)"mbstat", CFG.location, CFG.logfile,
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
if (!strncmp(tl(argv[i]), "w", 1))
do_wait = TRUE;
if (!strncmp(tl(argv[i]), "o", 1))
do_open = TRUE;
if (!strncmp(tl(argv[i]), "c", 1))
do_close = TRUE;
if (!strncmp(tl(argv[i]), "s", 1)) {
do_sema = TRUE;
i++;
semafore = xstrcpy(tl(argv[i]));
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, tl(argv[i]));
}
if (!strncmp(tl(argv[i]), "-q", 2))
do_quiet = TRUE;
}
ProgName();
pw = getpwuid(getuid());
InitClient(pw->pw_name, (char *)"mbstat", CFG.location, CFG.logfile,
CFG.util_loglevel, CFG.error_log, CFG.mgrlog, CFG.debuglog); CFG.util_loglevel, CFG.error_log, CFG.mgrlog, CFG.debuglog);
Syslog(' ', " "); Syslog(' ', " ");
Syslog(' ', "MBSTAT v%s", VERSION); Syslog(' ', "MBSTAT v%s", VERSION);
Syslog(' ', cmd); Syslog(' ', cmd);
free(cmd); free(cmd);
if (!do_quiet) { if (!do_quiet) {
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf("\n"); printf("\n");
} }
if (do_close) if (do_close)
do_open = FALSE; do_open = FALSE;
if (do_open) { if (do_open) {
Open(); Open();
do_close = FALSE; do_close = FALSE;
do_wait = FALSE; do_wait = FALSE;
} }
if (do_close) if (do_close)
Close(); Close();
if (do_wait) if (do_wait)
Wait(); Wait();
if (do_sema) if (do_sema)
Semafore(semafore); Semafore(semafore);
if (!(do_open || do_close || do_wait || do_sema)) if (!(do_open || do_close || do_wait || do_sema))
Help(); Help();
die(MBERR_OK); die(MBERR_OK);
return 0; return 0;
} }
int Semafore(char *semafore) int Semafore(char *semafore)
{ {
char buf[81]; char buf[81];
strcpy(buf, SockR("SECR:1,%s;", semafore)); strcpy(buf, SockR("SECR:1,%s;", semafore));
if (strncmp(buf, "100:0;", 6) == 0) { if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "Semafore \"%s\" is set", semafore); Syslog('+', "Semafore \"%s\" is set", semafore);
if (!do_quiet) if (!do_quiet)
printf("Semafore \"%s\" is set\n", semafore); printf("Semafore \"%s\" is set\n", semafore);
return TRUE; return TRUE;
} else { } else {
Syslog('+', "Failed to set \"%s\" semafore", semafore); Syslog('+', "Failed to set \"%s\" semafore", semafore);
printf("Failed to set \"%s\" semafore\n", semafore); printf("Failed to set \"%s\" semafore\n", semafore);
return FALSE; return FALSE;
} }
} }
int Close(void) int Close(void)
{ {
char buf[81]; char buf[81];
strcpy(buf, SockR("SCLO:0;")); strcpy(buf, SockR("SCLO:0;"));
if (strncmp(buf, "100:0;", 6) == 0) { if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is closed"); Syslog('+', "The BBS is closed");
if (!do_quiet) if (!do_quiet)
printf("The BBS is closed\n"); printf("The BBS is closed\n");
return TRUE; return TRUE;
} else { } else {
printf("Failed to close the BBS\n"); printf("Failed to close the BBS\n");
return FALSE; return FALSE;
} }
} }
int Open(void) int Open(void)
{ {
char buf[81]; char buf[81];
strcpy(buf, SockR("SOPE:0;")); strcpy(buf, SockR("SOPE:0;"));
if (strncmp(buf, "100:0;", 6) == 0) { if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is open"); Syslog('+', "The BBS is open");
if (!do_quiet) if (!do_quiet)
printf("The BBS is open\n"); printf("The BBS is open\n");
return TRUE; return TRUE;
} else { } else {
printf("Failed to open the BBS\n"); printf("Failed to open the BBS\n");
return FALSE; return FALSE;
} }
} }
int Wait(void) int Wait(void)
{ {
int Waiting = 3600; int Waiting = 3600;
char buf[PATH_MAX]; char buf[PATH_MAX];
if (IsSema((char *)"upsdown")) if (IsSema((char *)"upsdown"))
Waiting = 30; Waiting = 30;
Syslog('+', "Waiting for the BBS to become free, timeout %d seconds", Waiting); Syslog('+', "Waiting for the BBS to become free, timeout %d seconds", Waiting);
while (Waiting) { while (Waiting) {
strcpy(buf, SockR("SFRE:0;")); strcpy(buf, SockR("SFRE:0;"));
if (strncmp(buf, "100:0;", 6) == 0) { if (strncmp(buf, "100:0;", 6) == 0) {
Syslog('+', "The BBS is free"); Syslog('+', "The BBS is free");
if (!do_quiet) if (!do_quiet)
printf("The BBS is free. \n"); printf("The BBS is free. \n");
return TRUE; return TRUE;
}
if (!do_quiet) {
buf[strlen(buf) -1] = '\0';
printf("\r%s\r", buf+6);
fflush(stdout);
}
sleep(1);
Waiting--;
} }
if (!do_quiet) {
buf[strlen(buf) -1] = '\0';
printf("\r%s\r", buf+6);
fflush(stdout);
}
sleep(1);
Waiting--;
}
WriteError("Wait for BBS free timeout, aborting"); WriteError("Wait for BBS free timeout, aborting");
return FALSE; return FALSE;
} }

View File

@ -4,7 +4,7 @@
* Purpose ...............: User Pack Util * Purpose ...............: User Pack Util
* *
***************************************************************************** *****************************************************************************
* Copyright (C) 1997-2004 * Copyright (C) 1997-2005
* *
* Michiel Broek FIDO: 2:280/2802 * Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10 * Beekmansbos 10
@ -111,7 +111,7 @@ int main(int argc, char **argv)
oldmask = umask(027); oldmask = umask(027);
if (!do_quiet) if (!do_quiet)
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
UserPack(Days, Level, pack); UserPack(Days, Level, pack);
umask(oldmask); umask(oldmask);
@ -120,7 +120,7 @@ int main(int argc, char **argv)
Syslog(' ', "MBUSER finished in %s", t_elapsed(t_start, t_end)); Syslog(' ', "MBUSER finished in %s", t_elapsed(t_start, t_end));
if (!do_quiet) if (!do_quiet)
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
ExitClient(MBERR_OK); ExitClient(MBERR_OK);
return 0; return 0;
} }
@ -132,38 +132,38 @@ int main(int argc, char **argv)
*/ */
void ProgName(void) void ProgName(void)
{ {
if (do_quiet) if (do_quiet)
return; return;
mbse_colour(15, 0); mbse_colour(WHITE, BLACK);
printf("\nMBUSER: MBSE BBS %s - User maintenance utility\n", VERSION); printf("\nMBUSER: MBSE BBS %s - User maintenance utility\n", VERSION);
mbse_colour(14, 0); mbse_colour(YELLOW, BLACK);
printf(" %s\n\n", COPYRIGHT); printf(" %s\n\n", COPYRIGHT);
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
} }
void Help(void) void Help(void)
{ {
do_quiet = FALSE; do_quiet = FALSE;
ProgName(); ProgName();
mbse_colour(11, 0); mbse_colour(LIGHTCYAN, BLACK);
printf("\nUsage: mbuser [commands] <options>\n\n"); printf("\nUsage: mbuser [commands] <options>\n\n");
mbse_colour(9, 0); mbse_colour(LIGHTBLUE, BLACK);
printf(" Commands are:\n\n"); printf(" Commands are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" kill [n] [l] Kill users not called in \"n\" days below level \"l\"\n"); printf(" kill [n] [l] Kill users not called in \"n\" days below level \"l\"\n");
printf(" pack Pack the userbase\n"); printf(" pack Pack the userbase\n");
mbse_colour(9, 0); mbse_colour(LIGHTBLUE, BLACK);
printf("\n Options are:\n\n"); printf("\n Options are:\n\n");
mbse_colour(3, 0); mbse_colour(CYAN, BLACK);
printf(" -quiet Quiet mode, (no screen output)\n\n"); printf(" -quiet Quiet mode, (no screen output)\n\n");
mbse_colour(7, 0); mbse_colour(LIGHTGRAY, BLACK);
printf("\n"); printf("\n");
ExitClient(MBERR_COMMANDLINE); ExitClient(MBERR_COMMANDLINE);
} }
@ -173,224 +173,219 @@ void Help(void)
*/ */
void UserPack(int days, int level, int pack) void UserPack(int days, int level, int pack)
{ {
FILE *fin, *fout; FILE *fin, *fout;
char *fnin, *fnout; char *fnin, *fnout, *cmd;
long oldsize, curpos; long oldsize, curpos;
int updated, delete = 0, rc, highest = 0, record = 0, sysop = FALSE; int updated, delete = 0, rc, highest = 0, record = 0, sysop = FALSE;
time_t Last; time_t Last;
char *cmd;
fnin = calloc(PATH_MAX, sizeof(char)); fnin = calloc(PATH_MAX, sizeof(char));
fnout = calloc(PATH_MAX, sizeof(char)); fnout = calloc(PATH_MAX, sizeof(char));
sprintf(fnin, "%s/etc/users.data", getenv("MBSE_ROOT")); sprintf(fnin, "%s/etc/users.data", getenv("MBSE_ROOT"));
sprintf(fnout, "%s/etc/users.temp", getenv("MBSE_ROOT")); sprintf(fnout, "%s/etc/users.temp", getenv("MBSE_ROOT"));
/* /*
* First copy the users database, all packing will be done * First copy the users database, all packing will be done
* on a the copy. * on a the copy.
*/ */
if ((fin = fopen(fnin, "r")) == NULL) { if ((fin = fopen(fnin, "r")) == NULL) {
WriteError("Can't open %s", fnin); WriteError("$Can't open %s", fnin);
free(fnin); free(fnin);
free(fnout); free(fnout);
return; return;
} }
if ((fout = fopen(fnout, "w+")) == NULL) { if ((fout = fopen(fnout, "w+")) == NULL) {
WriteError("Can't create %s", fnout); WriteError("$Can't create %s", fnout);
fclose(fin);
free(fnin);
free(fnout);
return;
}
fread(&usrhdr, sizeof(usrhdr), 1, fin);
oldsize = usrhdr.recsize;
updated = FALSE;
/*
* First count records and blanks at the end. Check if the sysop name
* in the main configuration exists in the userdatabase.
*/
while (fread(&usr, oldsize, 1,fin) == 1) {
delete++;
if (!usr.Deleted && strlen(usr.sUserName)) {
highest = (ftell(fin) / oldsize);
if (!strcmp(usr.sUserName, CFG.sysop_name) && !strcmp(usr.Name, CFG.sysop))
sysop = TRUE;
}
}
if (highest != delete) {
Syslog('+', "Blank records at the end, truncating userbase");
updated = TRUE;
}
if (!sysop)
WriteError("No valid Sysop Fidoname and/or Unixname found in userbase, check setup!");
fseek(fin, usrhdr.hdrsize, SEEK_SET);
if (oldsize != sizeof(usr)) {
updated = TRUE;
Syslog('+', "Userbase recordsize is changed, making update");
}
usrhdr.hdrsize = sizeof(usrhdr);
usrhdr.recsize = sizeof(usr);
fwrite(&usrhdr, sizeof(usrhdr), 1, fout);
/*
* The datarecord is filled with zero's before each read
* so that if the record format changed, the new fields will
* be empty by default. The blank records at the end of the
* database are dropped.
*/
memset(&usr, 0, sizeof(usr));
while (fread(&usr, oldsize, 1,fin) == 1) {
record++;
fwrite(&usr, sizeof(usr), 1, fout);
memset(&usr, 0, sizeof(usr));
if (CFG.slow_util && do_quiet)
msleep(1);
Nopper();
}
fclose(fin); fclose(fin);
delete = 0; free(fnin);
free(fnout);
return;
}
fread(&usrhdr, sizeof(usrhdr), 1, fin);
oldsize = usrhdr.recsize;
updated = FALSE;
/* /*
* Handle packing for days below level * First count records and blanks at the end. Check if the sysop name
*/ * in the main configuration exists in the userdatabase.
if ((days) && (level)) { */
fseek(fout, sizeof(usrhdr), SEEK_SET); while (fread(&usr, oldsize, 1,fin) == 1) {
curpos = sizeof(usrhdr); delete++;
if (!usr.Deleted && strlen(usr.sUserName)) {
highest = (ftell(fin) / oldsize);
if (!strcmp(usr.sUserName, CFG.sysop_name) && !strcmp(usr.Name, CFG.sysop))
sysop = TRUE;
}
}
if (highest != delete) {
Syslog('+', "Blank records at the end, truncating userbase");
updated = TRUE;
}
if (!sysop)
WriteError("No valid Sysop Fidoname and/or Unixname found in userbase, check setup!");
while (fread(&usr, sizeof(usr), 1, fout) == 1) { fseek(fin, usrhdr.hdrsize, SEEK_SET);
/*
* New users don't have the last login date set yet,
* use the registration date instead.
*/
if (usr.iTotalCalls == 0)
Last = usr.tFirstLoginDate;
else
Last = usr.tLastLoginDate;
/* if (oldsize != sizeof(usr)) {
* Wow, killing on the second exact!. Don't kill updated = TRUE;
* the guest accounts. Syslog('+', "Userbase recordsize is changed, making update");
*/ }
if ((((t_start - Last) / 86400) > days) &&
(usr.Security.level < level) && (!usr.Guest) && usrhdr.hdrsize = sizeof(usrhdr);
usrhdr.recsize = sizeof(usr);
fwrite(&usrhdr, sizeof(usrhdr), 1, fout);
/*
* The datarecord is filled with zero's before each read
* so that if the record format changed, the new fields will
* be empty by default. The blank records at the end of the
* database are dropped.
*/
memset(&usr, 0, sizeof(usr));
while (fread(&usr, oldsize, 1,fin) == 1) {
record++;
fwrite(&usr, sizeof(usr), 1, fout);
memset(&usr, 0, sizeof(usr));
if (CFG.slow_util && do_quiet)
msleep(1);
Nopper();
}
fclose(fin);
delete = 0;
/*
* Handle packing for days below level
*/
if (days && level) {
fseek(fout, sizeof(usrhdr), SEEK_SET);
curpos = sizeof(usrhdr);
while (fread(&usr, sizeof(usr), 1, fout) == 1) {
/*
* New users don't have the last login date set yet,
* use the registration date instead.
*/
if (usr.iTotalCalls == 0)
Last = usr.tFirstLoginDate;
else
Last = usr.tLastLoginDate;
/*
* Wow, killing on the second exact!. Don't kill the guest accounts.
*/
if ((((t_start - Last) / 86400) > days) && (usr.Security.level < level) && (!usr.Guest) &&
(usr.sUserName[0] != '\0') && (!usr.NeverDelete)) { (usr.sUserName[0] != '\0') && (!usr.NeverDelete)) {
Syslog('+', "Mark user %s", usr.sUserName); Syslog('+', "Mark user %s", usr.sUserName);
if (!do_quiet) { if (!do_quiet) {
printf("Mark user %s\n", usr.sUserName); printf("Mark user %s\n", usr.sUserName);
fflush(stdout); fflush(stdout);
}
delete++;
updated = TRUE;
fseek(fout, - sizeof(usr), SEEK_CUR);
/*
* Just mark for deletion
*/
usr.Deleted = TRUE;
fwrite(&usr, sizeof(usr), 1, fout);
}
if (CFG.slow_util && do_quiet)
msleep(1);
} }
Syslog('+', "Marked %d users to delete", delete); delete++;
} updated = TRUE;
fseek(fout, - sizeof(usr), SEEK_CUR);
/*
* Just mark for deletion
*/
usr.Deleted = TRUE;
fwrite(&usr, sizeof(usr), 1, fout);
}
if (CFG.slow_util && do_quiet)
msleep(1);
}
Syslog('+', "Marked %d users to delete", delete);
}
/* /*
* Pack the userbase if told so * Pack the userbase if told so
*/ */
if (pack) { if (pack) {
Syslog('+', "Packing userbase"); Syslog('+', "Packing userbase");
delete = 0; delete = 0;
fseek(fout, sizeof(usrhdr), SEEK_SET); fseek(fout, sizeof(usrhdr), SEEK_SET);
while (fread(&usr, sizeof(usr), 1, fout) == 1) { while (fread(&usr, sizeof(usr), 1, fout) == 1) {
if (CFG.slow_util && do_quiet) if (CFG.slow_util && do_quiet)
msleep(1); msleep(1);
Nopper(); Nopper();
if (usr.Deleted) { if (usr.Deleted) {
if (!do_quiet) { if (!do_quiet) {
printf("Delete user %s\n", usr.Name); printf("Delete user %s\n", usr.Name);
fflush(stdout); fflush(stdout);
} }
if (usr.Name[0] != '\0') { if (usr.Name[0] != '\0') {
if ((setuid(0) == -1) || (setgid(0) == -1)) { if ((setuid(0) == -1) || (setgid(0) == -1)) {
WriteError("Cannot setuid(root) or setgid(root)"); WriteError("Cannot setuid(root) or setgid(root)");
WriteError("Cannot delete unix account %s", usr.Name); WriteError("Cannot delete unix account %s", usr.Name);
} else { } else {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
rc = execute_str((char *)"/usr/sbin/userdel ", usr.Name, NULL, rc = execute_str((char *)"/usr/sbin/userdel ", usr.Name, NULL,
(char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null"); (char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null");
#else #else
rc = execute_str((char *)"/usr/sbin/pw userdel ", usr.Name, NULL, rc = execute_str((char *)"/usr/sbin/pw userdel ", usr.Name, NULL,
(char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null"); (char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null");
#endif #endif
#ifdef _VPOPMAIL_PATH #ifdef _VPOPMAIL_PATH
cmd = xstrcpy((char *)_VPOPMAIL_PATH); cmd = xstrcpy((char *)_VPOPMAIL_PATH);
cmd = xstrcat(cmd, (char *)"/vdeluser "); cmd = xstrcat(cmd, (char *)"/vdeluser ");
rc = execute_str(cmd, usr.Name, NULL, rc = execute_str(cmd, usr.Name, NULL, (char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null");
(char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null"); free(cmd);
free(cmd);
#endif #endif
if (chdir(CFG.bbs_usersdir) == 0) { if (chdir(CFG.bbs_usersdir) == 0) {
cmd = xstrcpy((char *)"-Rf "); cmd = xstrcpy((char *)"-Rf ");
cmd = xstrcat(cmd, usr.Name); cmd = xstrcat(cmd, usr.Name);
rc = execute_pth((char *)"rm", cmd, rc = execute_pth((char *)"rm", cmd, (char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null");
(char *)"/dev/null",(char *)"/dev/null",(char *)"/dev/null"); free(cmd);
free(cmd);
}
}
}
fseek(fout, - sizeof(usr), SEEK_CUR);
/*
* Blank the deleted records for reuse.
*/
memset(&usr, 0, sizeof(usr));
fwrite(&usr, sizeof(usr), 1, fout);
delete++;
updated = TRUE;
} }
}
} }
Syslog('+', "Deleted %d records", delete);
}
if (updated) { fseek(fout, - sizeof(usr), SEEK_CUR);
/* /*
* Copy file back to the original file, truncate any * Blank the deleted records for reuse.
* deleted records at the end.
*/ */
fseek(fout, 0, SEEK_SET); memset(&usr, 0, sizeof(usr));
if ((fin = fopen(fnin, "w")) == NULL) { fwrite(&usr, sizeof(usr), 1, fout);
WriteError("Can't open %s", fnin); delete++;
free(fnin); updated = TRUE;
free(fnout); }
return;
}
fread(&usrhdr, sizeof(usrhdr), 1, fout);
fwrite(&usrhdr, sizeof(usrhdr), 1, fin);
record = 0;
while (fread(&usr, sizeof(usr), 1,fout) == 1) {
Nopper();
record++;
fwrite(&usr, sizeof(usr), 1, fin);
if (record >= highest)
break;
}
fclose(fin);
fclose(fout);
chmod(fnin, 0660);
Syslog('+', "Userbase is updated, written %d records", record);
} else {
fclose(fout);
} }
Syslog('+', "Deleted %d records", delete);
}
unlink(fnout); if (updated) {
free(fnin); /*
free(fnout); * Copy file back to the original file, truncate any
* deleted records at the end.
*/
fseek(fout, 0, SEEK_SET);
if ((fin = fopen(fnin, "w")) == NULL) {
WriteError("$Can't open %s", fnin);
free(fnin);
free(fnout);
return;
}
fread(&usrhdr, sizeof(usrhdr), 1, fout);
fwrite(&usrhdr, sizeof(usrhdr), 1, fin);
record = 0;
while (fread(&usr, sizeof(usr), 1,fout) == 1) {
Nopper();
record++;
fwrite(&usr, sizeof(usr), 1, fin);
if (record >= highest)
break;
}
fclose(fin);
fclose(fout);
chmod(fnin, 0660);
Syslog('+', "Userbase is updated, written %d records", record);
} else {
fclose(fout);
}
unlink(fnout);
free(fnin);
free(fnout);
} }