]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - helpers/external_acl/file_userip/ext_file_userip_acl.cc
SourceFormat Enforcement
[thirdparty/squid.git] / helpers / external_acl / file_userip / ext_file_userip_acl.cc
index d9a118470f0681d2267f16f5a45b6cacf98e67c2..9bc2b73637532d44d8925ad547271e59af70d414 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
 /*
  * Copyright (C) 2002 Rodrigo Campos
  *
 #include "rfc1738.h"
 #include "util.h"
 
-#if HAVE_STDIO_H
-#include <stdio.h>
-#endif
-#if HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-#if HAVE_STRING_H
-#include <string.h>
-#endif
+#include <cstdlib>
+#include <cstring>
 #if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -58,7 +59,7 @@ struct ip_user_dict *load_dict(FILE *);
 int dict_lookup(struct ip_user_dict *, char *, char *);
 
 /** Size of lines read from the dictionary file */
-#define DICT_BUFFER_SIZE       8196
+#define DICT_BUFFER_SIZE    8196
 
 /** This function parses the dictionary file and loads it
  * in memory. All IP addresses are processed with a bitwise AND
@@ -69,33 +70,41 @@ int dict_lookup(struct ip_user_dict *, char *, char *);
  */
 struct ip_user_dict *
 load_dict(FILE * FH) {
-    struct ip_user_dict *current_entry;        /* the structure used to
-                                          store data */
-    struct ip_user_dict *first_entry = NULL;   /* the head of the
-                                                  linked list */
+    struct ip_user_dict *current_entry; /* the structure used to
+                       store data */
+    struct ip_user_dict *first_entry = NULL;    /* the head of the
+                           linked list */
     char line[DICT_BUFFER_SIZE]; /* the buffer for the lines read
-                                  from the dict file */
-    char *cp;                  /* a char pointer used to parse
-                                  each line */
-    char *username;            /* for the username */
-    char *tmpbuf;                      /* for the address before the
-                                  bitwise AND */
+                   from the dict file */
+    char *tmpbuf;           /* for the address before the
+                   bitwise AND */
 
     /* the pointer to the first entry in the linked list */
     first_entry = (struct ip_user_dict*)malloc(sizeof(struct ip_user_dict));
     current_entry = first_entry;
 
-    while ((cp = fgets (line, DICT_BUFFER_SIZE, FH)) != NULL) {
+    unsigned int lineCount = 0;
+    while (fgets(line, sizeof(line), FH) != NULL) {
+        ++lineCount;
         if (line[0] == '#') {
             continue;
         }
+
+        char *cp; // a char pointer used to parse each line.
         if ((cp = strchr (line, '\n')) != NULL) {
             /* chop \n characters */
             *cp = '\0';
         }
-        if ((cp = strtok (line, "\t ")) != NULL) {
+        if (strtok(line, "\t ") != NULL) {
+            // NP: line begins with IP/mask. Skipped to the end of it with this strtok()
+
             /* get the username */
-            username = strtok (NULL, "\t ");
+            char *username;
+            if ((username = strtok(NULL, "\t ")) == NULL) {
+                debug("Missing username on line %u of dictionary file\n", lineCount);
+                continue;
+            }
+
             /* look for a netmask */
             if ((cp = strtok (line, "/")) != NULL) {
                 /* store the ip address in a temporary buffer */
@@ -175,15 +184,15 @@ match_user(char *dict_username, char *username)
         }
     }
     return 0;
-}                              /* match_user */
+}               /* match_user */
 
 int
 match_group(char *dict_group, char *username)
 {
-    struct group *g;           /* a struct to hold group entries */
-    ++dict_group;                      /* the @ should be the first char
-                                  so we rip it off by incrementing
-                                  * the pointer by one */
+    struct group *g;        /* a struct to hold group entries */
+    ++dict_group;           /* the @ should be the first char
+                   so we rip it off by incrementing
+                   * the pointer by one */
 
     if ((g = getgrnam(dict_group)) == NULL) {
         debug("Group does not exist '%s'\n", dict_group);
@@ -209,7 +218,6 @@ usage(const char *program_name)
 int
 main (int argc, char *argv[])
 {
-    FILE *FH;
     char *filename = NULL;
     char *program_name = argv[0];
     char *cp;
@@ -241,7 +249,11 @@ main (int argc, char *argv[])
         usage(program_name);
         exit(1);
     }
-    FH = fopen(filename, "r");
+    FILE *FH = fopen(filename, "r");
+    if (!FH) {
+        fprintf(stderr, "%s: FATAL: Unable to open file '%s': %s", program_name, filename, xstrerror());
+        exit(1);
+    }
     current_entry = load_dict(FH);
 
     while (fgets(line, HELPER_INPUT_BUFFER, stdin)) {
@@ -278,3 +290,4 @@ main (int argc, char *argv[])
     fclose (FH);
     return 0;
 }
+