All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
map_unit.c
Go to the documentation of this file.
1 /*
2  * radattr.c Map debugging tool.
3  *
4  * Version: $Id: d5708ca45ee5709c3a47f5f2b1e748ff0a875a86 $
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 2015 Alan DeKok <aland@freeradius.org>
21  */
22 
23 RCSID("$Id: d5708ca45ee5709c3a47f5f2b1e748ff0a875a86 $")
24 
25 #include <freeradius-devel/libradius.h>
26 
27 #include <freeradius-devel/conf.h>
28 #include <freeradius-devel/modpriv.h>
29 #include <freeradius-devel/modcall.h>
30 
31 #include <ctype.h>
32 
33 #ifdef HAVE_GETOPT_H
34 # include <getopt.h>
35 #endif
36 
37 #include <assert.h>
38 
39 #include <freeradius-devel/log.h>
40 
41 #include <sys/wait.h>
42 
43 /* Linker hacks */
44 
45 #ifdef HAVE_PTHREAD_H
46 pid_t rad_fork(void)
47 {
48  return fork();
49 }
50 
51 pid_t rad_waitpid(pid_t pid, int *status)
52 {
53  return waitpid(pid, status, 0);
54 }
55 #endif
56 
58 {
59  return RLM_MODULE_OK;
60 }
61 
62 char const *get_radius_dir(void)
63 {
64  return NULL;
65 }
66 
68 {
69  return NULL;
70 }
71 
73 {
74  return NULL;
75 }
76 
77 main_config_t main_config; //!< Main server configuration.
78 
79 /* Linker hacks */
80 
81 static void NEVER_RETURNS usage(void)
82 {
83  fprintf(stderr, "usage: map_unit [OPTS] filename ...\n");
84  fprintf(stderr, " -d <raddb> Set user dictionary directory (defaults to " RADDBDIR ").\n");
85  fprintf(stderr, " -D <dictdir> Set main dictionary directory (defaults to " DICTDIR ").\n");
86  fprintf(stderr, " -O <output_dir> Set output directory\n");
87  fprintf(stderr, " -x Debugging mode.\n");
88  fprintf(stderr, " -M Show program version information.\n");
89 
90  exit(1);
91 }
92 
93 static int process_file(char const *filename)
94 {
95  int rcode;
96  char const *name1, *name2;
97  CONF_SECTION *cs;
98  vp_map_t *head, *map;
99  char buffer[8192];
100 
101  memset(&main_config, 0, sizeof(main_config));
102 
103  main_config.config = cf_section_alloc(NULL, "main", NULL);
104  if (cf_file_read(main_config.config, filename) < 0) {
105  fprintf(stderr, "map_unit: Failed parsing %s\n",
106  filename);
107  exit(1);
108  }
109 
110  /*
111  * Always has to be an "update" section.
112  */
113  cs = cf_section_sub_find(main_config.config, "update");
114  if (!cs) {
115  talloc_free(main_config.config);
116  return -1;
117  }
118 
119  /*
120  * Convert the update section to a list of maps.
121  */
122  rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
123  if (rcode < 0) return -1; /* message already printed */
124  if (!head) {
125  cf_log_err_cs(cs, "'update' sections cannot be empty");
126  return -1;
127  }
128 
129  buffer[0] = '\t';
130 
131  name1 = cf_section_name1(cs);
132  name2 = cf_section_name2(cs);
133 
134  /*
135  * And print it all out.
136  */
137  if (!name2) {
138  printf("%s {\n", name1);
139  } else {
140  printf("%s %s {\n", name1, name2);
141  }
142 
143  for (map = head; map != NULL; map = map->next) {
144  map_snprint(buffer + 1, sizeof(buffer) - 1, map);
145  puts(buffer);
146  }
147  printf("}\n");
148 
149  talloc_free(main_config.config);
150  return 0;
151 }
152 
153 int main(int argc, char *argv[])
154 {
155  int c, rcode = 0;
156  bool report = false;
157  char const *radius_dir = RADDBDIR;
158  char const *dict_dir = DICTDIR;
159  fr_dict_t *dict = NULL;
160 
161 #ifndef NDEBUG
162  if (fr_fault_setup(getenv("PANIC_ACTION"), argv[0]) < 0) {
163  fr_perror("radattr");
164  exit(EXIT_FAILURE);
165  }
166 #endif
167 
168  while ((c = getopt(argc, argv, "d:D:xMh")) != EOF) switch (c) {
169  case 'd':
170  radius_dir = optarg;
171  break;
172  case 'D':
173  dict_dir = optarg;
174  break;
175  case 'x':
176  fr_debug_lvl++;
178  break;
179  case 'M':
180  report = true;
181  break;
182  case 'h':
183  default:
184  usage();
185  }
186  argc -= (optind - 1);
187  argv += (optind - 1);
188 
189  /*
190  * Mismatch between the binary and the libraries it depends on
191  */
193  fr_perror("radattr");
194  return 1;
195  }
196 
197  if (fr_dict_init(NULL, &dict, dict_dir, RADIUS_DICTIONARY, "radius") < 0) {
198  fr_perror("radattr");
199  return 1;
200  }
201 
202  if (fr_dict_read(dict, radius_dir, RADIUS_DICTIONARY) == -1) {
203  fr_perror("radattr");
204  return 1;
205  }
206 
207  if (argc < 2) {
208  rcode = process_file("-");
209 
210  } else {
211  rcode = process_file(argv[1]);
212  }
213 
214  if (report) {
215  talloc_free(dict);
216  fr_log_talloc_report(NULL);
217  }
218 
219  if (rcode < 0) rcode = 1; /* internal to Unix process return code */
220 
221  return rcode;
222 }
size_t map_snprint(char *out, size_t outlen, vp_map_t const *map)
Print a map to a string.
Definition: map.c:1494
static char const * dict_dir
Definition: radwho.c:53
module_instance_t * module_instantiate(UNUSED CONF_SECTION *modules, UNUSED char const *askedname)
Definition: map_unit.c:67
static char const * radius_dir
Path to raddb directory.
Definition: mainconfig.c:88
Main server configuration.
Definition: radiusd.h:108
The module is OK, continue.
Definition: radiusd.h:91
int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
Definition: dict.c:2291
static char const * name
#define UNUSED
Definition: libradius.h:134
int cf_file_read(CONF_SECTION *cs, char const *file)
Definition: conffile.c:3421
#define rad_waitpid(a, b)
Definition: radiusd.h:564
int fr_log_talloc_report(TALLOC_CTX *ctx)
Generate a talloc memory report for a context and print to stderr/stdout.
Definition: debug.c:810
static expr_map_t map[]
Definition: rlm_expr.c:169
int fr_debug_lvl
Definition: misc.c:40
#define rad_fork(n)
Definition: radiusd.h:563
struct vp_map * next
The next valuepair map.
Definition: map.h:55
int fr_fault_setup(char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition: debug.c:890
int map_afrom_cs(vp_map_t **out, CONF_SECTION *cs, pair_lists_t dst_list_def, pair_lists_t src_list_def, map_validate_t validate, void *ctx, unsigned int max) CC_HINT(nonnull(1
Vendors and attribute names.
Definition: dict.c:61
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition: version.c:38
static int comp(void const *a, void const *b)
Definition: rbmonkey.c:44
module_instance_t * module_instantiate_method(UNUSED CONF_SECTION *modules, UNUSED char const *name, UNUSED rlm_components_t *method)
Definition: map_unit.c:72
void void fr_perror(char const *,...) CC_HINT(format(printf
Attributes in incoming or internally proxied request.
Definition: tmpl.h:82
#define RADIUS_DICTIONARY
Definition: conf.h:7
void void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt,...) CC_HINT(format(printf
enum rlm_rcodes rlm_rcode_t
Return codes indicating the result of the module call.
CONF_SECTION * cf_section_sub_find(CONF_SECTION const *, char const *name)
Find a sub-section in a section.
Definition: conffile.c:3708
static int process_file(char const *filename)
Definition: map_unit.c:93
char const * cf_section_name1(CONF_SECTION const *cs)
Definition: conffile.c:3592
int main(int argc, char *argv[])
Definition: map_unit.c:153
CONF_SECTION * config
Root of the server config.
Definition: radiusd.h:110
int modcall_fixup_update(vp_map_t *map, void *ctx)
log_lvl_t rad_debug_lvl
Global debugging level.
Definition: log.c:49
enum rlm_components rlm_components_t
The different section components of the server.
CONF_SECTION * cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
Allocate a CONF_SECTION.
Definition: conffile.c:626
char const * get_radius_dir(void)
Get the global radius config directory.
Definition: map_unit.c:62
#define NEVER_RETURNS
Definition: libradius.h:133
static void NEVER_RETURNS usage(void)
Definition: map_unit.c:81
main_config_t main_config
Main server configuration.
Definition: map_unit.c:77
#define RCSID(id)
Definition: build.h:135
Value pair map.
Definition: map.h:46
int fr_dict_init(TALLOC_CTX *ctx, fr_dict_t **out, char const *dir, char const *fn, char const *name)
(re)initialize a protocol dictionary
Definition: dict.c:2148
rlm_rcode_t indexed_modcall(UNUSED rlm_components_t comp, UNUSED int idx, UNUSED REQUEST *request)
Definition: map_unit.c:57
char const * cf_section_name2(CONF_SECTION const *cs)
Definition: conffile.c:3601
#define RADIUSD_MAGIC_NUMBER
Definition: libradius.h:51