Accept GNUTLS priority in configuration file.
[cascardo/rnetproxy.git] / ppmanager.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 **  
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **  
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 **  
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **  
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <gdbm.h>
25 #include <errno.h>
26
27 enum
28 {
29   ACTION_ADD,
30   ACTION_DEL,
31   ACTION_LIST
32 };
33
34 void
35 pp_dblistkeys (GDBM_FILE db)
36 {
37   datum key;
38   datum oldkey;
39   key = gdbm_firstkey (db);
40   while (key.dptr != NULL)
41     {
42       printf ("%s\n", key.dptr);
43       oldkey = key;
44       key = gdbm_nextkey (db, oldkey);
45       free (oldkey.dptr);
46     }
47 }
48
49 void
50 usage (char *programname)
51 {
52   fprintf (stderr, "%s {allow | deny} {add | del | list} [user]\n",
53            programname);
54   exit (1);
55 }
56
57 int
58 main (int argc, char **argv)
59 {
60   GDBM_FILE db;
61   int action;
62   char *db_fn;
63   char *username;
64   datum key;
65   datum data;
66   int r = 0;
67   if (argc < 3)
68     usage (argv[0]);
69   if (!strcmp (argv[1], "deny"))
70     db_fn = "/var/lib/popproxy/deny.db";
71   else if (!strcmp (argv[1], "allow"))
72     db_fn = "/var/lib/popproxy/allow.db";
73   else
74     usage (argv[0]);
75   if (!strcmp (argv[2], "add"))
76     action = ACTION_ADD;
77   else if (!strcmp (argv[2], "del"))
78     action = ACTION_DEL;
79   else if (!strcmp (argv[2], "list"))
80     action = ACTION_LIST;
81   else
82     usage (argv[0]);
83   if ((action == ACTION_ADD || action == ACTION_DEL) && argc < 4)
84     usage (argv[0]);
85   if (action != ACTION_LIST)
86     {
87       username = argv[3];
88       key.dptr = username;
89       key.dsize = strlen (username);
90       data.dptr =  "";
91       data.dsize = 1;
92     }
93   db = gdbm_open (db_fn, 0, GDBM_WRCREAT, 0600, NULL);
94   if (db == NULL)
95     {
96       fprintf (stderr, "Could not create database.\n");
97       exit (1);
98     }
99   switch (action)
100     {
101     case ACTION_ADD:
102       r = gdbm_store (db, key, data, GDBM_REPLACE);
103       break;
104     case ACTION_DEL:
105       r = gdbm_delete (db, key);
106       break;
107     case ACTION_LIST:
108       pp_dblistkeys (db);
109       r = 0;
110       break;
111     default:
112       fprintf (stderr, "Action unrecognized.\n");
113       r = 1;
114     }
115   gdbm_close (db);
116   return r;
117 }