Fixed major issue

This commit is contained in:
Emil 2023-08-15 07:02:32 -06:00
parent d7693263ee
commit 3e2cf10c77
No known key found for this signature in database
GPG Key ID: 5432DB986FDBCF8A
3 changed files with 63 additions and 84 deletions

View File

@ -44,12 +44,14 @@ api_rope(void)
sqlite3_close(connection); sqlite3_close(connection);
} }
#define DBERR_NOMEM(v) if (v) { r = (IRC_RED "No memory!" IRC_STOP); goto fail; }
DECL char * DECL char *
remind(char * who) remind(char * who)
{ {
char * r; char * r;
char * title; char * title;
char * desc; char * desc = NULL;
/* char * repo; */ /* char * repo; */
DBERR(sqlite3_reset(remind_stmt)); DBERR(sqlite3_reset(remind_stmt));
DBERR(sqlite3_bind_text(remind_stmt, 1, who, -1, SQLITE_STATIC)); DBERR(sqlite3_bind_text(remind_stmt, 1, who, -1, SQLITE_STATIC));
@ -59,25 +61,20 @@ remind(char * who)
{ {
title = (char *) sqlite3_column_text(remind_stmt, 0); title = (char *) sqlite3_column_text(remind_stmt, 0);
title = strdup(title); title = strdup(title);
if (!title) { goto fail; }
desc = (char *) sqlite3_column_text(remind_stmt, 1); desc = (char *) sqlite3_column_text(remind_stmt, 1);
if (desc) { desc = strdup(desc); } else { desc = ""; } if (desc && (desc = strdup(desc)) == NULL)
/* repo = (char *) sqlite3_column_text(remind_stmt, 3); */ { goto fail; }
/* if (repo) { repo = strdup(repo); } else { repo = "<no link available>"; } */
/* if (-1 == asprintf(&r, */
/* IRC_RED "%s: " IRC_YELLOW "%s" IRC_GREEN */
/* " (@" IRC_BLUE "%s" IRC_GREEN ")" IRC_STOP, */
/* title, desc, repo)) */
if (-1 == asprintf(&r, if (-1 == asprintf(&r,
IRC_RED "%s - " IRC_YELLOW "%s" IRC_STOP, IRC_RED "%s " IRC_YELLOW "%s" IRC_STOP,
title, desc)) title, desc ? desc : ""))
{ /* this will probably never happen. But it implies a memory failure */ { r = IRC_RED "No memory!" IRC_STOP; }
r = strdup(IRC_RED "No memory!" IRC_STOP); fail:
} free(title);
free(desc);
} }
else else
{ { r = IRC_RED "No current assignment." IRC_STOP; }
r = strdup(IRC_RED "No current assignment." IRC_STOP);
}
return r; return r;
} }

View File

@ -1,16 +1,5 @@
/* irc.c - IRC interface */ /* irc.c - IRC interface */
#define FULL_FREE(obj) \
do \
{ \
if ((obj)) \
{ \
memset((obj), '\0', strlen((obj))); \
free((obj)); \
(obj) = NULL; \
} \
} while (0)
#define PREFIX_COMMAND_CHAR '!' #define PREFIX_COMMAND_CHAR '!'
#define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg) #define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg)
@ -187,8 +176,8 @@ parse_command(char const * cmd)
size_t i = 0; size_t i = 0;
char* msgswp = NULL; char* msgswp = NULL;
/* size_t len = strlen(cmd); */ /* size_t len = strlen(cmd); */
printf("Handling '%s'\n", cmd);
if ((i += has_arg(cmd))) if (!(i += has_arg(cmd)))
{ {
/* NO ARGUMENTS */ /* NO ARGUMENTS */
if (strcmp(cmd, "remind") == 0) if (strcmp(cmd, "remind") == 0)
@ -221,6 +210,7 @@ parse_command(char const * cmd)
#ifndef NO_VULN_COMMANDS #ifndef NO_VULN_COMMANDS
if (strncmp(cmd, "raw", i) == 0) if (strncmp(cmd, "raw", i) == 0)
{ {
printf("RAW\n");
/* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */ /* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */
msgswp = raw(arg); msgswp = raw(arg);
ircmsg(creds.channel, msgswp); ircmsg(creds.channel, msgswp);
@ -237,11 +227,6 @@ parse_command(char const * cmd)
free(msgswp); free(msgswp);
} }
DECL void
creds_free(void)
{
FULL_FREE(creds.username);
FULL_FREE(creds.password);
FULL_FREE(creds.channel);
FULL_FREE(creds.server);
}

View File

@ -2,23 +2,12 @@
#define VERSION_STRING "1" #define VERSION_STRING "1"
/* Parses the format username[:password]@server[:port] */ /* Parses the format username[:password]@server[:port]
* user:password@server:port = userNULpasswordNULserverNUL (port N/A)
#define GENCOPY(dst,src,l) \ */
do \
{ \
dst = malloc(sep + 1); \
if (dst) \
{ \
strncpy(dst,src,l); \
dst[len] = '\0'; \
} \
else \
{ return 1; } \
} while (0)
DECL int DECL int
parse_url(char const * url) parse_url(char * url)
{ {
size_t len = strlen(url); size_t len = strlen(url);
size_t sep = 0, ls = 0, rs; size_t sep = 0, ls = 0, rs;
@ -32,25 +21,22 @@ parse_url(char const * url)
while (++rs < len && url[rs] != ':'); while (++rs < len && url[rs] != ':');
if (rs == len) if (rs == len)
{ rs = 0; } { rs = 0; }
GENCOPY(creds.username, url, ls ? ls : sep); creds.username = url;
url[ls ? ls : sep] = '\0';
if (ls) if (ls)
{ {
GENCOPY(creds.password, url + ls + 1, sep - ls - 1); creds.password = url + ls + 1;
url[sep] = '\0';
} }
creds.server = url + sep + 1;
if (rs) if (rs)
{ {
GENCOPY(creds.server, url + sep + 1, rs - sep - 1); url[rs] = '\0';
creds.port = atoi(url + rs + 1); creds.port = atoi(url + rs + 1);
} }
else
{
GENCOPY(creds.server, url + sep + 1, len - sep - 1);
}
return 0; return 0;
} }
#undef GENCOPY
DECL void DECL void
rope(void) rope(void)
{ {
@ -59,36 +45,48 @@ rope(void)
api_rope(); api_rope();
} }
/* All possible failures / successes wipe the password from memory. */
DECL int DECL int
init(void) init(void)
{ {
int ret = 0;
srand(time(NULL)); srand(time(NULL));
if(api_init())
{ ERR(DB_ERROR, "Error initializing database."); }
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));
callbacks.event_connect = event_connect; callbacks.event_connect = event_connect;
callbacks.event_channel = event_channel; callbacks.event_channel = event_channel;
if(!api_init())
{
session = irc_create_session(&callbacks); session = irc_create_session(&callbacks);
if (!session) if (!session)
{ {
ERRMSG("Error creating IRC session"); ERRMSG("Error creating IRC session");
goto fail; ret = 1;
} }
else
{
atexit(rope); atexit(rope);
assert(creds.username != NULL); if (creds.username == NULL ||
assert(creds.server != NULL); creds.server == NULL)
{ ERRMSG("Server and Username required. Stopping now");
exit(1); }
if (irc_connect(session, if (irc_connect(session,
creds.server, creds.port, creds.password, creds.server, creds.port, creds.password,
creds.username, creds.username, creds.username)) creds.username, creds.username, creds.username))
{ {
fprintf(stderr, "IRC ERROR: %s\n", irc_strerror(irc_errno(session))); fprintf(stderr, "IRC ERROR: %s\n", irc_strerror(irc_errno(session)));
exit(1); ret = 1;
} }
FULL_FREE(creds.password); }
return 0; }
fail: else
FULL_FREE(creds.password); {
return 1; ERRMSG("Error initializing database.");
ret = 1;
}
if (creds.password)
{ memset(creds.password, '\0', strlen(creds.password)); }
return ret;
} }
DECL int DECL int
@ -109,7 +107,7 @@ help(void)
"-url URL - Sets the target URL\n" "-url URL - Sets the target URL\n"
"-db DBFILE - Sets the database file (default: probotic_data.sqlite)\n" "-db DBFILE - Sets the database file (default: probotic_data.sqlite)\n"
"-identify PASSWORD - Identifies against NickServ\n" "-identify PASSWORD - Identifies against NickServ\n"
"-version - Prints Version information\n" "-version - Prints version information\n"
"-help - Prints this information\n" "-help - Prints this information\n"
"\nUse format username[:password]@server[:port], port defaults to 6667.\n"); "\nUse format username[:password]@server[:port], port defaults to 6667.\n");
} }
@ -147,7 +145,7 @@ main (int argc,
else if (strcmp(arg, "url") == 0) else if (strcmp(arg, "url") == 0)
{ parse_url(argv[1]); } { parse_url(argv[1]); }
else if (strcmp(arg, "channel") == 0) else if (strcmp(arg, "channel") == 0)
{ free(creds.channel); creds.channel = strdup(argv[1]); } { creds.channel = argv[1]; }
else if (strcmp(arg, "identify") == 0) else if (strcmp(arg, "identify") == 0)
{ ident_password = argv[1]; } { ident_password = argv[1]; }
else else
@ -160,7 +158,6 @@ main (int argc,
{ goto help; } { goto help; }
} }
} }
atexit(creds_free);
fprintf(stderr, "Connecting to %s:%s@%s:%d\nChannel: %s\n", fprintf(stderr, "Connecting to %s:%s@%s:%d\nChannel: %s\n",
creds.username, creds.password, creds.username, creds.password,