24e2bc2b28d7dd386de1d6916e3bf9ac87f30688
[freeradius.git] / src / main / builddbm.c
1 /*
2  * builddbm.c   Build a DBM file from an ASCII users file.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2000  The FreeRADIUS server project
21  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  */
23
24 char sccsid[] =
25 "@(#)builddbm.c 2.2 Copyright 1999 Cistron Internet Services";
26
27 #include "autoconf.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include <stdio.h>
33 #include <strings.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #ifdef WITH_DBM
38 #       include <dbm.h>
39 #endif
40 #ifdef WITH_NDBM
41 #       include <ndbm.h>
42 #endif
43
44 #include "radiusd.h"
45
46 const char *progname;
47 int debug_flag;
48 char *radius_dir;
49 char *radlog_dir;
50
51 FILE *userfd;
52
53
54 /*
55  *      Print a list of VALUE_PAIRS into a string,
56  *      separated by comma's and closed off with a newline.
57  */
58 void makelist(char *out, int outlen, VALUE_PAIR *vp)
59 {
60         char *ptr;
61         int len;
62
63         ptr = out;
64         ptr[0] = 0;
65
66         while (vp && outlen > 3) {
67                 vp_prints(ptr, outlen, vp);
68                 strcat(ptr, ", ");
69                 len = strlen(ptr);
70                 outlen -= len + 2;
71                 ptr += len;
72                 vp = vp->next;
73         }
74         strcat(ptr, "\n");
75 }
76
77
78 int main(int argc, char **argv)
79 {
80         PAIR_LIST *users;
81         char name[MAX_STRING_LEN];
82         char content[4096];
83         int len;
84         datum named;
85         datum contentd;
86         int defno = 0;
87 #ifdef WITH_DBM
88         int fd;
89 #endif
90 #ifdef WITH_NDBM
91         DBM *dbm;
92 #endif
93
94         progname = argv[0];
95         radius_dir = ".";
96         librad_dodns = 0;
97
98         if (dict_init(RADDBDIR, RADIUS_DICTIONARY) < 0) {
99                 librad_perror("builddbm");
100                 return 1;
101         }
102
103         /*
104          *      Read the "users" file.
105          */
106         if ((users = pairlist_read(RADIUS_USERS, 1)) == NULL)
107                 exit(1);
108
109         /*
110          *      Initialize a new, empty database.
111          */
112         umask(077);
113 #ifdef WITH_DBM
114         if ((fd = open("users.pag", O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
115                 fprintf(stderr, "%s: Couldn't open users.pag for writing\n",
116                                 progname);
117                 exit(1);
118         }
119         close(fd);
120         if ((fd = open("users.dir", O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
121                 fprintf(stderr, "%s: Couldn't open users.dir for writing\n",
122                                 progname);
123                 exit(1);
124         }
125         close(fd);
126         if (dbminit("users") != 0) {
127                 fprintf(stderr, "%s: ", progname);
128                 perror("dbminit(users)");
129                 exit(1);
130         }
131 #endif
132 #ifdef WITH_NDBM
133         if ((dbm = dbm_open("users", O_RDWR|O_CREAT|O_TRUNC, 0600)) == NULL) {
134                 fprintf(stderr, "%s: ", progname);
135                 perror("dbm_open(users)");
136                 exit(1);
137         }
138 #endif
139
140         while (users) {
141
142                 makelist(content, sizeof(content), users->check);
143                 len = strlen(content);
144                 makelist(content + len, sizeof(content) - len, users->reply);
145
146                 strNcpy(name, users->name, sizeof(name));
147                 if (strcmp(name, "DEFAULT") == 0) {
148                         if (defno > 0)
149                                 sprintf(name, "DEFAULT%d", defno);
150                         defno++;
151                 }
152                 named.dptr = name;
153                 named.dsize = strlen(name);
154                 contentd.dptr = content;
155                 contentd.dsize = strlen(content);
156 #ifdef WITH_DBM
157                 if (store(named, contentd) != 0)
158 #endif
159 #ifdef WITH_NDBM
160                 if (dbm_store(dbm, named, contentd, DBM_INSERT) != 0)
161 #endif
162                 {
163                         fprintf(stderr, "%s: Couldn't store datum for %s\n",
164                                         progname, name);
165                         exit(1);
166                 }
167                 users = users->next;
168         }
169 #ifdef WITH_DBM
170         dbmclose();
171 #endif
172 #ifdef WITH_NDBM
173         dbm_close(dbm);
174 #endif
175         return 0;
176 }
177