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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
25 #include <freeradius-devel/autoconf.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
44 #include <freeradius-devel/radiusd.h>
45 #include <freeradius-devel/rad_assert.h>
48 * The signal() function in Solaris 2.5.1 sets SA_NODEFER in
49 * sa_flags, which causes grief if signal() is called in the
50 * handler before the cause of the signal has been cleared.
51 * (Infinite recursion).
53 * The same problem appears on HPUX, so we avoid it, if we can.
55 * Using sigaction() to reset the signal handler fixes the problem,
56 * so where available, we prefer that solution.
58 void (*reset_signal(int signo, void (*func)(int)))(int)
61 struct sigaction act, oact;
63 act.sa_handler = func;
64 sigemptyset(&act.sa_mask);
66 #ifdef SA_INTERRUPT /* SunOS */
67 act.sa_flags |= SA_INTERRUPT;
69 if (sigaction(signo, &act, &oact) < 0)
71 return oact.sa_handler;
75 * re-set by calling the 'signal' function, which
76 * may cause infinite recursion and core dumps due to
79 * However, the system is too dumb to implement sigaction(),
80 * so we don't have a choice.
87 * Per-request data, added by modules...
89 struct request_data_t {
95 void (*free_opaque)(void *);
99 * Add opaque data (with a "free" function) to a REQUEST.
101 * The unique ptr is meant to be a malloc'd module configuration,
102 * and the unique integer allows the caller to have multiple
103 * opaque data associated with a REQUEST.
105 int request_data_add(REQUEST *request,
106 void *unique_ptr, int unique_int,
107 void *opaque, void (*free_opaque)(void *))
109 request_data_t *this, **last, *next;
112 * Some simple sanity checks.
114 if (!request || !opaque) return -1;
117 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
118 if (((*last)->unique_ptr == unique_ptr) &&
119 ((*last)->unique_int == unique_int)) {
124 if (this->opaque && /* free it, if necessary */
126 this->free_opaque(this->opaque);
127 break; /* replace the existing entry */
131 if (!this) this = rad_malloc(sizeof(*this));
132 memset(this, 0, sizeof(*this));
135 this->unique_ptr = unique_ptr;
136 this->unique_int = unique_int;
137 this->opaque = opaque;
138 this->free_opaque = free_opaque;
147 * Get opaque data from a request.
149 void *request_data_get(REQUEST *request,
150 void *unique_ptr, int unique_int)
152 request_data_t **last;
154 for (last = &(request->data); *last != NULL; last = &((*last)->next)) {
155 if (((*last)->unique_ptr == unique_ptr) &&
156 ((*last)->unique_int == unique_int)) {
157 request_data_t *this = *last;
158 void *ptr = this->opaque;
161 * Remove the entry from the list, and free it.
165 return ptr; /* don't free it, the caller does that */
169 return NULL; /* wasn't found, too bad... */
174 * Free a REQUEST struct.
176 void request_free(REQUEST **request_ptr)
180 if ((request_ptr == NULL) || !*request_ptr)
183 request = *request_ptr;
186 * If there's a thread currently active on this request,
189 rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
192 rad_free(&request->packet);
195 rad_free(&request->proxy);
198 rad_free(&request->reply);
200 if (request->proxy_reply)
201 rad_free(&request->proxy_reply);
203 if (request->config_items)
204 pairfree(&request->config_items);
206 request->username = NULL;
207 request->password = NULL;
210 request_data_t *this, *next;
212 for (this = request->data; this != NULL; this = next) {
214 if (this->opaque && /* free it, if necessary */
216 this->free_opaque(this->opaque);
219 request->data = NULL;
223 request->magic = 0x01020304; /* set the request to be nonsense */
224 strcpy(request->secret, "REQUEST-DELETED");
225 strcpy(request->proxysecret, "REQUEST-DELETED");
233 * Check a filename for sanity.
235 * Allow only uppercase/lowercase letters, numbers, and '-_/.'
237 int rad_checkfilename(const char *filename)
239 if (strspn(filename, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_/.") == strlen(filename)) {
247 * Create possibly many directories.
249 * Note that the input directory name is NOT a constant!
250 * This is so that IF an error is returned, the 'directory' ptr
251 * points to the name of the file which caused the error.
253 int rad_mkdir(char *directory, int mode)
260 * If the directory exists, don't do anything.
262 if (stat(directory, &st) == 0) {
267 * Look for the LAST directory name. Try to create that,
268 * failing on any error.
270 p = strrchr(directory, '/');
273 rcode = rad_mkdir(directory, mode);
276 * On error, we leave the directory name as the
277 * one which caused the error.
284 * Reset the directory delimiter, and go ask
285 * the system to make the directory.
293 * Having done everything successfully, we do the
294 * system call to actually go create the directory.
296 return mkdir(directory, mode);
301 * Module malloc() call, which does stuff if the malloc fails.
303 * This call ALWAYS succeeds!
305 void *rad_malloc(size_t size)
307 void *ptr = malloc(size);
310 radlog(L_ERR|L_CONS, "no memory");
318 * Logs an error message and aborts the program
322 void NEVER_RETURNS rad_assert_fail (const char *file, unsigned int line)
324 radlog(L_ERR|L_CONS, "Assertion failed in %s, line %u", file, line);
330 * Create a new REQUEST data structure.
332 REQUEST *request_alloc(void)
336 request = rad_malloc(sizeof(REQUEST));
337 memset(request, 0, sizeof(REQUEST));
339 request->magic = REQUEST_MAGIC;
341 request->proxy = NULL;
342 request->reply = NULL;
343 request->proxy_reply = NULL;
344 request->config_items = NULL;
345 request->username = NULL;
346 request->password = NULL;
347 request->timestamp = time(NULL);
348 request->child_pid = NO_SUCH_CHILD_PID;
349 request->container = NULL;
350 request->options = RAD_REQUEST_OPTION_NONE;
357 * Create a new REQUEST, based on an old one.
359 * This function allows modules to inject fake requests
360 * into the server, for tunneled protocols like TTLS & PEAP.
362 REQUEST *request_alloc_fake(REQUEST *oldreq)
366 request = request_alloc();
368 request->number = oldreq->number;
369 request->child_pid = NO_SUCH_CHILD_PID;
370 request->options = RAD_REQUEST_OPTION_FAKE_REQUEST;
372 request->packet = rad_alloc(0);
373 rad_assert(request->packet != NULL);
375 request->reply = rad_alloc(0);
376 rad_assert(request->reply != NULL);
379 * Fill in the fake request packet.
381 request->packet->sockfd = -1;
382 request->packet->src_ipaddr.af = AF_INET;
383 request->packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_LOOPBACK);
384 request->packet->dst_ipaddr = request->packet->src_ipaddr;
385 request->packet->src_port = request->number >> 8;
386 request->packet->dst_port = 0;
389 * This isn't STRICTLY required, as the fake request SHOULD NEVER
390 * be put into the request list. However, it's still reasonable
393 request->packet->id = request->number & 0xff;
394 request->packet->code = oldreq->packet->code;
395 request->timestamp = oldreq->timestamp;
398 * Fill in the fake reply, based on the fake request.
400 request->reply->sockfd = request->packet->sockfd;
401 request->reply->dst_ipaddr = request->packet->src_ipaddr;
402 request->reply->dst_port = request->packet->src_port;
403 request->reply->id = request->packet->id;
404 request->reply->code = 0; /* UNKNOWN code */
411 * Copy a quoted string.
413 int rad_copy_string(char *to, const char *from)
425 } while (*from && (*from != quote));
427 if (*from != quote) return -1; /* not properly quoted */
440 int rad_copy_variable(char *to, const char *from)
452 sublen = rad_copy_string(to, from);
453 if (sublen < 0) return sublen;
458 case '}': /* end of variable expansion */
462 return length; /* proper end of variable */
470 case '%': /* start of variable expansion */
471 if (from[1] == '{') {
475 sublen = rad_copy_variable(to, from);
476 if (sublen < 0) return sublen;
480 } /* else FIXME: catch %%{ ?*/
490 } /* loop over the input string */
493 * We ended the string before a trailing '}'