summaryrefslogtreecommitdiffstats
path: root/xpa/acl.c
blob: 24b44893cb2953dc97e8c119393ff5eefa592052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

/*
 *
 * acl.c -- xpa access control list management
 *
 */

#include <xpap.h>

/*
 *----------------------------------------------------------------------------
 *
 *
 *			Private Routines
 *
 *
 *----------------------------------------------------------------------------
 */

/* this is the head of the global list -- too lazy to do anything more */
static XACL aclhead=NULL;

#ifdef ANSI_FUNC
static XACL 
XPAAclLookup (char *xclass, char *name, unsigned int ip, int exact)
#else
static XACL XPAAclLookup(xclass, name, ip, exact)
     char *xclass;
     char *name;
     unsigned int ip;
     int exact;
#endif
{
  XACL cur;
  /*  look for exact match */
  for(cur=aclhead; cur!=NULL; cur=cur->next){
    if( !strcmp(xclass, cur->xclass) 		&&
	!strcmp(name, cur->name)   		&&
	((cur->ip == 0) || (cur->ip == ip)) 	){
      return(cur);
    }
  }
  /* otherwise look for a template match (unless exact was specified) */
  if( !exact ){
    for(cur=aclhead; cur!=NULL; cur=cur->next){
      if( tmatch(xclass, cur->xclass)		&&
	  tmatch(name, cur->name) 		&&
	  ((cur->ip == 0) || (cur->ip == ip)) 	){
	return(cur);
      }
    }
  }
  return(NULL);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclParse
 *
 * Purpose:	parse acl list into components
 *
 * Returns:	0 on success, -1 on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
XPAAclParse (char *lbuf, char *xclass, char *name, unsigned int *ip,
	     char *acl, int len)
#else
static int XPAAclParse(lbuf, xclass, name, ip, acl, len)
     char *lbuf;
     char *xclass;
     char *name;
     unsigned int *ip;
     char *acl;
     int len;
#endif
{
  char tbuf[SZ_LINE];
  int lp=0;

  /* class:name is required */
  if( word(lbuf, tbuf, &lp) ){
    XPAParseName(tbuf, xclass, name, len);
  }
  else
    return(-1);

  /* host is required but can be "*" for "all hosts" */
  if( word(lbuf, tbuf, &lp) ){
    if( !strcmp(tbuf, "*") )
      *ip = 0;
    else
      *ip = gethostip(tbuf);
  }
  else
    return(-1);

  /* acl is required */
  if( word(lbuf, tbuf, &lp) ){
    if( !strcmp(tbuf, "+") )
      strcpy(acl, XPA_ACLS);
    else if( !strcmp(tbuf, "-") )
      *acl = '\0';
    else
      strcpy(acl, tbuf);
    return(0);
  }
  else
    return(-1);
}

/*
 *----------------------------------------------------------------------------
 *
 *
 *		Semi-Public Routines (used by command.c)
 *
 *
 *----------------------------------------------------------------------------
 */

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAReceiveAcl
 *
 * Purpose:	add or modify the acl for this access point
 *
 * Returns:	0 for success, -1 for failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAReceiveAcl (void *client_data, void *call_data, char *paramlist,
	       char *buf, size_t len)
#else
int XPAReceiveAcl(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char *buf;
     size_t len;
#endif
{
  XPA xpa = (XPA)call_data;
  XPAComm comm;
  int i;
  int got;
  char *s=NULL;
  char lbuf[SZ_LINE];
  char tbuf[SZ_LINE];

  if( paramlist && *paramlist ){
    s = paramlist;
    while( isspace((int)*s) )
      s++;
    snprintf(tbuf, SZ_LINE, "%s:%s %s\n", xpa->xclass, xpa->name, s);
    if( XPAAclEdit(tbuf) < 0 ){
      snprintf(lbuf, SZ_LINE, "invalid acl: %s\n", tbuf);
      XPAError(xpa, lbuf);
      return(-1);
    }
  }
  else{
    while((XPAGets(xpa, xpa_datafd(xpa), lbuf, SZ_LINE, XPAShortTimeout())>0)&&
	  *lbuf ){
      snprintf(tbuf, SZ_LINE, "%s:%s %s\n", xpa->xclass, xpa->name, lbuf);
      got = XPAAclEdit(tbuf);
      if( got < 0 ){
	snprintf(lbuf, SZ_LINE, "invalid acl: %s\n", tbuf);
	XPAError(xpa, lbuf);
	return(-1);
      }
    }
  }

  /* reset all acl flags for this xpa so acl is recalculated */
  for(comm=xpa->commhead; comm!=NULL; comm=comm->next){
    for(i=0; i<4; i++){
      comm->acl[i] = -1;
    }
  }

  return(0);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPASendAcl
 *
 * Purpose:	return the acl for this access point
 *
 * Returns:	0 for success, -1 for failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPASendAcl (void *client_data, void *call_data, char *paramlist,
	    char **buf, size_t *len)
#else
int XPASendAcl(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char **buf;
     size_t *len;
#endif
{
  XPA xpa = (XPA)call_data;
  XACL cur;
  int got = 0;
  char tbuf[SZ_LINE];

  /* zero all flags */
  for(cur=aclhead; cur!=NULL; cur=cur->next){
    cur->flag = 0;
  }
  /* look for exact matches */
  for(cur=aclhead; cur!=NULL; cur=cur->next){
    if(!strcmp(xpa->xclass, cur->xclass) && !strcmp(xpa->name, cur->name)){
      snprintf(tbuf, SZ_LINE, "%s:%s %s %s\n",
	       cur->xclass, cur->name, getiphost(cur->ip), cur->acl);
      send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
      cur->flag = 1;
      got++;
    }
  }
  /* look for template matches that we have not printed yet */
  for(cur=aclhead; cur!=NULL; cur=cur->next){
    if( cur->flag == 0 ){
      if(tmatch(xpa->xclass, cur->xclass) && tmatch(xpa->name, cur->name)){
	snprintf(tbuf, SZ_LINE, "%s:%s %s %s\n",
		 cur->xclass, cur->name, getiphost(cur->ip), cur->acl);
	send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
	got++;
      }
    }
  }
  /* zero all flags */
  for(cur=aclhead; cur!=NULL; cur=cur->next){
    cur->flag = 0;
  }
  if( got == 0 ){
    send(xpa_datafd(xpa), "\n", 1, 0);
  }
  return(0);
}

/*
 *----------------------------------------------------------------------------
 *
 *
 *			Public Routines
 *
 *
 *----------------------------------------------------------------------------
 */

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclEdit
 *
 * Purpose:	add or modify acl entry in the xpa acl list
 *
 * Returns: 	0 on success, -1 on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAAclEdit (char *lbuf)
#else
int XPAAclEdit(lbuf)
     char *lbuf;
#endif
{
  XACL cur;
  char xclass[SZ_LINE];
  char name[SZ_LINE];
  char acl[SZ_LINE];
  unsigned int ip;

  if( XPAAclParse(lbuf, xclass, name, &ip, acl, SZ_LINE) < 0 )
    return(-1);
  if( ip == 0 )
    return(-1);
  cur = XPAAclLookup(xclass, name, ip, 1);
  if( cur == NULL )
    return(XPAAclAdd(lbuf));
  else{
    if( *acl == '\0' ){
      XPAAclDel(cur);
    }
    else{
      if( cur->acl )
	xfree(cur->acl);
      cur->acl = xstrdup(acl);
    }
    return(0);
  }
}


/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclAdd
 *
 * Purpose:	add one acl entry to the xpa acl list
 *
 * Returns: 	0 on success, -1 on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAAclAdd (char *lbuf)
#else
int XPAAclAdd(lbuf)
     char *lbuf;
#endif
{
  XACL xnew;
  XACL cur;
  char xclass[SZ_LINE];
  char name[SZ_LINE];
  char acl[SZ_LINE];
  unsigned int ip;

  /* allocate acl struct */
  if( (xnew = (XACL)xcalloc(1, sizeof(XACLRec))) == NULL )
    goto error;

  /* parse info from line buffer */
  if( XPAAclParse(lbuf, xclass, name, &ip, acl, SZ_LINE) < 0 )
    goto error;

  /* fill in the blanks */
  xnew->xclass = xstrdup(xclass);
  xnew->name = xstrdup(name);
  xnew->ip = ip;
  xnew->acl = xstrdup(acl);

  /* add this acl to end of list of acl's */
  if( aclhead == NULL ){
    aclhead = xnew;
  }
  else{
    for(cur=aclhead; cur->next!=NULL; cur=cur->next)
      ;
    cur->next = xnew;
  }
  return(0);

error:
  if( xnew )
    xfree(xnew);
  return(-1);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	XPAAclDel
 *
 * Purpose: 	free up memory in the acl record structure
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAAclDel (XACL acl)
#else
int XPAAclDel(acl)
     XACL acl;
#endif
{
  XACL cur;

  if( acl == NULL )
    return(-1);

  /* remove from list of acl's */
  if( aclhead ){
    if( aclhead == acl ){
      aclhead = aclhead->next;
    }
    else{
      for(cur=aclhead; cur!=NULL; cur=cur->next){
	if( cur->next == acl ){
	  cur->next = (cur->next)->next;
	  break;
	}
      }
    }
  }

  /* free up string space */
  if( acl->xclass ) xfree(acl->xclass);
  if( acl->name )  xfree(acl->name);
  if( acl->acl )   xfree(acl->acl);

  /* free up record struct */
  xfree((char *)acl);
  return(0);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclFree
 *
 * Purpose:	
 *
 * Results:	1 on success, 0 for failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
void 
XPAAclFree (void)
#else
void XPAAclFree()
#endif
{
  XACL cur, tacl;

  for(cur=aclhead; cur!=NULL; ){
    tacl = cur->next;
    XPAAclDel(cur);
    cur = tacl;
  }
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclNew
 *
 * Purpose:	read or re-read the acl list
 *
 * Results:	number of lines in list (including default)
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAAclNew (char *aname, int flag)
#else
int XPAAclNew(aname, flag)
     char *aname;
     int flag;
#endif
{
  int got=0;
  char lbuf[SZ_LINE];
  char hostname[SZ_LINE];
  char *s;
  char *obuf;
  char *aclname=NULL;
  char *aclpath=NULL;
  char *defacl=NULL;
  char *defcopy=NULL;
  char *keywords[10];
  char *values[10];
  int nkeys;
  FILE *fp=NULL;

  /* if there is an old list, free it */
  if( flag == 0 )
    XPAAclFree();

  /* get acl file name */
  if( aname && *aname )
    aclname = aname;
  else if( (aclname=(char *)getenv("XPA_ACLFILE")) == NULL )
    aclname = XPA_ACLFILE;

  /* get the default acl */
  if( (defacl=(char *)getenv("XPA_DEFACL")) == NULL )
    defacl = XPA_DEFACL;

  /* macro-expand it to deal with the host name */
  gethost(hostname, SZ_LINE);
  nkeys = 0;
  keywords[0] = "host";  values[0] = hostname; nkeys++;

  /* open the acl file */
  if( (aclpath=(char *)Access(aclname, "r")) != NULL ){
    if( (fp=fopen(aclpath, "r")) != NULL ){
      while( fgets(lbuf, SZ_LINE, fp) ){
	if( *lbuf == '#' ){
	  continue;
	}
	if( (obuf=macro(lbuf, keywords, values, nkeys, NULL, NULL)) != NULL ){
	  if( XPAAclAdd(obuf) == 0 )
	    got++;
	  xfree(obuf);
	}
      }
      fclose(fp);
    }
    xfree(aclpath);
  }

  /* add default acl (it very likely was overridden in the file) */
  defcopy=(char *)xstrdup(defacl);
  for(s=(char *)strtok(defcopy,";"); s!=NULL; s=(char *)strtok(NULL,";")){
    if( (obuf = macro(s, keywords, values, nkeys, NULL, NULL)) != NULL ){
      if( XPAAclAdd(obuf) == 0 )
	got++;
      xfree(obuf);
    }
  }
  if( defcopy) xfree(defcopy);

  /* return the news */
  return(got);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAAclCheck
 *
 * Purpose:	validate an acl for a given class, name, and host
 *
 * Results:	1 on success, 0 on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
int 
XPAAclCheck (XPA xpa, unsigned int ip, char *acl)
#else
int XPAAclCheck(xpa, ip, acl)
     XPA xpa;
     unsigned int ip;
     char *acl;
#endif
{
  char *s;
  XACL cur;

  if( !(cur = XPAAclLookup(xpa->xclass, xpa->name, ip, 0)) )
    return(0);
  else if( cur->acl == NULL )
    return(0);
  else{
    for(s=acl; *s; s++){
      if( !strchr(cur->acl, *s) )
	return(0);
    }
    return(1);
  }
}