Debian Codegraph @

xpa/acl.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * acl.c -- xpa access control list management
8 *
9 */
10
11 #include <xpap.h>
12
13 /*
14 *----------------------------------------------------------------------------
15 *
16 *
17 * Private Routines
18 *
19 *
20 *----------------------------------------------------------------------------
21 */
22
23 /* this is the head of the global list -- too lazy to do anything more */
24 static XACL aclhead=NULL;
25
26 #ifdef ANSI_FUNC
27 static XACL
28 XPAAclLookup (char *xclass, char *name, unsigned int ip, int exact)
29 #else
30 static XACL XPAAclLookup(xclass, name, ip, exact)
31 char *xclass;
32 char *name;
33 unsigned int ip;
34 int exact;
35 #endif
36 {
37 XACL cur;
38 /* look for exact match */
39 for(cur=aclhead; cur!=NULL; cur=cur->next){
40 if( !strcmp(xclass, cur->xclass) &&
41 !strcmp(name, cur->name) &&
42 ((cur->ip == 0) || (cur->ip == ip)) ){
43 return(cur);
44 }
45 }
46 /* otherwise look for a template match (unless exact was specified) */
47 if( !exact ){
48 for(cur=aclhead; cur!=NULL; cur=cur->next){
49 if( tmatch(xclass, cur->xclass) &&
50 tmatch(name, cur->name) &&
51 ((cur->ip == 0) || (cur->ip == ip)) ){
52 return(cur);
53 }
54 }
55 }
56 return(NULL);
57 }
58
59 /*
60 *----------------------------------------------------------------------------
61 *
62 * Routine: XPAAclParse
63 *
64 * Purpose: parse acl list into components
65 *
66 * Returns: 0 on success, -1 on failure
67 *
68 *----------------------------------------------------------------------------
69 */
70 #ifdef ANSI_FUNC
71 static int
72 XPAAclParse (char *lbuf, char *xclass, char *name, unsigned int *ip,
73 char *acl, int len)
74 #else
75 static int XPAAclParse(lbuf, xclass, name, ip, acl, len)
76 char *lbuf;
77 char *xclass;
78 char *name;
79 unsigned int *ip;
80 char *acl;
81 int len;
82 #endif
83 {
84 char tbuf[SZ_LINE];
85 int lp=0;
86
87 /* class:name is required */
88 if( word(lbuf, tbuf, &lp) ){
89 XPAParseName(tbuf, xclass, name, len);
90 }
91 else
92 return(-1);
93
94 /* host is required but can be "*" for "all hosts" */
95 if( word(lbuf, tbuf, &lp) ){
96 if( !strcmp(tbuf, "*") )
97 *ip = 0;
98 else
99 *ip = gethostip(tbuf);
100 }
101 else
102 return(-1);
103
104 /* acl is required */
105 if( word(lbuf, tbuf, &lp) ){
106 if( !strcmp(tbuf, "+") )
107 strcpy(acl, XPA_ACLS);
108 else if( !strcmp(tbuf, "-") )
109 *acl = '\0';
110 else
111 strcpy(acl, tbuf);
112 return(0);
113 }
114 else
115 return(-1);
116 }
117
118 /*
119 *----------------------------------------------------------------------------
120 *
121 *
122 * Semi-Public Routines (used by command.c)
123 *
124 *
125 *----------------------------------------------------------------------------
126 */
127
128 /*
129 *----------------------------------------------------------------------------
130 *
131 * Routine: XPAReceiveAcl
132 *
133 * Purpose: add or modify the acl for this access point
134 *
135 * Returns: 0 for success, -1 for failure
136 *
137 *----------------------------------------------------------------------------
138 */
139 #ifdef ANSI_FUNC
140 int
141 XPAReceiveAcl (void *client_data, void *call_data, char *paramlist,
142 char *buf, size_t len)
143 #else
144 int XPAReceiveAcl(client_data, call_data, paramlist, buf, len)
145 void *client_data;
146 void *call_data;
147 char *paramlist;
148 char *buf;
149 size_t len;
150 #endif
151 {
152 XPA xpa = (XPA)call_data;
153 XPAComm comm;
154 int i;
155 int got;
156 char *s=NULL;
157 char lbuf[SZ_LINE];
158 char tbuf[SZ_LINE];
159
160 if( paramlist && *paramlist ){
161 s = paramlist;
162 while( isspace((int)*s) )
163 s++;
164 snprintf(tbuf, SZ_LINE, "%s:%s %s\n", xpa->xclass, xpa->name, s);
165 if( XPAAclEdit(tbuf) < 0 ){
166 snprintf(lbuf, SZ_LINE, "invalid acl: %s\n", tbuf);
167 XPAError(xpa, lbuf);
168 return(-1);
169 }
170 }
171 else{
172 while((XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPAShortTimeout())>0)&&
173 *lbuf ){
174 snprintf(tbuf, SZ_LINE, "%s:%s %s\n", xpa->xclass, xpa->name, lbuf);
175 got = XPAAclEdit(tbuf);
176 if( got < 0 ){
177 snprintf(lbuf, SZ_LINE, "invalid acl: %s\n", tbuf);
178 XPAError(xpa, lbuf);
179 return(-1);
180 }
181 }
182 }
183
184 /* reset all acl flags for this xpa so acl is recalculated */
185 for(comm=xpa->commhead; comm!=NULL; comm=comm->next){
186 for(i=0; i<4; i++){
187 comm->acl[i] = -1;
188 }
189 }
190
191 return(0);
192 }
193
194 /*
195 *----------------------------------------------------------------------------
196 *
197 * Routine: XPASendAcl
198 *
199 * Purpose: return the acl for this access point
200 *
201 * Returns: 0 for success, -1 for failure
202 *
203 *----------------------------------------------------------------------------
204 */
205 #ifdef ANSI_FUNC
206 int
207 XPASendAcl (void *client_data, void *call_data, char *paramlist,
208 char **buf, size_t *len)
209 #else
210 int XPASendAcl(client_data, call_data, paramlist, buf, len)
211 void *client_data;
212 void *call_data;
213 char *paramlist;
214 char **buf;
215 size_t *len;
216 #endif
217 {
218 XPA xpa = (XPA)call_data;
219 XACL cur;
220 int got = 0;
221 char tbuf[SZ_LINE];
222
223 /* zero all flags */
224 for(cur=aclhead; cur!=NULL; cur=cur->next){
225 cur->flag = 0;
226 }
227 /* look for exact matches */
228 for(cur=aclhead; cur!=NULL; cur=cur->next){
229 if(!strcmp(xpa->xclass, cur->xclass) && !strcmp(xpa->name, cur->name)){
230 snprintf(tbuf, SZ_LINE, "%s:%s %s %s\n",
231 cur->xclass, cur->name, getiphost(cur->ip), cur->acl);
232 send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
233 cur->flag = 1;
234 got++;
235 }
236 }
237 /* look for template matches that we have not printed yet */
238 for(cur=aclhead; cur!=NULL; cur=cur->next){
239 if( cur->flag == 0 ){
240 if(tmatch(xpa->xclass, cur->xclass) && tmatch(xpa->name, cur->name)){
241 snprintf(tbuf, SZ_LINE, "%s:%s %s %s\n",
242 cur->xclass, cur->name, getiphost(cur->ip), cur->acl);
243 send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
244 got++;
245 }
246 }
247 }
248 /* zero all flags */
249 for(cur=aclhead; cur!=NULL; cur=cur->next){
250 cur->flag = 0;
251 }
252 if( got == 0 ){
253 send(xpa_datafd(xpa), "\n", 1, 0);
254 }
255 return(0);
256 }
257
258 /*
259 *----------------------------------------------------------------------------
260 *
261 *
262 * Public Routines
263 *
264 *
265 *----------------------------------------------------------------------------
266 */
267
268 /*
269 *----------------------------------------------------------------------------
270 *
271 * Routine: XPAAclEdit
272 *
273 * Purpose: add or modify acl entry in the xpa acl list
274 *
275 * Returns: 0 on success, -1 on failure
276 *
277 *----------------------------------------------------------------------------
278 */
279 #ifdef ANSI_FUNC
280 int
281 XPAAclEdit (char *lbuf)
282 #else
283 int XPAAclEdit(lbuf)
284 char *lbuf;
285 #endif
286 {
287 XACL cur;
288 char xclass[SZ_LINE];
289 char name[SZ_LINE];
290 char acl[SZ_LINE];
291 unsigned int ip;
292
293 if( XPAAclParse(lbuf, xclass, name, &ip, acl, SZ_LINE) < 0 )
294 return(-1);
295 if( ip == 0 )
296 return(-1);
297 cur = XPAAclLookup(xclass, name, ip, 1);
298 if( cur == NULL )
299 return(XPAAclAdd(lbuf));
300 else{
301 if( *acl == '\0' ){
302 XPAAclDel(cur);
303 }
304 else{
305 if( cur->acl )
306 xfree(cur->acl);
307 cur->acl = xstrdup(acl);
308 }
309 return(0);
310 }
311 }
312
313
314 /*
315 *----------------------------------------------------------------------------
316 *
317 * Routine: XPAAclAdd
318 *
319 * Purpose: add one acl entry to the xpa acl list
320 *
321 * Returns: 0 on success, -1 on failure
322 *
323 *----------------------------------------------------------------------------
324 */
325 #ifdef ANSI_FUNC
326 int
327 XPAAclAdd (char *lbuf)
328 #else
329 int XPAAclAdd(lbuf)
330 char *lbuf;
331 #endif
332 {
333 XACL xnew;
334 XACL cur;
335 char xclass[SZ_LINE];
336 char name[SZ_LINE];
337 char acl[SZ_LINE];
338 unsigned int ip;
339
340 /* allocate acl struct */
341 if( (xnew = (XACL)xcalloc(1, sizeof(XACLRec))) == NULL )
342 goto error;
343
344 /* parse info from line buffer */
345 if( XPAAclParse(lbuf, xclass, name, &ip, acl, SZ_LINE) < 0 )
346 goto error;
347
348 /* fill in the blanks */
349 xnew->xclass = xstrdup(xclass);
350 xnew->name = xstrdup(name);
351 xnew->ip = ip;
352 xnew->acl = xstrdup(acl);
353
354 /* add this acl to end of list of acl's */
355 if( aclhead == NULL ){
356 aclhead = xnew;
357 }
358 else{
359 for(cur=aclhead; cur->next!=NULL; cur=cur->next)
360 ;
361 cur->next = xnew;
362 }
363 return(0);
364
365 error:
366 if( xnew )
367 xfree(xnew);
368 return(-1);
369 }
370
371 /*
372 *---------------------------------------------------------------------------
373 *
374 * Routine: XPAAclDel
375 *
376 * Purpose: free up memory in the acl record structure
377 *
378 * Results: 0 on success, -1 for failure
379 *
380 *---------------------------------------------------------------------------
381 */
382 #ifdef ANSI_FUNC
383 int
384 XPAAclDel (XACL acl)
385 #else
386 int XPAAclDel(acl)
387 XACL acl;
388 #endif
389 {
390 XACL cur;
391
392 if( acl == NULL )
393 return(-1);
394
395 /* remove from list of acl's */
396 if( aclhead ){
397 if( aclhead == acl ){
398 aclhead = aclhead->next;
399 }
400 else{
401 for(cur=aclhead; cur!=NULL; cur=cur->next){
402 if( cur->next == acl ){
403 cur->next = (cur->next)->next;
404 break;
405 }
406 }
407 }
408 }
409
410 /* free up string space */
411 if( acl->xclass ) xfree(acl->xclass);
412 if( acl->name ) xfree(acl->name);
413 if( acl->acl ) xfree(acl->acl);
414
415 /* free up record struct */
416 xfree((char *)acl);
417 return(0);
418 }
419
420 /*
421 *----------------------------------------------------------------------------
422 *
423 * Routine: XPAAclFree
424 *
425 * Purpose:
426 *
427 * Results: 1 on success, 0 for failure
428 *
429 *----------------------------------------------------------------------------
430 */
431 #ifdef ANSI_FUNC
432 void
433 XPAAclFree (void)
434 #else
435 void XPAAclFree()
436 #endif
437 {
438 XACL cur, tacl;
439
440 for(cur=aclhead; cur!=NULL; ){
441 tacl = cur->next;
442 XPAAclDel(cur);
443 cur = tacl;
444 }
445 }
446
447 /*
448 *----------------------------------------------------------------------------
449 *
450 * Routine: XPAAclNew
451 *
452 * Purpose: read or re-read the acl list
453 *
454 * Results: number of lines in list (including default)
455 *
456 *----------------------------------------------------------------------------
457 */
458 #ifdef ANSI_FUNC
459 int
460 XPAAclNew (char *aname, int flag)
461 #else
462 int XPAAclNew(aname, flag)
463 char *aname;
464 int flag;
465 #endif
466 {
467 int got=0;
468 char lbuf[SZ_LINE];
469 char hostname[SZ_LINE];
470 char *s;
471 char *obuf;
472 char *aclname=NULL;
473 char *aclpath=NULL;
474 char *defacl=NULL;
475 char *defcopy=NULL;
476 char *keywords[10];
477 char *values[10];
478 int nkeys;
479 FILE *fp=NULL;
480
481 /* if there is an old list, free it */
482 if( flag == 0 )
483 XPAAclFree();
484
485 /* get acl file name */
486 if( aname && *aname )
487 aclname = aname;
488 else if( (aclname=(char *)getenv("XPA_ACLFILE")) == NULL )
489 aclname = XPA_ACLFILE;
490
491 /* get the default acl */
492 if( (defacl=(char *)getenv("XPA_DEFACL")) == NULL )
493 defacl = XPA_DEFACL;
494
495 /* macro-expand it to deal with the host name */
496 gethost(hostname, SZ_LINE);
497 nkeys = 0;
498 keywords[0] = "host"; values[0] = hostname; nkeys++;
499
500 /* open the acl file */
501 if( (aclpath=(char *)Access(aclname, "r")) != NULL ){
502 if( (fp=fopen(aclpath, "r")) != NULL ){
503 while( fgets(lbuf, SZ_LINE, fp) ){
504 if( *lbuf == '#' ){
505 continue;
506 }
507 if( (obuf=macro(lbuf, keywords, values, nkeys, NULL, NULL)) != NULL ){
508 if( XPAAclAdd(obuf) == 0 )
509 got++;
510 xfree(obuf);
511 }
512 }
513 fclose(fp);
514 }
515 xfree(aclpath);
516 }
517
518 /* add default acl (it very likely was overridden in the file) */
519 defcopy=(char *)xstrdup(defacl);
520 for(s=(char *)strtok(defcopy,";"); s!=NULL; s=(char *)strtok(NULL,";")){
521 if( (obuf = macro(s, keywords, values, nkeys, NULL, NULL)) != NULL ){
522 if( XPAAclAdd(obuf) == 0 )
523 got++;
524 xfree(obuf);
525 }
526 }
527 if( defcopy) xfree(defcopy);
528
529 /* return the news */
530 return(got);
531 }
532
533 /*
534 *----------------------------------------------------------------------------
535 *
536 * Routine: XPAAclCheck
537 *
538 * Purpose: validate an acl for a given class, name, and host
539 *
540 * Results: 1 on success, 0 on failure
541 *
542 *----------------------------------------------------------------------------
543 */
544 #ifdef ANSI_FUNC
545 int
546 XPAAclCheck (XPA xpa, unsigned int ip, char *acl)
547 #else
548 int XPAAclCheck(xpa, ip, acl)
549 XPA xpa;
550 unsigned int ip;
551 char *acl;
552 #endif
553 {
554 char *s;
555 XACL cur;
556
557 if( !(cur = XPAAclLookup(xpa->xclass, xpa->name, ip, 0)) )
558 return(0);
559 else if( cur->acl == NULL )
560 return(0);
561 else{
562 for(s=acl; *s; s++){
563 if( !strchr(cur->acl, *s) )
564 return(0);
565 }
566 return(1);
567 }
568 }