Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / venus / cacheout.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10
11 #include <afsconfig.h>
12 #include <afs/param.h>
13
14 #include <roken.h>
15
16 #include <ctype.h>
17
18 #include <afs/auth.h>
19 #include <afs/cmd.h>
20 #include <afs/cellconfig.h>
21 #include <afs/afsint.h>
22 #include <afs/vlserver.h>
23 #include <rx/rx.h>
24 #include <rx/xdr.h>
25 #include <ubik.h>
26 #include <afs/kauth.h>
27 #include <afs/afsutil.h>
28 #include <afs/vldbint.h>
29
30 /*
31 File servers in NW byte order.
32 */
33
34 static afs_int32 server_count = 0;
35 static afs_int32 server_id[256];
36
37 static struct ubik_client *client;
38
39 static struct rx_securityClass *sc;
40 static int scindex;
41
42 /*
43 Obtain list of file servers as known to VLDB. These may
44 not actually be configured as file servers in the cell.
45 */
46
47 static afs_int32
48 ListServers(void)
49 {
50 afs_int32 code;
51 int i;
52 struct VLCallBack vlcb;
53 struct VLCallBack spare3;
54 bulkaddrs addrs, m_addrs;
55 afs_uint32 ip;
56 afs_int32 base, index;
57 afsUUID m_uuid;
58 afs_int32 m_uniq = 0;
59 afs_int32 m_nentries;
60 char hoststr[16];
61 ListAddrByAttributes m_attrs;
62
63 memset(&m_attrs, 0, sizeof(m_attrs));
64 memset(&addrs, 0, sizeof(addrs));
65 memset(&spare3, 0, sizeof(spare3));
66 code =
67 ubik_VL_GetAddrs(client, 0, 0, 0, &vlcb,
68 &server_count, &addrs);
69 if (code) {
70 printf("Fatal error: could not get list of file servers\n");
71 return 1;
72 }
73
74 for (i = 0; i < server_count; ++i) {
75 ip = addrs.bulkaddrs_val[i];
76
77 if (((ip & 0xff000000) == 0xff000000) && (ip & 0xffff)) {
78 base = (ip >> 16) & 0xff;
79 index = ip & 0xffff;
80
81 /* server is a multihomed host; query the vldb for its addresses,
82 * and just pick the first one */
83
84 if ((base >= 0) && (base <= VL_MAX_ADDREXTBLKS) && (index >= 1)
85 && (index <= VL_MHSRV_PERBLK)) {
86
87 m_attrs.Mask = VLADDR_INDEX;
88 m_attrs.index = (base * VL_MHSRV_PERBLK) + index;
89 m_nentries = 0;
90 m_addrs.bulkaddrs_val = 0;
91 m_addrs.bulkaddrs_len = 0;
92
93 code = ubik_VL_GetAddrsU(client, 0, &m_attrs, &m_uuid, &m_uniq,
94 &m_nentries, &m_addrs);
95
96 if (code || m_addrs.bulkaddrs_len == 0) {
97 printf("Error getting multihomed addresses for server "
98 "%s (index %ld)\n",
99 afs_inet_ntoa_r(m_addrs.bulkaddrs_val[0], hoststr),
100 afs_printable_int32_ld(m_attrs.index));
101 server_id[i] = 0;
102 } else {
103 server_id[i] = htonl(m_addrs.bulkaddrs_val[0]);
104 }
105 }
106 } else {
107 server_id[i] = htonl(addrs.bulkaddrs_val[i]);
108 }
109 }
110
111 return code;
112 }
113
114
115 static int
116 InvalidateCache(struct cmd_syndesc *as, void *arock)
117 {
118 afs_int32 code = 0;
119 struct cmd_item *u;
120 struct rx_connection *conn;
121 int i;
122 afs_int32 port = 7000;
123 char hoststr[16];
124 int err = 0;
125
126 afs_int32 spare1 = 0;
127 afs_int32 spare2, spare3;
128
129 afs_int32 id[256];
130 afs_int32 ip[256];
131
132 struct ViceIds vid;
133 struct IPAddrs ipa;
134
135 code = ListServers();
136 if (code)
137 return code;
138
139
140 /* make sure something there */
141
142 if (!as->parms[0].items && !as->parms[1].items) {
143 printf("Use -help flag for list of optional argmuments\n");
144 return 1;
145 }
146
147 /* get user ids */
148
149 for (i = 0, u = as->parms[0].items; i < 255 && u; ++i, u = u->next) {
150 code = util_GetInt32(u->data, &id[i]);
151 if (code) {
152 printf("Fatal error: bad conversion to long for %s\n", u->data);
153 return code;
154 }
155 }
156
157 id[i] = 0;
158 vid.ViceIds_len = i;
159 vid.ViceIds_val = id;
160
161 /* get IP addresses, convert to NW byte order */
162
163 for (i = 0, u = as->parms[1].items; i < 255 && u; ++i, u = u->next)
164 ip[i] = inet_addr(u->data);
165
166 ip[i] = 0;
167 ipa.IPAddrs_len = i;
168 ipa.IPAddrs_val = ip;
169
170 for (i = 0; i < server_count; ++i) {
171 if (!server_id[i]) {
172 err = 1;
173 continue;
174 }
175 conn = rx_NewConnection(server_id[i], htons(port), 1, sc, scindex);
176 if (!conn) {
177 printf("Informational: could not connect to \
178 file server %s\n", afs_inet_ntoa_r(server_id[i], hoststr));
179 err = 1;
180 continue;
181 }
182
183 /* invalidate the cache */
184
185 code = RXAFS_FlushCPS(conn, &vid, &ipa, spare1, &spare2, &spare3);
186
187 /*
188 * May get spurious error codes in case server is
189 * down or is reported by VLDB as a file server
190 * even though it is not configured as such in the
191 * cell.
192 */
193
194 if (code) {
195 printf("Informational: failed to invalidate \
196 file server %s cache code = %ld\n", afs_inet_ntoa_r(server_id[i], hoststr),
197 afs_printable_int32_ld(code));
198 err = 1;
199 }
200
201 rx_DestroyConnection(conn);
202 }
203 return err;
204 }
205
206 static int
207 GetServerList(struct cmd_syndesc *as, void *arock)
208 {
209 int code;
210 int i;
211
212 code = ListServers();
213 if (code)
214 return (code);
215
216 printf("There are %d file servers in the cell\n\n", server_count);
217 fflush(stdout);
218 for (i = 0; i < server_count; ++i)
219 printf("%s\n", hostutil_GetNameByINet(server_id[i]));
220 fflush(stdout);
221
222 return code;
223 }
224
225 /*
226 User enters lists of:
227
228 1. AFS user ids - say from "pts exam username".
229 2. IP addresses - say from /etc/hosts (no wildcards).
230
231 Command is executed in user's cell.
232 */
233
234 static int
235 MyBeforeProc(struct cmd_syndesc *as, void *arock)
236 {
237 char *tcell = NULL;
238 char confdir[200];
239 struct afsconf_dir *tdir;
240 struct afsconf_cell info;
241 struct rx_connection *serverconns[MAXSERVERS];
242 afs_int32 code, i;
243 rxkad_level sclevel = rxkad_auth;
244
245 sprintf(confdir, "%s", AFSDIR_CLIENT_ETC_DIRPATH);
246 if (as->parms[4].items) { /* -localauth */
247 sprintf(confdir, "%s", AFSDIR_SERVER_ETC_DIRPATH);
248 }
249
250 if (as->parms[5].items) { /* -encrypt */
251 sclevel = rxkad_crypt;
252 }
253
254 /* setup to talk to servers */
255 code = rx_Init(0);
256 if (code) {
257 printf("Warning: could not initialize network communication.\n");
258 return 1;
259 }
260
261 sc = rxnull_NewClientSecurityObject();
262 scindex = 0;
263
264 tdir = afsconf_Open(confdir);
265 if (!tdir) {
266 printf("Warning: could not get cell configuration.\n");
267 return 1;
268 }
269
270 if (as->parms[2].items) /* if -cell specified */
271 tcell = as->parms[2].items->data;
272 code = afsconf_GetCellInfo(tdir, tcell, AFSCONF_VLDBSERVICE, &info);
273 if (code || info.numServers > MAXSERVERS) {
274 printf("Warning: could not init cell info.\n");
275 return 1;
276 }
277
278 if (as->parms[4].items) { /* -localauth */
279 if (sclevel == rxkad_crypt) {
280 code = afsconf_ClientAuthSecure(tdir, &sc, &scindex);
281 } else {
282 code = afsconf_ClientAuth(tdir, &sc, &scindex);
283 }
284 if (code || scindex == 0) {
285 afsconf_Close(tdir);
286 fprintf(stderr, "Could not get security object for -localauth (code: %d)\n",
287 code);
288 return -1;
289 }
290
291 } else if (!as->parms[3].items) { /* not -noauth */
292 struct ktc_principal sname;
293 struct ktc_token ttoken;
294
295 strcpy(sname.cell, info.name);
296 sname.instance[0] = '\0';
297 strcpy(sname.name, "afs");
298
299 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
300 if (code) {
301 fprintf(stderr, "Could not get afs tokens, running unauthenticated\n");
302 } else {
303 scindex = 2;
304 sc = rxkad_NewClientSecurityObject(sclevel, &ttoken.sessionKey,
305 ttoken.kvno, ttoken.ticketLen,
306 ttoken.ticket);
307 }
308 }
309
310 for (i = 0; i < info.numServers; ++i)
311 serverconns[i] =
312 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
313 info.hostAddr[i].sin_port, USER_SERVICE_ID, sc,
314 scindex);
315 for (; i < MAXSERVERS; ++i) {
316 serverconns[i] = (struct rx_connection *)0;
317 }
318 code = ubik_ClientInit(serverconns, &client);
319 if (code) {
320 printf("Warning: could not initialize RPC interface.\n");
321 return 1;
322 }
323 return 0;
324 }
325
326
327 int
328 main(int argc, char **argv)
329 {
330 afs_int32 code = 0;
331 struct cmd_syndesc *ts;
332
333 #ifdef AFS_AIX32_ENV
334 struct sigaction nsa;
335
336 sigemptyset(&nsa.sa_mask);
337 nsa.sa_handler = SIG_DFL;
338 nsa.sa_flags = SA_FULLDUMP;
339 sigaction(SIGSEGV, &nsa, NULL);
340 #endif
341
342 /*
343 * Look in /usr/vice/etc (client side database).
344 */
345 cmd_SetBeforeProc(MyBeforeProc, NULL);
346
347 ts = cmd_CreateSyntax("initcmd" /*"invalidatecache" */ , InvalidateCache,
348 NULL, 0, "invalidate server ACL cache");
349 cmd_AddParm(ts, "-id", CMD_LIST, CMD_OPTIONAL, "user identifier");
350 cmd_AddParm(ts, "-ip", CMD_LIST, CMD_OPTIONAL, "IP address");
351 cmd_CreateAlias(ts, "ic");
352 cmd_AddParm(ts, "-cell", CMD_SINGLE, CMD_OPTIONAL, "cell name");
353 cmd_AddParm(ts, "-noauth", CMD_FLAG, CMD_OPTIONAL, "don't authenticate");
354 cmd_AddParm(ts, "-localauth", CMD_FLAG, CMD_OPTIONAL, "user server tickets");
355 cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL, "encrypt commands");
356
357 ts = cmd_CreateSyntax("listservers", GetServerList, NULL, 0,
358 "list servers in the cell");
359 cmd_CreateAlias(ts, "ls");
360
361 code = cmd_Dispatch(argc, argv);
362
363 rx_Finalize();
364
365 exit(code);
366 }