1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | /* This file is part of liblocker. It deals with zephyr subscriptions |
---|
17 | * pertaining to lockers. |
---|
18 | */ |
---|
19 | |
---|
20 | static const char rcsid[] = "$Id: zephyr.c,v 1.4 1999-05-22 17:57:55 danw Exp $"; |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <unistd.h> |
---|
24 | |
---|
25 | #include <com_err.h> |
---|
26 | #include <zephyr/zephyr.h> |
---|
27 | |
---|
28 | #include "locker.h" |
---|
29 | #include "locker_private.h" |
---|
30 | |
---|
31 | /* XXX The old version of attach set timeouts around the Zephyr |
---|
32 | * subscription code. We're not quite sure why. This code doesn't, |
---|
33 | * since it's more annoying to do that in a library context. But |
---|
34 | * if something breaks, this might be why. |
---|
35 | */ |
---|
36 | |
---|
37 | /* Magic number taken from zctl sources. */ |
---|
38 | #define ZEPHYR_MAXONEPACKET 7 |
---|
39 | |
---|
40 | int locker_do_zsubs(locker_context context, int op) |
---|
41 | { |
---|
42 | int wgport; |
---|
43 | ZSubscription_t zsubs[ZEPHYR_MAXONEPACKET]; |
---|
44 | int i, j, status, retval; |
---|
45 | uid_t uid = geteuid(); |
---|
46 | |
---|
47 | if (!context->zsubs) |
---|
48 | return LOCKER_SUCCESS; |
---|
49 | |
---|
50 | /* Initialize Zephyr. (This can fail.) */ |
---|
51 | status = ZInitialize(); |
---|
52 | if (status) |
---|
53 | { |
---|
54 | locker__error(context, "Could not initialize Zephyr library: %s.\n", |
---|
55 | error_message(status)); |
---|
56 | return LOCKER_EZEPHYR; |
---|
57 | } |
---|
58 | |
---|
59 | wgport = ZGetWGPort(); |
---|
60 | if (wgport == -1) |
---|
61 | { |
---|
62 | locker__free_zsubs(context); |
---|
63 | return LOCKER_EZEPHYR; |
---|
64 | } |
---|
65 | |
---|
66 | for (j = 0; j < ZEPHYR_MAXONEPACKET; j++) |
---|
67 | { |
---|
68 | zsubs[j].zsub_recipient = "*"; |
---|
69 | zsubs[j].zsub_class = LOCKER_ZEPHYR_CLASS; |
---|
70 | } |
---|
71 | |
---|
72 | /* seteuid since Zephyr ops may touch ticket file. */ |
---|
73 | if (uid != context->user) |
---|
74 | seteuid(context->user); |
---|
75 | |
---|
76 | retval = LOCKER_SUCCESS; |
---|
77 | for (i = 0; i < context->nzsubs; i += j) |
---|
78 | { |
---|
79 | for (j = 0; j < ZEPHYR_MAXONEPACKET && i + j < context->nzsubs; j++) |
---|
80 | zsubs[j].zsub_classinst = context->zsubs[i + j]; |
---|
81 | |
---|
82 | if (op == LOCKER_ZEPHYR_SUBSCRIBE) |
---|
83 | status = ZSubscribeTo(zsubs, j, wgport); |
---|
84 | else |
---|
85 | status = ZUnsubscribeTo(zsubs, j, wgport); |
---|
86 | if (status) |
---|
87 | { |
---|
88 | locker__error(context, "Error while %ssubscribing:\n%s.\n", |
---|
89 | op == LOCKER_ZEPHYR_SUBSCRIBE ? "" : "un", |
---|
90 | error_message(status)); |
---|
91 | retval = LOCKER_EZEPHYR; |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | if (uid != context->user) |
---|
97 | seteuid(uid); |
---|
98 | |
---|
99 | locker__free_zsubs(context); |
---|
100 | ZClosePort(); |
---|
101 | return retval; |
---|
102 | } |
---|
103 | |
---|
104 | int locker__add_zsubs(locker_context context, char **subs, int nsubs) |
---|
105 | { |
---|
106 | int newnzsubs = context->nzsubs + nsubs, i; |
---|
107 | char **newzsubs; |
---|
108 | |
---|
109 | newzsubs = realloc(context->zsubs, newnzsubs * sizeof(char *)); |
---|
110 | if (!newzsubs) |
---|
111 | { |
---|
112 | locker__error(context, "Out of memory recording Zephyr subscriptions.\n"); |
---|
113 | return LOCKER_ENOMEM; |
---|
114 | } |
---|
115 | |
---|
116 | context->zsubs = newzsubs; |
---|
117 | for (i = 0; i < nsubs; i++) |
---|
118 | { |
---|
119 | context->zsubs[context->nzsubs + i] = strdup(subs[i]); |
---|
120 | if (!context->zsubs[context->nzsubs + i]) |
---|
121 | { |
---|
122 | locker__error(context, "Out of memory recording Zephyr " |
---|
123 | "subscriptions.\n"); |
---|
124 | while (i--) |
---|
125 | free(context->zsubs[context->nzsubs + i]); |
---|
126 | return LOCKER_ENOMEM; |
---|
127 | } |
---|
128 | } |
---|
129 | context->nzsubs += nsubs; |
---|
130 | |
---|
131 | return LOCKER_SUCCESS; |
---|
132 | } |
---|
133 | |
---|
134 | void locker__free_zsubs(locker_context context) |
---|
135 | { |
---|
136 | int i; |
---|
137 | |
---|
138 | for (i = 0; i < context->nzsubs; i++) |
---|
139 | free(context->zsubs[i]); |
---|
140 | free(context->zsubs); |
---|
141 | |
---|
142 | context->nzsubs = 0; |
---|
143 | context->zsubs = NULL; |
---|
144 | } |
---|