~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to helpers/external_acl/ip_user/dict.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
Import upstream version 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: dict.c,v 1.2 2003/01/23 00:36:01 robertc Exp $ 
 
2
* Copyright (C) 2002 Rodrigo Campos
 
3
*
 
4
* This program is free software; you can redistribute it and/or modify
 
5
* it under the terms of the GNU General Public License as published by
 
6
* the Free Software Foundation; either version 2 of the License, or
 
7
* (at your option) any later version.
 
8
 
9
* This program is distributed in the hope that it will be useful,
 
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
* GNU General Public License for more details.
 
13
 
14
* You should have received a copy of the GNU General Public License
 
15
* along with this program; if not, write to the Free Software
 
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*
 
18
* Author: Rodrigo Campos (rodrigo@geekbunker.org)
 
19
 
20
*/
 
21
                          
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <sys/types.h>
 
26
#include <sys/socket.h>
 
27
#include <netinet/in.h>
 
28
#include <arpa/inet.h>
 
29
 
 
30
#include "ip_user.h"
 
31
 
 
32
#ifndef DEBUG
 
33
#undef DEBUG
 
34
#endif
 
35
 
 
36
 
 
37
/* This function parses the dictionary file and loads it 
 
38
 * in memory. All IP addresses are processed with a bitwise AND
 
39
 * with their netmasks before they are stored.
 
40
 * If there�s no netmask (no /) in the in the lhs , a mask
 
41
 * 255.255.255.255 is assumed.
 
42
 * It returns a pointer to the first entry of the linked list
 
43
 */
 
44
struct ip_user_dict *
 
45
load_dict (FILE * FH)
 
46
{
 
47
  struct ip_user_dict *current_entry;   /* the structure used to
 
48
                                           store data */
 
49
  struct ip_user_dict *first_entry = NULL;      /* the head of the
 
50
                                                   linked list */
 
51
  char line[BUFSIZE];           /* the buffer for the lines read
 
52
                                   from the dict file */
 
53
  char *cp;                     /* a char pointer used to parse
 
54
                                   each line */
 
55
  char *username;               /* for the username */
 
56
  char *tmpbuf;                 /* for the address before the
 
57
                                   bitwise AND */
 
58
 
 
59
  /* the pointer to the first entry in the linked list */
 
60
  first_entry = malloc (sizeof (struct ip_user_dict));
 
61
  current_entry = first_entry;
 
62
 
 
63
  while ((cp = fgets (line, sizeof (line), FH)) != NULL) {
 
64
                  if (line[0] == '#') {
 
65
                                  continue;
 
66
                  }
 
67
    if ((cp = strchr (line, '\n')) != NULL) {
 
68
      /* chop \n characters */
 
69
      *cp = '\0';
 
70
    }
 
71
    if ((cp = strtok (line, "\t ")) != NULL) {
 
72
      /* get the username */
 
73
      username = strtok (NULL, "\t ");
 
74
      /* look for a netmask */
 
75
      if ((cp = strtok (line, "/")) != NULL) {
 
76
        /* store the ip address in a temporary buffer */
 
77
        tmpbuf = cp;
 
78
        cp = strtok (NULL, "/");
 
79
        if (cp != NULL) {
 
80
          /* if we have a slash in the lhs, we have a netmask */
 
81
          current_entry->netmask = (inet_addr (cp));
 
82
          current_entry->address =
 
83
            (((inet_addr (tmpbuf))) & current_entry->netmask);
 
84
        } else {
 
85
          /* when theres no slash, we figure the netmask is /32 */
 
86
          current_entry->address = (inet_addr (tmpbuf));
 
87
          current_entry->netmask = (inet_addr ("255.255.255.255"));
 
88
        }
 
89
      }
 
90
      /* get space for the username */
 
91
      current_entry->username =
 
92
        calloc (strlen (username) + 1, sizeof (char));
 
93
      strcpy (current_entry->username, username);
 
94
 
 
95
      /* get space and point current_entry to the new entry */
 
96
      current_entry->next_entry =
 
97
        malloc (sizeof (struct ip_user_dict));
 
98
      current_entry = current_entry->next_entry;
 
99
    }
 
100
 
 
101
  }
 
102
 
 
103
  /* Return a pointer to the first entry linked list */
 
104
  return first_entry;
 
105
}                               /* load_dict */
 
106
 
 
107
/* This function looks for a matching ip/mask in
 
108
 * the dict file loaded in memory.
 
109
 * It returns 1 if it finds a match or 0 if no match is found
 
110
 */
 
111
int
 
112
dict_lookup (struct ip_user_dict *first_entry, char *username,
 
113
             char *address)
 
114
{
 
115
  /* Move the pointer to the first entry of the linked list. */
 
116
  struct ip_user_dict *current_entry = first_entry;
 
117
 
 
118
  while (current_entry->username != NULL) {
 
119
#ifdef DEBUG
 
120
    printf ("user: %s\naddr: %lu\nmask: %lu\n\n",
 
121
            current_entry->username, current_entry->address,
 
122
            current_entry->netmask);
 
123
#endif
 
124
 
 
125
    if ((inet_addr (address) & (unsigned long) current_entry->
 
126
         netmask) == current_entry->address) {
 
127
      /* If the username contains an @ we assume it�s a group and
 
128
         call the corresponding function */
 
129
      if ((strchr (current_entry->username, '@')) == NULL) {
 
130
        if ((match_user (current_entry->username, username)) == 1)
 
131
          return 1;
 
132
      } else {
 
133
        if ((match_group (current_entry->username, username)) == 1)
 
134
          return 1;
 
135
      }
 
136
    }
 
137
    current_entry = current_entry->next_entry;
 
138
  }
 
139
 
 
140
  /* If no match was found we return 0 */
 
141
  return 0;
 
142
}                               /* dict_lookup */