diff --git a/src/api.c b/src/api.c index 2814856..ea66c67 100644 --- a/src/api.c +++ b/src/api.c @@ -44,12 +44,14 @@ api_rope(void) sqlite3_close(connection); } +#define DBERR_NOMEM(v) if (v) { r = (IRC_RED "No memory!" IRC_STOP); goto fail; } + DECL char * remind(char * who) { char * r; char * title; - char * desc; + char * desc = NULL; /* char * repo; */ DBERR(sqlite3_reset(remind_stmt)); 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 = strdup(title); - desc = (char *) sqlite3_column_text(remind_stmt, 1); - if (desc) { desc = strdup(desc); } else { desc = ""; } - /* repo = (char *) sqlite3_column_text(remind_stmt, 3); */ - /* if (repo) { repo = strdup(repo); } else { repo = ""; } */ - /* if (-1 == asprintf(&r, */ - /* IRC_RED "%s: " IRC_YELLOW "%s" IRC_GREEN */ - /* " (@" IRC_BLUE "%s" IRC_GREEN ")" IRC_STOP, */ - /* title, desc, repo)) */ + if (!title) { goto fail; } + desc = (char *) sqlite3_column_text(remind_stmt, 1); + if (desc && (desc = strdup(desc)) == NULL) + { goto fail; } if (-1 == asprintf(&r, - IRC_RED "%s - " IRC_YELLOW "%s" IRC_STOP, - title, desc)) - { /* this will probably never happen. But it implies a memory failure */ - r = strdup(IRC_RED "No memory!" IRC_STOP); - } + IRC_RED "%s " IRC_YELLOW "%s" IRC_STOP, + title, desc ? desc : "")) + { r = IRC_RED "No memory!" IRC_STOP; } + fail: + free(title); + free(desc); } else - { - r = strdup(IRC_RED "No current assignment." IRC_STOP); - } + { r = IRC_RED "No current assignment." IRC_STOP; } return r; } diff --git a/src/irc.c b/src/irc.c index 5add888..b9d3e5e 100644 --- a/src/irc.c +++ b/src/irc.c @@ -1,16 +1,5 @@ /* 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 IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg) @@ -187,8 +176,8 @@ parse_command(char const * cmd) size_t i = 0; char* msgswp = NULL; /* size_t len = strlen(cmd); */ - - if ((i += has_arg(cmd))) + printf("Handling '%s'\n", cmd); + if (!(i += has_arg(cmd))) { /* NO ARGUMENTS */ if (strcmp(cmd, "remind") == 0) @@ -221,6 +210,7 @@ parse_command(char const * cmd) #ifndef NO_VULN_COMMANDS if (strncmp(cmd, "raw", i) == 0) { + printf("RAW\n"); /* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */ msgswp = raw(arg); ircmsg(creds.channel, msgswp); @@ -237,11 +227,6 @@ parse_command(char const * cmd) free(msgswp); } -DECL void -creds_free(void) -{ - FULL_FREE(creds.username); - FULL_FREE(creds.password); - FULL_FREE(creds.channel); - FULL_FREE(creds.server); -} + + + diff --git a/src/main.c b/src/main.c index 5c7f870..941f0ae 100644 --- a/src/main.c +++ b/src/main.c @@ -2,23 +2,12 @@ #define VERSION_STRING "1" -/* Parses the format username[:password]@server[:port] */ - -#define GENCOPY(dst,src,l) \ - do \ - { \ - dst = malloc(sep + 1); \ - if (dst) \ - { \ - strncpy(dst,src,l); \ - dst[len] = '\0'; \ - } \ - else \ - { return 1; } \ - } while (0) +/* Parses the format username[:password]@server[:port] + * user:password@server:port = userNULpasswordNULserverNUL (port N/A) + */ DECL int -parse_url(char const * url) +parse_url(char * url) { size_t len = strlen(url); size_t sep = 0, ls = 0, rs; @@ -32,25 +21,22 @@ parse_url(char const * url) while (++rs < len && url[rs] != ':'); if (rs == len) { rs = 0; } - GENCOPY(creds.username, url, ls ? ls : sep); + creds.username = url; + url[ls ? ls : sep] = '\0'; 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) { - GENCOPY(creds.server, url + sep + 1, rs - sep - 1); + url[rs] = '\0'; creds.port = atoi(url + rs + 1); } - else - { - GENCOPY(creds.server, url + sep + 1, len - sep - 1); - } return 0; } -#undef GENCOPY - DECL void rope(void) { @@ -59,36 +45,48 @@ rope(void) api_rope(); } +/* All possible failures / successes wipe the password from memory. */ DECL int init(void) { + int ret = 0; srand(time(NULL)); - if(api_init()) - { ERR(DB_ERROR, "Error initializing database."); } memset(&callbacks, 0, sizeof(callbacks)); callbacks.event_connect = event_connect; callbacks.event_channel = event_channel; - session = irc_create_session(&callbacks); - if (!session) + if(!api_init()) { - ERRMSG("Error creating IRC session"); - goto fail; + session = irc_create_session(&callbacks); + if (!session) + { + ERRMSG("Error creating IRC session"); + ret = 1; + } + else + { + atexit(rope); + if (creds.username == NULL || + creds.server == NULL) + { ERRMSG("Server and Username required. Stopping now"); + exit(1); } + + if (irc_connect(session, + creds.server, creds.port, creds.password, + creds.username, creds.username, creds.username)) + { + fprintf(stderr, "IRC ERROR: %s\n", irc_strerror(irc_errno(session))); + ret = 1; + } + } } - atexit(rope); - assert(creds.username != NULL); - assert(creds.server != NULL); - if (irc_connect(session, - creds.server, creds.port, creds.password, - creds.username, creds.username, creds.username)) + else { - fprintf(stderr, "IRC ERROR: %s\n", irc_strerror(irc_errno(session))); - exit(1); + ERRMSG("Error initializing database."); + ret = 1; } - FULL_FREE(creds.password); - return 0; -fail: - FULL_FREE(creds.password); - return 1; + if (creds.password) + { memset(creds.password, '\0', strlen(creds.password)); } + return ret; } DECL int @@ -109,7 +107,7 @@ help(void) "-url URL - Sets the target URL\n" "-db DBFILE - Sets the database file (default: probotic_data.sqlite)\n" "-identify PASSWORD - Identifies against NickServ\n" - "-version - Prints Version information\n" + "-version - Prints version information\n" "-help - Prints this information\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) { parse_url(argv[1]); } else if (strcmp(arg, "channel") == 0) - { free(creds.channel); creds.channel = strdup(argv[1]); } + { creds.channel = argv[1]; } else if (strcmp(arg, "identify") == 0) { ident_password = argv[1]; } else @@ -160,7 +158,6 @@ main (int argc, { goto help; } } } - atexit(creds_free); fprintf(stderr, "Connecting to %s:%s@%s:%d\nChannel: %s\n", creds.username, creds.password,