1
/* $Id: dict.c,v 1.2 2003/01/23 00:36:01 robertc Exp $
2
* Copyright (C) 2002 Rodrigo Campos
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.
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.
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.
18
* Author: Rodrigo Campos (rodrigo@geekbunker.org)
25
#include <sys/types.h>
26
#include <sys/socket.h>
27
#include <netinet/in.h>
28
#include <arpa/inet.h>
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
47
struct ip_user_dict *current_entry; /* the structure used to
49
struct ip_user_dict *first_entry = NULL; /* the head of the
51
char line[BUFSIZE]; /* the buffer for the lines read
53
char *cp; /* a char pointer used to parse
55
char *username; /* for the username */
56
char *tmpbuf; /* for the address before the
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;
63
while ((cp = fgets (line, sizeof (line), FH)) != NULL) {
67
if ((cp = strchr (line, '\n')) != NULL) {
68
/* chop \n characters */
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 */
78
cp = strtok (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);
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"));
90
/* get space for the username */
91
current_entry->username =
92
calloc (strlen (username) + 1, sizeof (char));
93
strcpy (current_entry->username, username);
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;
103
/* Return a pointer to the first entry linked list */
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
112
dict_lookup (struct ip_user_dict *first_entry, char *username,
115
/* Move the pointer to the first entry of the linked list. */
116
struct ip_user_dict *current_entry = first_entry;
118
while (current_entry->username != NULL) {
120
printf ("user: %s\naddr: %lu\nmask: %lu\n\n",
121
current_entry->username, current_entry->address,
122
current_entry->netmask);
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)
133
if ((match_group (current_entry->username, username)) == 1)
137
current_entry = current_entry->next_entry;
140
/* If no match was found we return 0 */