]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/ip_user/main.c
Author: Francesco Chemolli <kinkie@squid-cache.org>
[thirdparty/squid.git] / helpers / external_acl / ip_user / main.c
1 /* $Id: main.c,v 1.6 2007/07/19 03:36:12 hno 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 "util.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include "ip_user.h"
30
31 static void
32 usage (char *program_name)
33 {
34 fprintf (stderr, "Usage:\n%s -f <configuration file>\n",
35 program_name);
36 }
37
38 int
39 main (int argc, char *argv[])
40 {
41 FILE *FH;
42 char *filename = NULL;
43 char *program_name = argv[0];
44 char *cp;
45 char *username, *address;
46 char line[BUFSIZE];
47 struct ip_user_dict *current_entry;
48 int ch;
49
50 setvbuf (stdout, NULL, _IOLBF, 0);
51 while ((ch = getopt (argc, argv, "f:")) != -1) {
52 switch (ch) {
53 case 'f':
54 filename = optarg;
55 break;
56 default:
57 usage (program_name);
58 exit (1);
59 }
60 }
61 if (filename == NULL) {
62 usage (program_name);
63 exit(1);
64 }
65 FH = fopen (filename, "r");
66 current_entry = load_dict (FH);
67
68 while (fgets (line, sizeof (line), stdin)) {
69 if ((cp = strchr (line, '\n')) == NULL) {
70 /* too large message received.. skip and deny */
71 fprintf(stderr, "%s: ERROR: Too large: %s\n", argv[0], line);
72 while (fgets(line, sizeof(line), stdin)) {
73 fprintf(stderr, "%s: ERROR: Too large..: %s\n", argv[0], line);
74 if (strchr(line, '\n') != NULL)
75 break;
76 }
77 goto error;
78 }
79 *cp = '\0';
80 address = strtok (line, " \t");
81 username = strtok (NULL, " \t");
82 if (!address || !username) {
83 fprintf (stderr, "%s: unable to read tokens\n", argv[0]);
84 goto error;
85 }
86 rfc1738_unescape(address);
87 rfc1738_unescape(username);
88 #ifdef DEBUG
89 printf ("result: %d\n",
90 dict_lookup (current_entry, username, address));
91 #endif
92 if ((dict_lookup (current_entry, username, address)) != 0) {
93 printf ("OK\n");
94 } else {
95 error:
96 printf ("ERR\n");
97 }
98 }
99
100
101 fclose (FH);
102 return 0;
103 }