More robust bluewave unpacking/packing, added file_id.diz importing for zip files

This commit is contained in:
Andrew Pamment 2016-12-07 10:43:39 +10:00
parent 3bb76b6b5f
commit d568da0b65
5 changed files with 218 additions and 44 deletions

53
bbs.c
View File

@ -10,6 +10,8 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/time.h> #include <sys/time.h>
#include <stdarg.h> #include <stdarg.h>
#include <fts.h>
#include <errno.h>
#include "bbs.h" #include "bbs.h"
#include "lua/lua.h" #include "lua/lua.h"
#include "lua/lualib.h" #include "lua/lualib.h"
@ -800,3 +802,54 @@ void runbbs_ssh(char *ip) {
} }
runbbs_real(-1, ip, 1); runbbs_real(-1, ip, 1);
} }
int recursive_delete(const char *dir) {
int ret = 0;
FTS *ftsp = NULL;
FTSENT *curr;
char *files[] = { (char *) dir, NULL };
ftsp = fts_open(files, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL);
if (!ftsp) {
dolog("%s: fts_open failed: %s", dir, strerror(errno));
ret = -1;
goto finish;
}
while ((curr = fts_read(ftsp))) {
switch (curr->fts_info) {
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
dolog("%s: fts_read error: %s", curr->fts_accpath, strerror(curr->fts_errno));
break;
case FTS_DC:
case FTS_DOT:
case FTS_NSOK:
break;
case FTS_D:
break;
case FTS_DP:
case FTS_F:
case FTS_SL:
case FTS_SLNONE:
case FTS_DEFAULT:
if (remove(curr->fts_accpath) < 0) {
dolog("%s: Failed to remove: %s", curr->fts_path, strerror(errno));
ret = -1;
}
break;
}
}
finish:
if (ftsp) {
fts_close(ftsp);
}
return ret;
}

1
bbs.h
View File

@ -174,6 +174,7 @@ struct msg_headers {
int msg_count; int msg_count;
}; };
extern int recursive_delete(const char *dir);
extern void automessage_write(struct user_record *user); extern void automessage_write(struct user_record *user);
extern void automessage_display(); extern void automessage_display();
extern void dolog(char *fmt, ...); extern void dolog(char *fmt, ...);

View File

@ -228,15 +228,21 @@ void bwave_create_packet() {
mkdir(buffer, 0755); mkdir(buffer, 0755);
} }
snprintf(buffer, 1024, "%s/node%d/bwave/", conf.bbs_path, mynode);
snprintf(buffer, 1024, "%s/node%d/%s.FTI", conf.bbs_path, mynode, conf.bwave_name); if (stat(buffer, &s) == 0) {
recursive_delete(buffer);
}
mkdir(buffer, 0755);
snprintf(buffer, 1024, "%s/node%d/bwave/%s.FTI", conf.bbs_path, mynode, conf.bwave_name);
fti_file = fopen(buffer, "wb"); fti_file = fopen(buffer, "wb");
snprintf(buffer, 1024, "%s/node%d/%s.MIX", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave/%s.MIX", conf.bbs_path, mynode, conf.bwave_name);
mix_file = fopen(buffer, "wb"); mix_file = fopen(buffer, "wb");
snprintf(buffer, 1024, "%s/node%d/%s.DAT", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave/%s.DAT", conf.bbs_path, mynode, conf.bwave_name);
dat_file = fopen(buffer, "wb"); dat_file = fopen(buffer, "wb");
s_printf("\r\n"); s_printf("\r\n");
@ -307,7 +313,7 @@ void bwave_create_packet() {
fclose(mix_file); fclose(mix_file);
fclose(fti_file); fclose(fti_file);
snprintf(buffer, 1024, "%s/node%d/%s.INF", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave/%s.INF", conf.bbs_path, mynode, conf.bwave_name);
inf_file = fopen(buffer, "wb"); inf_file = fopen(buffer, "wb");
fwrite(&hdr, sizeof(INF_HEADER), 1, inf_file); fwrite(&hdr, sizeof(INF_HEADER), 1, inf_file);
@ -337,7 +343,7 @@ void bwave_create_packet() {
sprintf(&buffer[bpos], "%s", archive); sprintf(&buffer[bpos], "%s", archive);
bpos = strlen(buffer); bpos = strlen(buffer);
} else if (conf.zip_cmd[i] == 'f') { } else if (conf.zip_cmd[i] == 'f') {
sprintf(&buffer[bpos], "%s/node%d/%s.INF %s/node%d/%s.MIX %s/node%d/%s.FTI %s/node%d/%s.DAT", conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name); sprintf(&buffer[bpos], "%s/node%d/bwave/%s.INF %s/node%d/bwave/%s.MIX %s/node%d/bwave/%s.FTI %s/node%d/bwave/%s.DAT", conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name, conf.bbs_path, mynode, conf.bwave_name);
bpos = strlen(buffer); bpos = strlen(buffer);
} else if (conf.zip_cmd[i] == '*') { } else if (conf.zip_cmd[i] == '*') {
buffer[bpos++] = '*'; buffer[bpos++] = '*';
@ -360,14 +366,9 @@ void bwave_create_packet() {
tcsetattr(STDIN_FILENO, TCSANOW, &oldit); tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot); tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
} }
snprintf(buffer, 1024, "%s/node%d/%s.FTI", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave", conf.bbs_path, mynode);
unlink(buffer); recursive_delete(buffer);
snprintf(buffer, 1024, "%s/node%d/%s.MIX", conf.bbs_path, mynode, conf.bwave_name);
unlink(buffer);
snprintf(buffer, 1024, "%s/node%d/%s.DAT", conf.bbs_path, mynode, conf.bwave_name);
unlink(buffer);
snprintf(buffer, 1024, "%s/node%d/%s.INF", conf.bbs_path, mynode, conf.bwave_name);
unlink(buffer);
unlink(archive); unlink(archive);
gUser->bwavepktno++; gUser->bwavepktno++;
if (gUser->bwavepktno > 999) { if (gUser->bwavepktno > 999) {
@ -565,7 +566,12 @@ void bwave_upload_reply() {
mkdir(buffer, 0755); mkdir(buffer, 0755);
} }
snprintf(buffer, 1024, "%s/node%d/", conf.bbs_path, mynode); snprintf(buffer, 1024, "%s/node%d/bwave/", conf.bbs_path, mynode);
if (stat(buffer, &s) == 0) {
recursive_delete(buffer);
}
mkdir(buffer, 0755);
upload_zmodem(gUser, buffer); upload_zmodem(gUser, buffer);
@ -577,7 +583,7 @@ void bwave_upload_reply() {
sprintf(&buffer[bpos], "%s", upload_filename); sprintf(&buffer[bpos], "%s", upload_filename);
bpos = strlen(buffer); bpos = strlen(buffer);
} else if (conf.unzip_cmd[i] == 'd') { } else if (conf.unzip_cmd[i] == 'd') {
sprintf(&buffer[bpos], "%s/node%d/", conf.bbs_path, mynode); sprintf(&buffer[bpos], "%s/node%d/bwave/", conf.bbs_path, mynode);
bpos = strlen(buffer); bpos = strlen(buffer);
} else if (conf.unzip_cmd[i] == '*') { } else if (conf.unzip_cmd[i] == '*') {
buffer[bpos++] = '*'; buffer[bpos++] = '*';
@ -592,12 +598,12 @@ void bwave_upload_reply() {
unlink(upload_filename); unlink(upload_filename);
snprintf(buffer, 1024, "%s/node%d/%s.UPL", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave/%s.UPL", conf.bbs_path, mynode, conf.bwave_name);
upl_file = fopen(buffer, "r"); upl_file = fopen(buffer, "r");
if (!upl_file) { if (!upl_file) {
snprintf(buffer, 1024, "%s/node%d/%s.upl", conf.bbs_path, mynode, conf.bwave_name); snprintf(buffer, 1024, "%s/node%d/bwave/%s.upl", conf.bbs_path, mynode, conf.bwave_name);
upl_file = fopen(buffer, "r"); upl_file = fopen(buffer, "r");
if (!upl_file) { if (!upl_file) {
s_printf(get_string(196)); s_printf(get_string(196));
@ -666,7 +672,7 @@ void bwave_upload_reply() {
} }
} }
snprintf(msgbuffer, 1024, "%s/node%d/%s", conf.bbs_path, mynode, upl_rec.filename); snprintf(msgbuffer, 1024, "%s/node%d/bwave/%s", conf.bbs_path, mynode, upl_rec.filename);
if (conf.mail_conferences[confr]->tagline != NULL) { if (conf.mail_conferences[confr]->tagline != NULL) {
tagline = conf.mail_conferences[confr]->tagline; tagline = conf.mail_conferences[confr]->tagline;
@ -703,8 +709,6 @@ void bwave_upload_reply() {
fread(body, 1, s.st_size, msg_file); fread(body, 1, s.st_size, msg_file);
fclose(msg_file); fclose(msg_file);
unlink(msgbuffer);
body[s.st_size] = '\0'; body[s.st_size] = '\0';
strcat(body, originlinebuffer); strcat(body, originlinebuffer);
@ -740,12 +744,10 @@ void bwave_upload_reply() {
} }
} }
fclose(upl_file); snprintf(buffer, 1024, "%s/node%d/bwave/", conf.bbs_path, mynode);
unlink(buffer); recursive_delete(buffer);
snprintf(buffer, 1024, "%s/node%d/%s.OLC", conf.bbs_path, mynode, conf.bwave_name);
unlink(buffer);
snprintf(buffer, 1024, "%s/node%d/%s.REQ", conf.bbs_path, mynode, conf.bwave_name);
unlink(buffer);
s_printf("\r\n"); s_printf("\r\n");
s_printf(get_string(6)); s_printf(get_string(6));

115
files.c
View File

@ -16,6 +16,7 @@
extern struct bbs_config conf; extern struct bbs_config conf;
extern int gSocket; extern int gSocket;
extern int sshBBS; extern int sshBBS;
extern int mynode;
struct file_entry { struct file_entry {
char *filename; char *filename;
@ -217,6 +218,88 @@ void upload_zmodem(struct user_record *user, char *upload_p) {
} }
} }
char *get_file_id_diz(char *filename) {
char *description;
char buffer[1024];
struct stat s;
int bpos;
int i;
FILE *fptr;
int len;
if (tolower(filename[strlen(filename) - 1]) != 'p' || tolower(filename[strlen(filename) - 2]) != 'i' || tolower(filename[strlen(filename) - 3]) != 'z') {
return NULL;
}
snprintf(buffer, 1024, "%s/node%d", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
mkdir(buffer, 0755);
}
snprintf(buffer, 1024, "%s/node%d/temp", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
if (recursive_delete(buffer) != 0) {
return NULL;
}
}
mkdir(buffer, 0755);
bpos = 0;
for (i=0;i<strlen(conf.unzip_cmd);i++) {
if (conf.unzip_cmd[i] == '*') {
i++;
if (conf.unzip_cmd[i] == 'a') {
sprintf(&buffer[bpos], "%s", filename);
bpos = strlen(buffer);
} else if (conf.unzip_cmd[i] == 'd') {
sprintf(&buffer[bpos], "%s/node%d/temp/", conf.bbs_path, mynode);
bpos = strlen(buffer);
} else if (conf.unzip_cmd[i] == '*') {
buffer[bpos++] = '*';
buffer[bpos] = '\0';
}
} else {
buffer[bpos++] = conf.unzip_cmd[i];
buffer[bpos] = '\0';
}
}
system(buffer);
snprintf(buffer, 1024, "%s/node%d/temp/FILE_ID.DIZ", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
snprintf(buffer, 1024, "%s/node%d/temp/file_id.diz", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
snprintf(buffer, 1024, "%s/node%d/temp", conf.bbs_path, mynode);
recursive_delete(buffer);
return NULL;
}
}
description = (char *)malloc(s.st_size + 1);
fptr = fopen(buffer, "rb");
fread(description, 1, s.st_size, fptr);
description[s.st_size] = '\0';
fclose(fptr);
bpos = 0;
len = strlen(description);
for (i=0;i<len;i++) {
if (description[i] == '\r') {
continue;
} else {
description[bpos++] = description[i];
}
}
description[bpos] = '\0';
return description;
}
void upload(struct user_record *user) { void upload(struct user_record *user) {
char buffer[331]; char buffer[331];
char buffer2[66]; char buffer2[66];
@ -235,11 +318,18 @@ void upload(struct user_record *user) {
int rc; int rc;
struct stat s; struct stat s;
char *err_msg = NULL; char *err_msg = NULL;
char *description;
upload_zmodem(user, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->upload_path); upload_zmodem(user, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->upload_path);
description = NULL;
s_printf("\r\nPlease enter a description:\r\n"); s_printf(get_string(198));
description = get_file_id_diz(upload_filename);
if (description == NULL) {
s_printf(get_string(199));
s_printf(get_string(200));
buffer[0] = '\0'; buffer[0] = '\0';
for (i=0;i<5;i++) { for (i=0;i<5;i++) {
s_printf("\r\n%d: ", i); s_printf("\r\n%d: ", i);
@ -251,6 +341,9 @@ void upload(struct user_record *user) {
strcat(buffer, "\n"); strcat(buffer, "\n");
} }
} else {
s_printf(get_string(201));
}
sprintf(buffer3, "%s/%s.sq3", conf.bbs_path, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->database); sprintf(buffer3, "%s/%s.sq3", conf.bbs_path, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->database);
rc = sqlite3_open(buffer3, &db); rc = sqlite3_open(buffer3, &db);
@ -266,6 +359,9 @@ void upload(struct user_record *user) {
dolog("SQL error: %s", err_msg); dolog("SQL error: %s", err_msg);
sqlite3_free(err_msg); sqlite3_free(err_msg);
sqlite3_close(db); sqlite3_close(db);
if (description != NULL) {
free(description);
}
return; return;
} }
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
@ -274,12 +370,19 @@ void upload(struct user_record *user) {
stat(upload_filename, &s); stat(upload_filename, &s);
sqlite3_bind_text(res, 1, upload_filename, -1, 0); sqlite3_bind_text(res, 1, upload_filename, -1, 0);
if (description == NULL) {
sqlite3_bind_text(res, 2, buffer, -1, 0); sqlite3_bind_text(res, 2, buffer, -1, 0);
} else {
sqlite3_bind_text(res, 2, description, -1, 0);
}
sqlite3_bind_int(res, 3, s.st_size); sqlite3_bind_int(res, 3, s.st_size);
} else { } else {
dolog("Failed to execute statement: %s", sqlite3_errmsg(db)); dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
sqlite3_finalize(res); sqlite3_finalize(res);
sqlite3_close(db); sqlite3_close(db);
if (description != NULL) {
free(description);
}
return; return;
} }
@ -289,10 +392,20 @@ void upload(struct user_record *user) {
dolog("execution failed: %s", sqlite3_errmsg(db)); dolog("execution failed: %s", sqlite3_errmsg(db));
sqlite3_finalize(res); sqlite3_finalize(res);
sqlite3_close(db); sqlite3_close(db);
if (description != NULL) {
free(description);
}
return; return;
} }
sqlite3_finalize(res); sqlite3_finalize(res);
sqlite3_close(db); sqlite3_close(db);
if (description != NULL) {
free(description);
}
s_printf(get_string(202));
s_printf(get_string(6));
s_getc();
} }
void download_zmodem(struct user_record *user, char *filename) { void download_zmodem(struct user_record *user, char *filename) {

View File

@ -196,3 +196,8 @@ Is this Correct? (Y/N)
\e[1;32mScanned \e[1;37m%s\e[1;32m->\e[1;37m%s\e[1;32m got \e[1;37m%d\e[1;32m msgs...\e[0m\r\n \e[1;32mScanned \e[1;37m%s\e[1;32m->\e[1;37m%s\e[1;32m got \e[1;37m%d\e[1;32m msgs...\e[0m\r\n
\e[1;31mInvalid Reply Packet!\e[0m \e[1;31mInvalid Reply Packet!\e[0m
\r\n\e[1;31mFailed to add message\e[0m\r\n \r\n\e[1;31mFailed to add message\e[0m\r\n
\r\n\e[1;32mChecking for file_id.diz...\r\n
...Not Found!\r\n\r\n
\r\nPlease enter a description:\r\n
...Found!\r\n\r\n
\e[1;32mUpload Successful!\e[0m\r\n