From 19c2d8df76d2eed61092e7dae6bc7e458458741b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:08:47 +0200 Subject: [PATCH 1/6] main() is exptected to return a value --- fiche.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fiche.c b/fiche.c index c3f71c0..7cfafe3 100644 --- a/fiche.c +++ b/fiche.c @@ -62,6 +62,8 @@ int main(int argc, char **argv) } else while (1) perform_connection(listen_socket); + + return 0; } void *thread_connection(void *args) From 9c2919f01db42e367aae1db4f36e1c7610136796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:16:59 +0200 Subject: [PATCH 2/6] move all info() lines from parse_parameters() to startup_message(). Move DAEMON = 1 to getopt --- fiche.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/fiche.c b/fiche.c index 7cfafe3..9a87c42 100644 --- a/fiche.c +++ b/fiche.c @@ -381,6 +381,9 @@ void startup_message() info("Domain name: %s\n", DOMAIN); info("Saving files to: %s\n", BASEDIR); info("Fiche started listening on port %d.\n", PORT); + info("Buffer size set to: %d.\n", BUFSIZE); + info("Slug size set to: %d.\n", SLUG_SIZE); + info("Log file: %s\n", LOG); info("====================================\n"); } @@ -396,13 +399,11 @@ void parse_parameters(int argc, char **argv) { int c; - if (strcmp(*argv, "-D")) - DAEMON = 1; - while ((c = getopt (argc, argv, "Dep:b:s:d:o:l:B:u:w:")) != -1) switch (c) { case 'D': + DAEMON = 1; break; case 'e': snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ"); @@ -415,7 +416,6 @@ void parse_parameters(int argc, char **argv) break; case 'B': BUFSIZE = atoi(optarg); - info("Buffer size set to: %d.\n", BUFSIZE); break; case 'b': BANFILE = optarg; @@ -423,14 +423,12 @@ void parse_parameters(int argc, char **argv) break; case 's': SLUG_SIZE = atoi(optarg); - info("Slug size set to: %d.\n", SLUG_SIZE); break; case 'o': BASEDIR = optarg; break; case 'l': LOG = optarg; - info("Log file: %s\n", LOG); break; case 'u': set_uid_gid(optarg); From f9d7f3d5c8f9d98cbc61abe713cee24ac46c12cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:18:41 +0200 Subject: [PATCH 3/6] WARNING != ERROR --- fiche.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fiche.c b/fiche.c index 9a87c42..8ee3dab 100644 --- a/fiche.c +++ b/fiche.c @@ -175,7 +175,7 @@ struct client_data get_client_address(struct sockaddr_in client_address) hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET); if (hostp == NULL) { - info("ERROR: Couldn't obtain client's hostname\n"); + info("WARNING: Couldn't obtain client's hostname\n"); data.hostname = "n/a"; } else @@ -184,7 +184,7 @@ struct client_data get_client_address(struct sockaddr_in client_address) hostaddrp = inet_ntoa(client_address.sin_addr); if (hostaddrp == NULL) { - info("ERROR: Couldn't obtain client's address\n"); + info("WARNING: Couldn't obtain client's address\n"); data.ip_address = "n/a"; } else From f7926b75fc36c189f47de8e7db47f1bd36f2d1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:23:30 +0200 Subject: [PATCH 4/6] display_date() is only used one time, doesn't make sense to make it a function --- fiche.c | 7 +------ fiche.h | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/fiche.c b/fiche.c index 8ee3dab..9435381 100644 --- a/fiche.c +++ b/fiche.c @@ -147,11 +147,6 @@ void perform_connection(int listen_socket) pthread_detach(thread_id); } -void display_date() -{ - info("%s\n", get_date()); -} - char *get_date() { time_t rawtime; @@ -216,7 +211,7 @@ void display_info(struct client_data data, char *slug, char *message) if (slug == NULL) info("%s\n", message); else info("Saved to: %s\n", slug); - display_date(); + info("%s\n", get_date()); info("Client: %s (%s)\n", data.ip_address, data.hostname); info("====================================\n"); } diff --git a/fiche.h b/fiche.h index 5dfcd4f..e43b168 100644 --- a/fiche.h +++ b/fiche.h @@ -81,7 +81,6 @@ int check_protocol(char *buffer); void bind_to_port(int listen_socket, struct sockaddr_in serveraddr); void error(char *error_code){perror(error_code); exit(1);} -void display_date(); void perform_connection(int listen_socket); void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data); void save_to_file(char *buffer, char *slug, struct client_data data); From 7e33e463bbd38a3029bdd3f82cc2fdb5a344ddd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:29:40 +0200 Subject: [PATCH 5/6] remove info(), instead used printf's. Check for DAEMON at some stages --- fiche.c | 40 ++++++++++++++++++++-------------------- fiche.h | 1 - 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/fiche.c b/fiche.c index 9435381..e06384d 100644 --- a/fiche.c +++ b/fiche.c @@ -170,7 +170,7 @@ struct client_data get_client_address(struct sockaddr_in client_address) hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET); if (hostp == NULL) { - info("WARNING: Couldn't obtain client's hostname\n"); + printf("WARNING: Couldn't obtain client's hostname\n"); data.hostname = "n/a"; } else @@ -179,7 +179,7 @@ struct client_data get_client_address(struct sockaddr_in client_address) hostaddrp = inet_ntoa(client_address.sin_addr); if (hostaddrp == NULL) { - info("WARNING: Couldn't obtain client's address\n"); + printf("WARNING: Couldn't obtain client's address\n"); data.ip_address = "n/a"; } else @@ -208,12 +208,17 @@ void save_log(char *slug, char *hostaddrp, char *h_name) void display_info(struct client_data data, char *slug, char *message) { + if (DAEMON) + return; + if (slug == NULL) - info("%s\n", message); - else info("Saved to: %s\n", slug); - info("%s\n", get_date()); - info("Client: %s (%s)\n", data.ip_address, data.hostname); - info("====================================\n"); + printf("%s\n", message); + else + printf("Saved to: %s\n", slug); + + printf("%s\n", get_date()); + printf("Client: %s (%s)\n", data.ip_address, data.hostname); + printf("====================================\n"); } char *check_banlist(char *ip_address) @@ -371,23 +376,18 @@ void set_basedir() } void startup_message() -{ - info("====================================\n"); - info("Domain name: %s\n", DOMAIN); - info("Saving files to: %s\n", BASEDIR); - info("Fiche started listening on port %d.\n", PORT); - info("Buffer size set to: %d.\n", BUFSIZE); - info("Slug size set to: %d.\n", SLUG_SIZE); - info("Log file: %s\n", LOG); - info("====================================\n"); -} - -void info(char *buffer, ...) { if (DAEMON) return; - printf(buffer); + printf("====================================\n"); + printf("Domain name: %s\n", DOMAIN); + printf("Saving files to: %s\n", BASEDIR); + printf("Fiche started listening on port %d.\n", PORT); + printf("Buffer size set to: %d.\n", BUFSIZE); + printf("Slug size set to: %d.\n", SLUG_SIZE); + printf("Log file: %s\n", LOG); + printf("====================================\n"); } void parse_parameters(int argc, char **argv) diff --git a/fiche.h b/fiche.h index e43b168..b68e2af 100644 --- a/fiche.h +++ b/fiche.h @@ -92,7 +92,6 @@ void parse_parameters(int argc, char **argv); void save_log(char *slug, char *hostaddrp, char *h_name); void change_owner(char *directory); void set_uid_gid(); -void info(char *buffer, ...); char *check_banlist(char *ip_address); char *check_whitelist(char *ip_address); From b1de8217406faf69821d5d8be1cf847099a6df6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ketelaars?= Date: Sat, 19 Sep 2015 17:54:15 +0200 Subject: [PATCH 6/6] combine 2 if-statements. While here; spacing, spacing, spacing --- fiche.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/fiche.c b/fiche.c index e06384d..c4f9c4c 100644 --- a/fiche.c +++ b/fiche.c @@ -38,7 +38,7 @@ int main(int argc, char **argv) parse_parameters(argc, argv); if (BASEDIR == NULL) set_basedir(); - + startup_message(); int listen_socket, optval = 1; @@ -77,25 +77,23 @@ void *thread_connection(void *args) bzero(buffer, BUFSIZE); int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT); - if (WHITELIST != NULL) - if (check_whitelist(data.ip_address) == NULL) - { - display_info(data, NULL, "Rejected connection from unknown user."); - save_log(NULL, data.ip_address, data.hostname); - write(connection_socket, "You are not whitelisted!\n", 26); - close(connection_socket); - pthread_exit(NULL); - } + if (WHITELIST != NULL && check_whitelist(data.ip_address) == NULL) + { + display_info(data, NULL, "Rejected connection from unknown user."); + save_log(NULL, data.ip_address, data.hostname); + write(connection_socket, "You are not whitelisted!\n", 26); + close(connection_socket); + pthread_exit(NULL); + } - if (BANLIST != NULL) - if (check_banlist(data.ip_address) != NULL) - { - display_info(data, NULL, "Rejected connection from banned user."); - save_log(NULL, data.ip_address, data.hostname); - write(connection_socket, "You are banned!\n", 17); - close(connection_socket); - pthread_exit(NULL); - } + if (BANLIST != NULL && check_banlist(data.ip_address) != NULL) + { + display_info(data, NULL, "Rejected connection from banned user."); + save_log(NULL, data.ip_address, data.hostname); + write(connection_socket, "You are banned!\n", 17); + close(connection_socket); + pthread_exit(NULL); + } if (check_protocol(buffer) == 1) status = -1; @@ -124,7 +122,7 @@ void perform_connection(int listen_socket) { pthread_t thread_id; struct sockaddr_in client_address; - + int address_length = sizeof(client_address); int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_length); @@ -345,8 +343,8 @@ void save_to_file(char *slug, char *buffer, struct client_data data) void change_owner(char *directory) { - if ((UID != -1)&&(GID != -1)) - chown(directory, UID, GID); + if (UID != -1 && GID != -1) + chown(directory, UID, GID); } void set_uid_gid(char *username)