rlm_eap: add eap_chbind.c to build
[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 int check_config = FALSE;
55
56 static int usage(void)
57 {
58         printf("Usage: %s [ -d raddb_dir ] [ -o output_file ] [ -n name ]\n", progname);
59         printf("  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
60         printf("  -n name         Read raddb/name.conf instead of raddb/radiusd.conf\n");
61         printf("  -o output_file  File where XML output will be written.\n");
62
63         exit(1);
64 }
65
66 int main(int argc, char **argv)
67 {
68         int argval;
69         CONF_SECTION *cs;
70         const char *file = NULL;
71         const char *name = "radiusd";
72         FILE *fp;
73         char buffer[2048];
74
75         if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
76                 progname = argv[0];
77         else
78                 progname++;
79
80         while ((argval = getopt(argc, argv, "d:ho:n:")) != EOF) {
81                 switch(argval) {
82                 case 'd':
83                         if (file) {
84                                 fprintf(stderr, "%s: -d and -f cannot be used together.\n", progname);
85                                 exit(1);
86                         }
87                         radius_dir = optarg;
88                         break;
89
90                 default:
91                 case 'h':
92                         usage();
93                         break;
94
95                 case 'n':
96                         name = optarg;
97                         break;
98
99                 case 'o':
100                         file = optarg;
101                         break;
102                 }
103         }
104
105         snprintf(buffer, sizeof(buffer), "%s/%s.conf", radius_dir, name);
106         cs = cf_file_read(buffer);
107         if (!cs) {
108                 fprintf(stderr, "%s: Errors reading %s\n",
109                         progname, buffer);
110                 exit(1);
111         }
112
113         if (!file || (strcmp(file, "-") == 0)) {
114                 fp = stdout;
115                 file = NULL;
116         } else {
117                 fp = fopen(file, "w");
118                 if (!fp) {
119                         fprintf(stderr, "%s: Failed openng %s: %s\n",
120                                 progname, file, strerror(errno));
121                         exit(1);
122                 }
123         }
124
125         if (!cf_section2xml(fp, cs)) {
126                 if (file) unlink(file);
127                 return 1;
128         }
129
130         return 0;
131 }