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
|
/* This file is part of the Project Athena Zephyr Notification System.
* It contains functions for the Zephyr server class manager subsystem.
*
* Created by: John T. Kohl
*
* $Source$
* $Author$
*
* Copyright (c) 1987 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#include <zephyr/mit-copyright.h>
#ifndef lint
#ifndef SABER
static char rcsid_class_c[] = "$Header$";
#endif SABER
#endif lint
#include "zserver.h" /* includes zephyr/zephyr.h */
/*
* Class manager subsystem.
*
*
* External functions are:
*
* Code_t class_register(client, subs)
*
* Code_t class_deregister(client, subs)
*
* ZClientList_t *class_lookup(subs)
* ZClient_t *client;
* ZSubscr_t *subs;
*
* void class_free(lyst)
* ZClientList_t *lyst;
*
* ZAcl_t *class_get_acl(subs)
*
* int class_is_admin(subs)
*
* int class_is_hm(subs)
*
* int class_is_ulogin(subs)
*
* int class_is_ulocate(subs)
*
* int class_is_control(subs)
* ZSubscr_t *subs;
*
* Code_t class_restrict(class, acl)
* char *class;
* ZAcl_t *acl;
*
* Code_t class_setup_restricted(class, acl)
* char *class;
* ZAcl_t *acl;
*/
/*
* The data structure used for the class manager is an array of hash buckets
* each containing a pointer to a doubly linked circular list (in the style
* of insque/remque). Each element of this list contains a class.instance
* name (which hashes into the bucket associated with this list) and a
* doubly linked list of clients which are interested in this class.
* The data pointed to by these clients is owned by other modules. Care
* must be taken by the caller not to register a free()'d client
* structure.
*
* If any hash bucket is empty, the pointer is null.
*
* The first element in the hash bucket is a special header unused for
* storing classes, and is used for finding the end of the list.
*
* If any list of interested clients is empty, the class name is garbage
* collected, unless the class has been registered as restricted.
*/
/* Private variables */
#define EMPTY_CLASS 2000
#define HASHSIZE 511
#define HASHMUL 243
static ZClass_t *class_bucket[511]; /* the hash table of pointers */
static char class_buf[512]; /* scratch area for assembling
class.instance */
static Code_t remove_client(), insert_client();
static void free_class();
static ZClientList_t *client_alloc();
static ZClass_t *class_alloc();
static unsigned int hash(), setup_class();
/* public routines */
/* register the client as interested in class */
Code_t
class_register(client, subs)
ZClient_t *client;
ZSubscr_t *subs;
{
register ZClass_t *ptr, *ptr2;
int hashval = setup_class(subs);
if (!(ptr = class_bucket[hashval])) {
/* not registered */
if (!(ptr = class_alloc(class_buf)))
return(ENOMEM);
/* allocate the head of the bucket */
if (!(ptr2 = (ZClass_t *) xmalloc(sizeof(ZClass_t))))
return(ENOMEM);
ptr2->q_forw = ptr;
ptr2->q_back = ptr;
ptr->q_forw = ptr2;
ptr->q_back = ptr2;
class_bucket[hashval] = ptr2;
return(insert_client(ptr, client));
} else {
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw)
/* walk down the list, looking for a match */
if (!strcmp(ptr2->zct_classname, class_buf))
return(insert_client(ptr2, client));
/* fell off the end, no match */
if (!(ptr2 = class_alloc(class_buf)))
return(ENOMEM);
xinsque(ptr2, ptr); /* insert new class into hash bucket */
return(insert_client(ptr2, client));
}
}
/* dissociate client from the class, garbage collecting if appropriate */
Code_t
class_deregister(client, subs)
ZClient_t *client;
ZSubscr_t *subs;
{
register ZClass_t *ptr, *ptr2;
int retval = -1;
int hashval = setup_class(subs);
zdbug((LOG_DEBUG, "class_dereg"));
if (!(ptr = class_bucket[hashval]))
/* no such class to deregister */
return(ZSRV_BADASSOC);
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw) {
/* walk down the list, looking for a match */
if (!strcmp(ptr2->zct_classname, class_buf)) {
if ((retval = remove_client(ptr2, client)) == EMPTY_CLASS) {
zdbug((LOG_DEBUG,"empty class"));
/* Don't free up restricted classes. */
if (ptr2->zct_acl)
return(ZERR_NONE);
else {
xremque(ptr2);
free_class(ptr2);
return(ZERR_NONE);
}
}
/* if not EMPTY_CLASS, it's either ZSRV_BADASSOC
(not found) or ZERR_NONE (found and removed),
so break */
break;
}
}
/* fell off: either client not found or client found
and removed, retval contains the result */
return(retval);
}
/* return a linked list of what clients are interested in this class */
ZClientList_t *
class_lookup(subs)
ZSubscr_t *subs;
{
register ZClass_t *ptr, *ptr2;
register int count = 0, wc_count = 0, idx = 1;
register ZClientList_t *list_return, *list_copy;
ZClientList_t *list = NULLZCLT;
ZClientList_t *wc_list = NULLZCLT;
ZSubscr_t wc_sub;
int hashval = setup_class(subs);
if (ptr = class_bucket[hashval])
/* go search the list for the class */
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw) {
/* walk down the list, looking for a match */
if (!strcmp(ptr2->zct_classname, class_buf)) {
list = ptr2->zct_clientlist;
break;
}
}
/* list is the list of direct matches; now check for wildcards */
wc_sub = *subs;
wc_sub.zst_classinst = WILDCARD_INSTANCE;
hashval = setup_class(&wc_sub);
if (ptr = class_bucket[hashval])
/* go search the list for the class */
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw) {
/* walk down the list, looking for a match */
if (!strcmp(ptr2->zct_classname, class_buf)) {
wc_list = ptr2->zct_clientlist;
break;
}
}
/* merge the lists for returning */
if (list)
for (list_return = list->q_forw;
list_return != list;
list_return = list_return->q_forw)
count++;
if (wc_list)
for (list_return = wc_list->q_forw;
list_return != wc_list;
list_return = list_return->q_forw)
wc_count++;
if (!(wc_count + count))
return(NULLZCLT);
list_return = (ZClientList_t *) xmalloc((count + wc_count + 1) * sizeof(ZClientList_t));
if (!list_return) {
syslog(LOG_ERR, "class_lookup no mem");
return(NULLZCLT);
}
list_return[0].q_forw = list_return[0].q_back = &list_return[0];
if (list)
for (list_copy = list->q_forw;
list_copy != list;
list_copy = list_copy->q_forw) {
list_return[idx].zclt_client = list_copy->zclt_client;
xinsque(&list_return[idx], &list_return[0]);
idx++;
}
if (wc_list)
for (list_copy = wc_list->q_forw;
list_copy != wc_list;
list_copy = list_copy->q_forw) {
list_return[idx].zclt_client = list_copy->zclt_client;
xinsque(&list_return[idx], &list_return[0]);
idx++;
}
return(list_return);
}
/* free up the storage used by a returned list */
void
class_free(lyst)
ZClientList_t *lyst;
{
xfree(lyst);
return;
}
/*
* These routines return 0 or one depending on whether the indicated
* notice is of the type named by the routine name.
*/
int
class_is_control(notice)
ZNotice_t *notice;
{
return(!strcmp(notice->z_class, ZEPHYR_CTL_CLASS));
}
int
class_is_admin(notice)
ZNotice_t *notice;
{
return(!strcmp(notice->z_class, ZEPHYR_ADMIN_CLASS));
}
int
class_is_hm(notice)
ZNotice_t *notice;
{
return(!strcmp(notice->z_class, HM_CTL_CLASS));
}
int
class_is_ulogin(notice)
ZNotice_t *notice;
{
return(!strcmp(notice->z_class, LOGIN_CLASS));
}
int
class_is_ulocate(notice)
ZNotice_t *notice;
{
return(!strcmp(notice->z_class, LOCATE_CLASS));
}
/*
* return the acl structure associated with class, or NULLZACLT if there is
* no such acl struct
*/
ZAcl_t *
class_get_acl(class)
char *class;
{
register ZClass_t *ptr, *ptr2;
int hashval = hash(class);
if (!(ptr = class_bucket[hashval]))
return(NULLZACLT);
/* walk down the list, looking for a match */
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw)
if (!strcmp(ptr2->zct_classname, class))
return(ptr2->zct_acl);
/* fell off the end, no match ==> not restricted */
return(NULLZACLT);
}
#ifdef notdef
/* currently not used */
/*
* restrict class by associating it with the acl structure acl.
* return ZERR_NONE if no error, or ZSRV_NOCLASS if there is no such
* class, or ZSRV_CLASSRESTRICTED if it is already restricted.
*/
Code_t
class_restrict(class, acl)
char *class;
ZAcl_t *acl;
{
register ZClass_t *ptr, *ptr2;
int hashval = hash(class);
if (!(ptr = class_bucket[hashval]))
return(ZSRV_NOCLASS);
for (ptr2 = ptr->q_forw; ptr2 != ptr; ptr2 = ptr2->q_forw)
/* walk down the list, looking for a match */
if (!strcmp(ptr2->zct_classname, class)) {
if (ptr2->zct_acl)
return(ZSRV_CLASSRESTRICTED);
ptr2->zct_acl = acl;
return(ZERR_NONE);
}
/* fell off the end, no match */
return(ZSRV_NOCLASS);
}
#endif notdef
/*
* restrict class by registering it and associating it with the acl
* structure acl. return ZERR_NONE if no error, or ZSRV_CLASSXISTS
* if the class is already registered, or ENOMEM in case of malloc failure.
*/
Code_t
class_setup_restricted(class, acl)
char *class;
ZAcl_t *acl;
{
register ZClass_t *ptr, *ptr2;
int hashval = hash(class);
if (!(ptr = class_bucket[hashval])) {
/* not registered */
if (!(ptr = class_alloc(class)))
return(ENOMEM);
ptr->zct_acl = acl;
/* allocate the head of the bucket */
if (!(ptr2 = (ZClass_t *) xmalloc(sizeof(ZClass_t))))
return(ENOMEM);
ptr2->q_forw = ptr;
ptr2->q_back = ptr;
ptr->q_forw = ptr2;
ptr->q_back = ptr2;
class_bucket[hashval] = ptr2;
return(ZERR_NONE);
} else
return(ZSRV_CLASSXISTS);
}
/* private routines */
/* the hash function */
static unsigned int
hash(string)
char *string;
{
register unsigned int hval = 0;
register unsigned char *cp = (unsigned char *) string;
while (*cp)
hval = (hval + (*cp++) * HASHMUL) % HASHSIZE;
return(hval);
}
/* set up the class.instance in the class_buf, and return its hash val */
static unsigned int
setup_class(subs)
ZSubscr_t *subs;
{
(void) strcpy(class_buf, subs->zst_class);
(void) strcat(class_buf, ".");
(void) strcat(class_buf, subs->zst_classinst);
return(hash(class_buf));
}
/* allocate space for a class structure */
static ZClass_t *
class_alloc(class)
char *class;
{
register ZClass_t *ptr;
ZClientList_t *clist;
if (!(ptr = (ZClass_t *) xmalloc(sizeof(ZClass_t))))
return(NULLZCT);
ptr->q_forw = ptr->q_back = ptr;
ptr->zct_classname = strsave(class);
if (!(clist = (ZClientList_t *) xmalloc(sizeof(ZClientList_t)))) {
xfree(ptr);
return(NULLZCT);
}
clist->q_forw = clist->q_back = clist;
ptr->zct_clientlist = clist;
ptr->zct_acl = NULLZACLT;
return(ptr);
}
/* free up the space used by this class structure */
static void
free_class(ptr)
ZClass_t *ptr;
{
xfree(ptr->zct_classname);
xfree(ptr->zct_clientlist);
xfree(ptr);
}
/* allocate space for a client entry */
static ZClientList_t *
client_alloc(client)
ZClient_t *client;
{
register ZClientList_t *ptr;
if (!(ptr = (ZClientList_t *) xmalloc(sizeof(ZClientList_t))))
return(NULLZCLT);
ptr->zclt_client = client;
return(ptr);
}
/* insert a client into the list associated with the class *ptr */
static Code_t
insert_client(ptr, client)
ZClass_t *ptr;
ZClient_t *client;
{
register ZClientList_t *listp, *clist;
for (clist = ptr->zct_clientlist->q_forw;
clist != ptr->zct_clientlist;
clist = clist->q_forw)
/* don't duplicate */
if (clist->zclt_client == client)
return(ZERR_NONE);
if (!(listp = client_alloc(client)))
return(ENOMEM);
xinsque(listp, ptr->zct_clientlist);
return(ZERR_NONE);
}
/*
* remove the client from the list associated with class *ptr, garbage
* collecting if appropriate
*/
static Code_t remove_client(ptr, client)
ZClass_t *ptr;
ZClient_t *client;
{
register ZClientList_t *listp = ptr->zct_clientlist;
register ZClientList_t *listp2;
if (!listp)
return(ZSRV_BADASSOC);
for (listp2 = listp->q_forw;
listp2 != listp;
listp2 = listp2->q_forw)
/* walk down list, looking for him */
if (listp2->zclt_client == client) {
xremque(listp2);
xfree(listp2);
if (listp->q_forw == listp)
return(EMPTY_CLASS);
else
return(ZERR_NONE);
}
return(ZSRV_BADASSOC);
}
|