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.
47 void (*reset_signal(int signo, void (*func)(int)))(int)
50 struct sigaction act, oact;
52 act.sa_handler = func;
53 sigemptyset(&act.sa_mask);
55 #ifdef SA_INTERRUPT /* SunOS */
56 act.sa_flags |= SA_INTERRUPT;
58 if (sigaction(signo, &act, &oact) < 0)
60 return oact.sa_handler;
64 * re-set by calling the 'signal' function, which
65 * may cause infinite recursion and core dumps due to
68 * However, the system is too dumb to implement sigaction(),
69 * so we don't have a choice.
78 * Per-request data, added by modules...
80 struct request_data_t {
86 void (*free_opaque)(void *);
90 * Add opaque data (with a "free" function) to a REQUEST.
92 * The unique ptr is meant to be a malloc'd module configuration,
93 * and the unique integer allows the caller to have multiple
94 * opaque data associated with a REQUEST.
96 int request_data_add(REQUEST *request,
97 void *unique_ptr, int unique_int,
98 void *opaque, void (*free_opaque)(void *))
100 request_data_t *this, **last, *next;
103 * Some simple sanity checks.
105 if (!request || !opaque) return -1;
108 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
109 if (((*last)->unique_ptr == unique_ptr) &&
110 ((*last)->unique_int == unique_int)) {
115 if (this->opaque && /* free it, if necessary */
117 this->free_opaque(this->opaque);
118 break; /* replace the existing entry */
122 if (!this) this = rad_malloc(sizeof(*this));
123 memset(this, 0, sizeof(*this));
126 this->unique_ptr = unique_ptr;
127 this->unique_int = unique_int;
128 this->opaque = opaque;
129 this->free_opaque = free_opaque;
138 * Get opaque data from a request.
140 void *request_data_get(REQUEST *request,
141 void *unique_ptr, int unique_int)
143 request_data_t **last;
145 if (!request) return NULL;
147 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
148 if (((*last)->unique_ptr == unique_ptr) &&
149 ((*last)->unique_int == unique_int)) {
150 request_data_t *this = *last;
151 void *ptr = this->opaque;
154 * Remove the entry from the list, and free it.
158 return ptr; /* don't free it, the caller does that */
162 return NULL; /* wasn't found, too bad... */
167 * Get opaque data from a request without removing it.
169 void *request_data_reference(REQUEST *request,
170 void *unique_ptr, int unique_int)
172 request_data_t **last;
174 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
175 if (((*last)->unique_ptr == unique_ptr) &&
176 ((*last)->unique_int == unique_int)) {
177 request_data_t *this = *last;
178 void *ptr = this->opaque;
184 return NULL; /* wasn't found, too bad... */
189 * Free a REQUEST struct.
191 void request_free(REQUEST **request_ptr)
195 if ((request_ptr == NULL) || !*request_ptr)
198 request = *request_ptr;
200 rad_assert(!request->in_request_hash);
201 rad_assert(!request->in_proxy_hash);
202 rad_assert(!request->ev);
205 rad_free(&request->packet);
209 rad_free(&request->proxy);
213 rad_free(&request->reply);
216 if (request->proxy_reply)
217 rad_free(&request->proxy_reply);
220 if (request->config_items)
221 pairfree(&request->config_items);
223 request->username = NULL;
224 request->password = NULL;
227 request_data_t *this, *next;
229 for (this = request->data; this != NULL; this = next) {
231 if (this->opaque && /* free it, if necessary */
233 this->free_opaque(this->opaque);
236 request->data = NULL;
240 (request->root->refcount > 0)) {
241 request->root->refcount--;
242 request->root = NULL;
247 request->coa->parent = NULL;
248 rad_assert(request->coa->ev == NULL);
249 request_free(&request->coa);
252 if (request->parent && (request->parent->coa == request)) {
253 request->parent->coa = NULL;
258 request->magic = 0x01020304; /* set the request to be nonsense */
260 request->client = NULL;
262 request->home_server = NULL;
270 * Check a filename for sanity.
272 * Allow only uppercase/lowercase letters, numbers, and '-_/.'
274 int rad_checkfilename(const char *filename)
276 if (strspn(filename, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_/.") == strlen(filename)) {
284 * Create possibly many directories.
286 * Note that the input directory name is NOT a constant!
287 * This is so that IF an error is returned, the 'directory' ptr
288 * points to the name of the file which caused the error.
290 int rad_mkdir(char *directory, int mode)
297 * If the directory exists, don't do anything.
299 if (stat(directory, &st) == 0) {
304 * Look for the LAST directory name. Try to create that,
305 * failing on any error.
307 p = strrchr(directory, FR_DIR_SEP);
310 rcode = rad_mkdir(directory, mode);
313 * On error, we leave the directory name as the
314 * one which caused the error.
321 * Reset the directory delimiter, and go ask
322 * the system to make the directory.
330 * Having done everything successfully, we do the
331 * system call to actually go create the directory.
333 return mkdir(directory, mode);
338 * Module malloc() call, which does stuff if the malloc fails.
340 * This call ALWAYS succeeds!
342 void *rad_malloc(size_t size)
344 void *ptr = malloc(size);
347 radlog(L_ERR|L_CONS, "no memory");
355 * Logs an error message and aborts the program
359 void NEVER_RETURNS rad_assert_fail (const char *file, unsigned int line,
362 radlog(L_ERR, "ASSERT FAILED %s[%u]: %s", file, line, expr);
368 * Create a new REQUEST data structure.
370 REQUEST *request_alloc(void)
374 request = rad_malloc(sizeof(REQUEST));
375 memset(request, 0, sizeof(REQUEST));
377 request->magic = REQUEST_MAGIC;
380 request->proxy = NULL;
382 request->reply = NULL;
384 request->proxy_reply = NULL;
386 request->config_items = NULL;
387 request->username = NULL;
388 request->password = NULL;
389 request->timestamp = time(NULL);
390 request->options = RAD_REQUEST_OPTION_NONE;
392 request->module = "";
393 request->component = "";
394 if (debug_flag) request->radlog = radlog_request;
401 * Create a new REQUEST, based on an old one.
403 * This function allows modules to inject fake requests
404 * into the server, for tunneled protocols like TTLS & PEAP.
406 REQUEST *request_alloc_fake(REQUEST *request)
410 fake = request_alloc();
412 fake->number = request->number;
413 #ifdef HAVE_PTHREAD_H
414 fake->child_pid = request->child_pid;
416 fake->parent = request;
417 fake->root = request->root;
418 fake->client = request->client;
419 fake->process = request->process;
422 * For new server support.
424 * FIXME: Key instead off of a "virtual server" data structure.
426 * FIXME: Permit different servers for inner && outer sessions?
428 fake->server = request->server;
430 fake->packet = rad_alloc(0);
436 fake->reply = rad_alloc(0);
442 fake->master_state = REQUEST_ACTIVE;
443 fake->child_state = REQUEST_RUNNING;
446 * Fill in the fake request.
448 fake->packet->sockfd = -1;
449 fake->packet->src_ipaddr = request->packet->src_ipaddr;
450 fake->packet->src_port = request->packet->src_port;
451 fake->packet->dst_ipaddr = request->packet->dst_ipaddr;
452 fake->packet->dst_port = 0;
455 * This isn't STRICTLY required, as the fake request MUST NEVER
456 * be put into the request list. However, it's still reasonable
459 fake->packet->id = fake->number & 0xff;
460 fake->packet->code = request->packet->code;
461 fake->timestamp = request->timestamp;
464 * Required for new identity support
466 fake->listener = request->listener;
469 * Fill in the fake reply, based on the fake request.
471 fake->reply->sockfd = fake->packet->sockfd;
472 fake->reply->src_ipaddr = fake->packet->dst_ipaddr;
473 fake->reply->src_port = fake->packet->dst_port;
474 fake->reply->dst_ipaddr = fake->packet->src_ipaddr;
475 fake->reply->dst_port = fake->packet->src_port;
476 fake->reply->id = fake->packet->id;
477 fake->reply->code = 0; /* UNKNOWN code */
480 * Copy debug information.
482 fake->options = request->options;
483 fake->radlog = request->radlog;
489 REQUEST *request_alloc_coa(REQUEST *request)
491 if (!request || request->coa) return NULL;
493 request->coa = request_alloc_fake(request);
494 request->coa->packet->code = 0; /* unknown, as of yet */
495 request->coa->child_state = REQUEST_RUNNING;
496 request->coa->proxy = rad_alloc(0);
503 * Copy a quoted string.
505 int rad_copy_string(char *to, const char *from)
517 } while (*from && (*from != quote));
519 if (*from != quote) return -1; /* not properly quoted */
532 int rad_copy_variable(char *to, const char *from)
544 sublen = rad_copy_string(to, from);
545 if (sublen < 0) return sublen;
550 case '}': /* end of variable expansion */
554 return length; /* proper end of variable */
562 case '%': /* start of variable expansion */
563 if (from[1] == '{') {
567 sublen = rad_copy_variable(to, from);
568 if (sublen < 0) return sublen;
572 } /* else FIXME: catch %%{ ?*/
582 } /* loop over the input string */
585 * We ended the string before a trailing '}'