2 * util.c Various utility functions.
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.
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.
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
20 * Copyright 2000,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/rad_assert.h>
36 * The signal() function in Solaris 2.5.1 sets SA_NODEFER in
37 * sa_flags, which causes grief if signal() is called in the
38 * handler before the cause of the signal has been cleared.
39 * (Infinite recursion).
41 * The same problem appears on HPUX, so we avoid it, if we can.
43 * Using sigaction() to reset the signal handler fixes the problem,
44 * so where available, we prefer that solution.
46 void (*reset_signal(int signo, void (*func)(int)))(int)
49 struct sigaction act, oact;
51 act.sa_handler = func;
52 sigemptyset(&act.sa_mask);
54 #ifdef SA_INTERRUPT /* SunOS */
55 act.sa_flags |= SA_INTERRUPT;
57 if (sigaction(signo, &act, &oact) < 0)
59 return oact.sa_handler;
63 * re-set by calling the 'signal' function, which
64 * may cause infinite recursion and core dumps due to
67 * However, the system is too dumb to implement sigaction(),
68 * so we don't have a choice.
75 * Per-request data, added by modules...
77 struct request_data_t {
83 void (*free_opaque)(void *);
87 * Add opaque data (with a "free" function) to a REQUEST.
89 * The unique ptr is meant to be a malloc'd module configuration,
90 * and the unique integer allows the caller to have multiple
91 * opaque data associated with a REQUEST.
93 int request_data_add(REQUEST *request,
94 void *unique_ptr, int unique_int,
95 void *opaque, void (*free_opaque)(void *))
97 request_data_t *this, **last, *next;
100 * Some simple sanity checks.
102 if (!request || !opaque) return -1;
105 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
106 if (((*last)->unique_ptr == unique_ptr) &&
107 ((*last)->unique_int == unique_int)) {
112 if (this->opaque && /* free it, if necessary */
114 this->free_opaque(this->opaque);
115 break; /* replace the existing entry */
119 if (!this) this = rad_malloc(sizeof(*this));
120 memset(this, 0, sizeof(*this));
123 this->unique_ptr = unique_ptr;
124 this->unique_int = unique_int;
125 this->opaque = opaque;
126 this->free_opaque = free_opaque;
135 * Get opaque data from a request.
137 void *request_data_get(REQUEST *request,
138 void *unique_ptr, int unique_int)
140 request_data_t **last;
142 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
143 if (((*last)->unique_ptr == unique_ptr) &&
144 ((*last)->unique_int == unique_int)) {
145 request_data_t *this = *last;
146 void *ptr = this->opaque;
149 * Remove the entry from the list, and free it.
153 return ptr; /* don't free it, the caller does that */
157 return NULL; /* wasn't found, too bad... */
162 * Get opaque data from a request without removing it.
164 void *request_data_reference(REQUEST *request,
165 void *unique_ptr, int unique_int)
167 request_data_t **last;
169 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
170 if (((*last)->unique_ptr == unique_ptr) &&
171 ((*last)->unique_int == unique_int)) {
172 request_data_t *this = *last;
173 void *ptr = this->opaque;
179 return NULL; /* wasn't found, too bad... */
184 * Free a REQUEST struct.
186 void request_free(REQUEST **request_ptr)
190 if ((request_ptr == NULL) || !*request_ptr)
193 request = *request_ptr;
196 * If there's a thread currently active on this request,
199 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
202 rad_free(&request->packet);
205 rad_free(&request->proxy);
208 rad_free(&request->reply);
210 if (request->proxy_reply)
211 rad_free(&request->proxy_reply);
213 if (request->config_items)
214 pairfree(&request->config_items);
216 request->username = NULL;
217 request->password = NULL;
220 request_data_t *this, *next;
222 for (this = request->data; this != NULL; this = next) {
224 if (this->opaque && /* free it, if necessary */
226 this->free_opaque(this->opaque);
229 request->data = NULL;
233 request->magic = 0x01020304; /* set the request to be nonsense */
234 strcpy(request->secret, "REQUEST-DELETED");
242 * Check a filename for sanity.
244 * Allow only uppercase/lowercase letters, numbers, and '-_/.'
246 int rad_checkfilename(const char *filename)
248 if (strspn(filename, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_/.") == strlen(filename)) {
256 * Create possibly many directories.
258 * Note that the input directory name is NOT a constant!
259 * This is so that IF an error is returned, the 'directory' ptr
260 * points to the name of the file which caused the error.
262 int rad_mkdir(char *directory, int mode)
269 * If the directory exists, don't do anything.
271 if (stat(directory, &st) == 0) {
276 * Look for the LAST directory name. Try to create that,
277 * failing on any error.
279 p = strrchr(directory, '/');
282 rcode = rad_mkdir(directory, mode);
285 * On error, we leave the directory name as the
286 * one which caused the error.
293 * Reset the directory delimiter, and go ask
294 * the system to make the directory.
302 * Having done everything successfully, we do the
303 * system call to actually go create the directory.
305 return mkdir(directory, mode);
310 * Module malloc() call, which does stuff if the malloc fails.
312 * This call ALWAYS succeeds!
314 void *rad_malloc(size_t size)
316 void *ptr = malloc(size);
319 radlog(L_ERR|L_CONS, "no memory");
327 * Logs an error message and aborts the program
331 void NEVER_RETURNS rad_assert_fail (const char *file, unsigned int line)
333 radlog(L_ERR|L_CONS, "Assertion failed in %s, line %u", file, line);
339 * Create a new REQUEST data structure.
341 REQUEST *request_alloc(void)
345 request = rad_malloc(sizeof(REQUEST));
346 memset(request, 0, sizeof(REQUEST));
348 request->magic = REQUEST_MAGIC;
350 request->proxy = NULL;
351 request->reply = NULL;
352 request->proxy_reply = NULL;
353 request->config_items = NULL;
354 request->username = NULL;
355 request->password = NULL;
356 request->timestamp = time(NULL);
357 request->child_pid = NO_SUCH_CHILD_PID;
358 request->options = RAD_REQUEST_OPTION_NONE;
365 * Create a new REQUEST, based on an old one.
367 * This function allows modules to inject fake requests
368 * into the server, for tunneled protocols like TTLS & PEAP.
370 REQUEST *request_alloc_fake(REQUEST *oldreq)
374 request = request_alloc();
376 request->number = oldreq->number;
377 request->child_pid = NO_SUCH_CHILD_PID;
379 request->packet = rad_alloc(0);
380 rad_assert(request->packet != NULL);
382 request->reply = rad_alloc(0);
383 rad_assert(request->reply != NULL);
386 * Fill in the fake request packet.
388 request->packet->sockfd = -1;
389 request->packet->src_ipaddr.af = AF_INET;
390 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_LOOPBACK);
391 request->packet->dst_ipaddr = request->packet->src_ipaddr;
392 request->packet->src_port = request->number >> 8;
395 * This field is used by the rest of the code to notice that the
396 * request is "internal", and not from a real client.
398 request->packet->dst_port = 0;
401 * This isn't STRICTLY required, as the fake request SHOULD NEVER
402 * be put into the request list. However, it's still reasonable
405 request->packet->id = request->number & 0xff;
406 request->packet->code = oldreq->packet->code;
407 request->timestamp = oldreq->timestamp;
410 * Fill in the fake reply, based on the fake request.
412 request->reply->sockfd = request->packet->sockfd;
413 request->reply->dst_ipaddr = request->packet->src_ipaddr;
414 request->reply->dst_port = request->packet->src_port;
415 request->reply->id = request->packet->id;
416 request->reply->code = 0; /* UNKNOWN code */
423 * Copy a quoted string.
425 int rad_copy_string(char *to, const char *from)
437 } while (*from && (*from != quote));
439 if (*from != quote) return -1; /* not properly quoted */
452 int rad_copy_variable(char *to, const char *from)
464 sublen = rad_copy_string(to, from);
465 if (sublen < 0) return sublen;
470 case '}': /* end of variable expansion */
474 return length; /* proper end of variable */
482 case '%': /* start of variable expansion */
483 if (from[1] == '{') {
487 sublen = rad_copy_variable(to, from);
488 if (sublen < 0) return sublen;
492 } /* else FIXME: catch %%{ ?*/
502 } /* loop over the input string */
505 * We ended the string before a trailing '}'