Signed / unsigned fixes and function prototypes
[freeradius.git] / src / main / radconf2xml.c
1 /*
2  * radconf2xml.c        Converts radiusd.conf to XML.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2008   The FreeRADIUS server project
21  * Copyright 2008   Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/radpaths.h>
29
30 #ifdef HAVE_GETOPT_H
31 #include <getopt.h>
32 #endif
33
34 /*
35  *      For configuration file stuff.
36  */
37 char *radius_dir = RADDBDIR;
38 const char *progname = "radconf2xml";
39
40 /*
41  *      The rest of this is because the conffile.c, etc. assume
42  *      they're running inside of the server.  And we don't (yet)
43  *      have a "libfreeradius-server", or "libfreeradius-util".
44  */
45 int debug_flag = 0;
46 struct main_config_t mainconfig;
47 char *request_log_file = NULL;
48 char *debug_log_file = NULL;
49 int radius_xlat(UNUSED char *out, UNUSED int outlen, UNUSED const char *fmt,
50                 UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
51 {
52         return -1;
53 }
54
55 static int usage(void)
56 {
57         printf("Usage: %s [ -d raddb_dir ] [ -o output_file ] [ -n name ]\n", progname);
58         printf("  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
59         printf("  -n name         Read raddb/name.conf instead of raddb/radiusd.conf\n");
60         printf("  -o output_file  File where XML output will be written.\n");
61
62         exit(1);
63 }
64
65 int main(int argc, char **argv)
66 {
67         int argval;
68         CONF_SECTION *cs;
69         const char *file = NULL;
70         const char *name = "radiusd";
71         FILE *fp;
72         char buffer[2048];
73
74         if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
75                 progname = argv[0];
76         else
77                 progname++;
78
79         while ((argval = getopt(argc, argv, "d:ho:n:")) != EOF) {
80                 switch(argval) {
81                 case 'd':
82                         if (file) {
83                                 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
84                                 exit(1);
85                         }
86                         radius_dir = optarg;
87                         break;
88
89                 default:
90                 case 'h':
91                         usage();
92                         break;
93
94                 case 'n':
95                         name = optarg;
96                         break;
97
98                 case 'o':
99                         file = optarg;
100                         break;
101                 }
102         }
103
104         snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
105         cs = cf_file_read(buffer);
106         if (!cs) {
107                 fprintf(stderr, "%s: Errors reading %s\n",
108                         progname, buffer);
109                 exit(1);
110         }
111
112         if (!file || (strcmp(file, "-") == 0)) {
113                 fp = stdout;
114         } else {
115                 fp = fopen(file, "w");
116                 if (!fp) {
117                         fprintf(stderr, "%s: Failed openng %s: %s\n",
118                                 progname, file, strerror(errno));
119                         exit(1);
120                 }
121         }
122
123         if (!cf_section2xml(fp, cs)) {
124                 if (fp != stdout) unlink(file);
125                 return 1;
126         }
127
128         return 0;
129 }