2 * util.c Various utility functions.
4 * Version: @(#)util.c 2.20 17-Jul-1999 miquels@cistron.nl
8 "@(#)util.c 2.20 Copyright 1997-1999 Cistron Internet Services B.V.";
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
30 * Call getpwnam but cache the result.
32 struct passwd *rad_getpwnam(char *name)
34 static struct passwd *lastpwd;
35 static char lastname[64];
36 static time_t lasttime = 0;
41 if ((now <= lasttime + 5 ) && strncmp(name, lastname, 64) == 0)
44 strncpy(lastname, name, 63);
46 lastpwd = getpwnam(name);
52 #if defined (sun) && defined(__svr4__)
54 * The signal() function in Solaris 2.5.1 sets SA_NODEFER in
55 * sa_flags, which causes grief if signal() is called in the
56 * handler before the cause of the signal has been cleared.
57 * (Infinite recursion).
59 void (*sun_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;
77 * Free an AUTHREQ struct.
79 void request_free(REQUEST *request)
82 rad_free(request->packet);
84 rad_free(request->proxy);
90 * Build a reply radius packet, based on the request data.
92 RADIUS_PACKET *build_reply(int code, REQUEST *request,
93 VALUE_PAIR *vps, char *user_msg)
98 if ((rp = rad_alloc(0)) == NULL) {
99 fprintf(stderr, "out of memory\n");
102 rp->dst_ipaddr = request->packet->src_ipaddr;
103 rp->dst_port = request->packet->src_port;
104 rp->id = request->packet->id;
106 memcpy(rp->vector, request->packet->vector, sizeof(rp->vector));
107 rp->vps = paircopy(vps);
110 * Need to copy PROXY_PAIRS from request->packet->vps
112 if ((vp = paircopy2(request->packet->vps, PW_PROXY_STATE)) != NULL)
113 pairadd(&(rp->vps), vp);
115 if (user_msg && !(vp = paircreate(PW_REPLY_MESSAGE, PW_TYPE_STRING))) {
116 strcpy(vp->strvalue, user_msg);
117 vp->length = strlen(user_msg);
118 pairadd(&(rp->vps), vp);