Debian Codegraph @

pppd/plugins/radius/buildreq.c

raw
1 /*
2 * Copyright (C) 1995,1997 Lars Fenneberg
3 *
4 * See the file COPYRIGHT for the respective terms and conditions.
5 * If the file is missing contact me at [email protected]
6 * and I'll send you a copy.
7 *
8 */
9
10 #include <includes.h>
11 #include <radiusclient.h>
12
13 unsigned char rc_get_seqnbr(void);
14
15 /*
16 * Function: rc_get_nas_id
17 *
18 * Purpose: fills in NAS-Identifier or NAS-IP-Address in request
19 *
20 */
21
22 int rc_get_nas_id(VALUE_PAIR **sendpairs)
23 {
24 UINT4 client_id;
25 char *nasid;
26
27 nasid = rc_conf_str("nas_identifier");
28 if (strlen(nasid)) {
29 /*
30 * Fill in NAS-Identifier
31 */
32 if (rc_avpair_add(sendpairs, PW_NAS_IDENTIFIER, nasid, 0,
33 VENDOR_NONE) == NULL)
34 return (ERROR_RC);
35
36 return (OK_RC);
37
38 } else {
39 /*
40 * Fill in NAS-IP-Address
41 */
42 if ((client_id = rc_own_ipaddress()) == 0)
43 return (ERROR_RC);
44
45 if (rc_avpair_add(sendpairs, PW_NAS_IP_ADDRESS, &client_id,
46 0, VENDOR_NONE) == NULL)
47 return (ERROR_RC);
48 }
49
50 return (OK_RC);
51 }
52
53 /*
54 * Function: rc_buildreq
55 *
56 * Purpose: builds a skeleton RADIUS request using information from the
57 * config file.
58 *
59 */
60
61 void rc_buildreq(SEND_DATA *data, int code, char *server, unsigned short port,
62 int timeout, int retries)
63 {
64 data->server = server;
65 data->svc_port = port;
66 data->seq_nbr = rc_get_seqnbr();
67 data->timeout = timeout;
68 data->retries = retries;
69 data->code = code;
70 }
71
72 /*
73 * Function: rc_guess_seqnbr
74 *
75 * Purpose: return a random sequence number
76 *
77 */
78
79 static unsigned char rc_guess_seqnbr(void)
80 {
81 return (unsigned char)(magic() & UCHAR_MAX);
82 }
83
84 /*
85 * Function: rc_get_seqnbr
86 *
87 * Purpose: generate a sequence number
88 *
89 */
90
91 unsigned char rc_get_seqnbr(void)
92 {
93 FILE *sf;
94 int tries = 1;
95 int seq_nbr, pos, ret;
96 char *seqfile = rc_conf_str("seqfile");
97
98 if ((sf = fopen(seqfile, "a+")) == NULL)
99 {
100 error("rc_get_seqnbr: couldn't open sequence file %s: %s", seqfile, strerror(errno));
101 /* well, so guess a sequence number */
102 return rc_guess_seqnbr();
103 }
104
105 while (do_lock_exclusive(fileno(sf))!= 0)
106 {
107 if (errno != EWOULDBLOCK) {
108 error("rc_get_seqnbr: flock failure: %s: %s", seqfile, strerror(errno));
109 fclose(sf);
110 return rc_guess_seqnbr();
111 }
112 tries++;
113 if (tries <= 10)
114 rc_mdelay(500);
115 else
116 break;
117 }
118
119 if (tries > 10) {
120 error("rc_get_seqnbr: couldn't get lock after %d tries: %s", tries-1, seqfile);
121 fclose(sf);
122 return rc_guess_seqnbr();
123 }
124
125 pos = ftell(sf);
126 rewind(sf);
127 if (fscanf(sf, "%d", &seq_nbr) != 1) {
128 if (pos != ftell(sf)) {
129 /* file was not empty */
130 error("rc_get_seqnbr: fscanf failure: %s", seqfile);
131 }
132 seq_nbr = rc_guess_seqnbr();
133 }
134
135 rewind(sf);
136 ret = ftruncate(fileno(sf),0);
137 if (ret != 0) {
138 error("rc_get_seqnbr: couldn't truncate sequence file, %m");
139 }
140 fprintf(sf,"%d\n", (seq_nbr+1) & UCHAR_MAX);
141
142 fflush(sf); /* fflush because a process may read it between the do_unlock and fclose */
143
144 if (do_unlock(fileno(sf)) != 0)
145 error("rc_get_seqnbr: couldn't release lock on %s: %s", seqfile, strerror(errno));
146
147 fclose(sf);
148
149 return (unsigned char)seq_nbr;
150 }
151
152 /*
153 * Function: rc_auth
154 *
155 * Purpose: Builds an authentication request for port id client_port
156 * with the value_pairs send and submits it to a server
157 *
158 * Returns: received value_pairs in received, messages from the server in msg
159 * and 0 on success, negative on failure as return value
160 *
161 */
162
163 int rc_auth(UINT4 client_port, VALUE_PAIR *send, VALUE_PAIR **received,
164 char *msg, REQUEST_INFO *info)
165 {
166 SERVER *authserver = rc_conf_srv("authserver");
167
168 if (!authserver) {
169 return (ERROR_RC);
170 }
171 return rc_auth_using_server(authserver, client_port, send, received,
172 msg, info);
173 }
174
175 /*
176 * Function: rc_auth_using_server
177 *
178 * Purpose: Builds an authentication request for port id client_port
179 * with the value_pairs send and submits it to a server. You
180 * explicitly supply a server list.
181 *
182 * Returns: received value_pairs in received, messages from the server in msg
183 * and 0 on success, negative on failure as return value
184 *
185 */
186
187 int rc_auth_using_server(SERVER *authserver,
188 UINT4 client_port,
189 VALUE_PAIR *send,
190 VALUE_PAIR **received,
191 char *msg, REQUEST_INFO *info)
192 {
193 SEND_DATA data;
194 int result;
195 int i;
196 int timeout = rc_conf_int("radius_timeout");
197 int retries = rc_conf_int("radius_retries");
198
199 data.send_pairs = send;
200 data.receive_pairs = NULL;
201
202 /*
203 * Fill in NAS-IP-Address or NAS-Identifier
204 */
205
206 if (rc_get_nas_id(&(data.send_pairs)) == ERROR_RC)
207 return (ERROR_RC);
208
209 /*
210 * Fill in NAS-Port
211 */
212
213 if (rc_avpair_add(&(data.send_pairs), PW_NAS_PORT, &client_port, 0, VENDOR_NONE) == NULL)
214 return (ERROR_RC);
215
216 result = ERROR_RC;
217 for(i=0; (i<authserver->max) && (result != OK_RC) && (result != BADRESP_RC)
218 ; i++)
219 {
220 if (data.receive_pairs != NULL) {
221 rc_avpair_free(data.receive_pairs);
222 data.receive_pairs = NULL;
223 }
224 rc_buildreq(&data, PW_ACCESS_REQUEST, authserver->name[i],
225 authserver->port[i], timeout, retries);
226
227 result = rc_send_server (&data, msg, info);
228 }
229
230 *received = data.receive_pairs;
231
232 return result;
233 }
234
235 /*
236 * Function: rc_auth_proxy
237 *
238 * Purpose: Builds an authentication request
239 * with the value_pairs send and submits it to a server.
240 * Works for a proxy; does not add IP address, and does
241 * does not rely on config file.
242 *
243 * Returns: received value_pairs in received, messages from the server in msg
244 * and 0 on success, negative on failure as return value
245 *
246 */
247
248 int rc_auth_proxy(VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
249 {
250 SEND_DATA data;
251 int result;
252 int i;
253 SERVER *authserver = rc_conf_srv("authserver");
254 int timeout = rc_conf_int("radius_timeout");
255 int retries = rc_conf_int("radius_retries");
256
257 data.send_pairs = send;
258 data.receive_pairs = NULL;
259
260 result = ERROR_RC;
261 for(i=0; (i<authserver->max) && (result != OK_RC) && (result != BADRESP_RC)
262 ; i++)
263 {
264 if (data.receive_pairs != NULL) {
265 rc_avpair_free(data.receive_pairs);
266 data.receive_pairs = NULL;
267 }
268 rc_buildreq(&data, PW_ACCESS_REQUEST, authserver->name[i],
269 authserver->port[i], timeout, retries);
270
271 result = rc_send_server (&data, msg, NULL);
272 }
273
274 *received = data.receive_pairs;
275
276 return result;
277 }
278
279
280 /*
281 * Function: rc_acct_using_server
282 *
283 * Purpose: Builds an accounting request for port id client_port
284 * with the value_pairs send. You explicitly supply server list.
285 *
286 * Remarks: NAS-Identifier/NAS-IP-Address, NAS-Port and Acct-Delay-Time get
287 * filled in by this function, the rest has to be supplied.
288 */
289
290 int rc_acct_using_server(SERVER *acctserver,
291 UINT4 client_port,
292 VALUE_PAIR *send)
293 {
294 SEND_DATA data;
295 VALUE_PAIR *adt_vp;
296 int result;
297 struct timeval start_time, dtime;
298 char msg[4096];
299 int i;
300 int timeout = rc_conf_int("radius_timeout");
301 int retries = rc_conf_int("radius_retries");
302
303 data.send_pairs = send;
304 data.receive_pairs = NULL;
305
306 /*
307 * Fill in NAS-IP-Address or NAS-Identifier
308 */
309
310 if (rc_get_nas_id(&(data.send_pairs)) == ERROR_RC)
311 return (ERROR_RC);
312
313 /*
314 * Fill in NAS-Port
315 */
316
317 if (rc_avpair_add(&(data.send_pairs), PW_NAS_PORT, &client_port, 0, VENDOR_NONE) == NULL)
318 return (ERROR_RC);
319
320 /*
321 * Fill in Acct-Delay-Time
322 */
323
324 dtime.tv_sec = 0;
325 if ((adt_vp = rc_avpair_add(&(data.send_pairs), PW_ACCT_DELAY_TIME, &dtime.tv_sec, 0, VENDOR_NONE)) == NULL)
326 return (ERROR_RC);
327
328 ppp_get_time(&start_time);
329 result = ERROR_RC;
330 for(i=0; (i<acctserver->max) && (result != OK_RC) && (result != BADRESP_RC)
331 ; i++)
332 {
333 if (data.receive_pairs != NULL) {
334 rc_avpair_free(data.receive_pairs);
335 data.receive_pairs = NULL;
336 }
337 rc_buildreq(&data, PW_ACCOUNTING_REQUEST, acctserver->name[i],
338 acctserver->port[i], timeout, retries);
339
340 ppp_get_time(&dtime);
341 dtime.tv_sec -= start_time.tv_sec;
342 rc_avpair_assign(adt_vp, &dtime.tv_sec, 0);
343
344 result = rc_send_server (&data, msg, NULL);
345 }
346
347 rc_avpair_free(data.receive_pairs);
348
349 return result;
350 }
351
352 /*
353 * Function: rc_acct
354 *
355 * Purpose: Builds an accounting request for port id client_port
356 * with the value_pairs send
357 *
358 * Remarks: NAS-Identifier/NAS-IP-Address, NAS-Port and Acct-Delay-Time get
359 * filled in by this function, the rest has to be supplied.
360 */
361
362 int rc_acct(UINT4 client_port, VALUE_PAIR *send)
363 {
364 SERVER *acctserver = rc_conf_srv("acctserver");
365 if (!acctserver) return (ERROR_RC);
366
367 return rc_acct_using_server(acctserver, client_port, send);
368 }
369
370 /*
371 * Function: rc_acct_proxy
372 *
373 * Purpose: Builds an accounting request with the value_pairs send
374 *
375 */
376
377 int rc_acct_proxy(VALUE_PAIR *send)
378 {
379 SEND_DATA data;
380 int result;
381 char msg[4096];
382 int i;
383 SERVER *acctserver = rc_conf_srv("authserver");
384 int timeout = rc_conf_int("radius_timeout");
385 int retries = rc_conf_int("radius_retries");
386
387 data.send_pairs = send;
388 data.receive_pairs = NULL;
389
390 result = ERROR_RC;
391 for(i=0; (i<acctserver->max) && (result != OK_RC) && (result != BADRESP_RC)
392 ; i++)
393 {
394 if (data.receive_pairs != NULL) {
395 rc_avpair_free(data.receive_pairs);
396 data.receive_pairs = NULL;
397 }
398 rc_buildreq(&data, PW_ACCOUNTING_REQUEST, acctserver->name[i],
399 acctserver->port[i], timeout, retries);
400
401 result = rc_send_server (&data, msg, NULL);
402 }
403
404 rc_avpair_free(data.receive_pairs);
405
406 return result;
407 }
408
409 /*
410 * Function: rc_check
411 *
412 * Purpose: ask the server hostname on the specified port for a
413 * status message
414 *
415 */
416
417 int rc_check(char *host, unsigned short port, char *msg)
418 {
419 SEND_DATA data;
420 int result;
421 UINT4 service_type;
422 int timeout = rc_conf_int("radius_timeout");
423 int retries = rc_conf_int("radius_retries");
424
425 data.send_pairs = data.receive_pairs = NULL;
426
427 /*
428 * Fill in NAS-IP-Address or NAS-Identifier,
429 * although it isn't neccessary
430 */
431
432 if (rc_get_nas_id(&(data.send_pairs)) == ERROR_RC)
433 return (ERROR_RC);
434
435 /*
436 * Fill in Service-Type
437 */
438
439 service_type = PW_ADMINISTRATIVE;
440 rc_avpair_add(&(data.send_pairs), PW_SERVICE_TYPE, &service_type, 0, VENDOR_NONE);
441
442 rc_buildreq(&data, PW_STATUS_SERVER, host, port, timeout, retries);
443 result = rc_send_server (&data, msg, NULL);
444
445 rc_avpair_free(data.receive_pairs);
446
447 return result;
448 }