added whitelist - fiche - A pastebin adjusted for gopher use
 (HTM) git clone git://vernunftzentrum.de/fiche.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 0eedbf68c00f142e8db1d438bf506a6af37d3010
 (DIR) parent d1a66efe6e498d93f7319a2cea8071a340c756d1
 (HTM) Author: solusipse <solus1ps3@gmail.com>
       Date:   Sat, 14 Sep 2013 00:17:26 +0200
       
       added whitelist
       
       Diffstat:
         fiche.c                             |      35 +++++++++++++++++++++++++-------
         fiche.h                             |       4 +++-
       
       2 files changed, 31 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/fiche.c b/fiche.c
       @@ -60,7 +60,18 @@ void *thread_connection(void *args)
            bzero(buffer, BUFSIZE);
            int status = recv(connection_socket, buffer, BUFSIZE, MSG_WAITALL);
        
       -    if (BANLIST != NULL)
       +    if (WHITELIST != NULL)
       +        if (check_whitelist(data.ip_address) == NULL)
       +        {
       +            printf("Rejected connection from unknown user.\n");
       +            display_line();
       +            save_log(NULL, data.ip_address, data.hostname);
       +            write(connection_socket, "You are not whitelisted!\n", 17);
       +            close(connection_socket);
       +            pthread_exit(NULL);
       +        }
       +
       +    if ((BANLIST != NULL))
                if (check_banlist(data.ip_address) != NULL)
                {
                    printf("Rejected connection from banned user.\n");
       @@ -177,11 +188,17 @@ void save_log(char *slug, char *hostaddrp, char *h_name)
        
        char *check_banlist(char *ip_address)
        {
       -    load_banlist(BANFILE);
       +    load_list(BANFILE, 0);
            return strstr(BANLIST, ip_address);
        }
        
       -void load_banlist(char *file_path)
       +char *check_whitelist(char *ip_address)
       +{
       +    load_list(WHITEFILE, 1);
       +    return strstr(WHITELIST, ip_address);
       +}
       +
       +void load_list(char *file_path, int type)
        {
            FILE *fp = fopen(file_path, "r");
            fseek(fp, 0, SEEK_END);
       @@ -189,12 +206,15 @@ void load_banlist(char *file_path)
            fseek(fp, 0, SEEK_SET);
        
            char *buffer = malloc(fsize + 1);
       -
            fread(buffer, fsize, 1, fp);
            fclose(fp);
        
            buffer[fsize] = 0;
       -    BANLIST = buffer;
       +
       +    if (type == 0)
       +        BANLIST = buffer;
       +    else
       +        WHITELIST = buffer;
        
            free(buffer);
        }
       @@ -330,7 +350,7 @@ void parse_parameters(int argc, char **argv)
                        break;
                    case 'b':
                        BANFILE = optarg;
       -                load_banlist(BANFILE);
       +                load_list(BANFILE, 0);
                        break;
                    case 's':
                        SLUG_SIZE = atoi(optarg);
       @@ -349,7 +369,8 @@ void parse_parameters(int argc, char **argv)
                        set_uid_gid(optarg);
                        break;
                    case 'w':
       -                WHITELIST = optarg;
       +                WHITEFILE = optarg;
       +                load_list(WHITEFILE, 1);
                        break;
                    default:
                        printf("usage: fiche [-bdpqs].\n");
 (DIR) diff --git a/fiche.h b/fiche.h
       @@ -47,6 +47,7 @@ char *LOG;
        char *BASEDIR;
        char *BANLIST;
        char *BANFILE;
       +char *WHITEFILE;
        char *WHITELIST;
        int PORT = 9999;
        int SLUG_SIZE = 4;
       @@ -69,7 +70,7 @@ void generate_url(char *buffer, char *slug);
        void save_to_file(char *buffer, char *slug);
        void startup_message();
        void set_basedir();
       -void load_banlist();
       +void load_list(char *file_path, int type);
        void parse_parameters(int argc, char **argv);
        void save_log(char *slug, char *hostaddrp, char *h_name);
        void change_owner(char *directory);
       @@ -77,6 +78,7 @@ void set_uid_gid();
        
        char *return_line(){return("\n====================================");}
        char *check_banlist(char *ip_address);
       +char *check_whitelist(char *ip_address);
        char *get_date();
        
        struct sockaddr_in set_address(struct sockaddr_in serveraddr);