]>
Commit | Line | Data |
---|---|---|
80e8aa14 | 1 | /* |
2 | * Command line oriented Moira host tool. | |
3 | * | |
4 | * kolya@MIT.EDU, January 2000 | |
5 | * | |
6 | * Somewhat based on blanche | |
7 | * | |
8 | * Copyright (C) 2000 by the Massachusetts Institute of Technology. | |
9 | * For copying and distribution information, please see the file | |
10 | * <mit-copyright.h>. | |
11 | */ | |
12 | ||
13 | #include <mit-copyright.h> | |
14 | #include <moira.h> | |
15 | #include <moira_site.h> | |
16 | #include <mrclient.h> | |
17 | ||
18 | #include <ctype.h> | |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | RCSID("$Header$"); | |
25 | ||
26 | struct owner_type { | |
27 | int type; | |
28 | char *name; | |
29 | }; | |
30 | ||
31 | struct mqelem { | |
32 | struct mqelem *q_forw; | |
33 | struct mqelem *q_back; | |
34 | void *q_data; | |
35 | }; | |
36 | ||
37 | struct string_list { | |
38 | char *string; | |
39 | struct string_list *next; | |
40 | }; | |
41 | ||
80e8aa14 | 42 | #define M_ANY 0 |
43 | #define M_USER 1 | |
44 | #define M_LIST 2 | |
a547d259 | 45 | #define M_KERBEROS 3 |
46 | #define M_NONE 4 | |
80e8aa14 | 47 | |
48 | /* argument parsing macro */ | |
49 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) | |
50 | ||
51 | /* flags from command line */ | |
52 | int info_flag, update_flag, create_flag, delete_flag, list_map_flag; | |
7e689fac | 53 | int update_alias_flag, update_map_flag, verbose, noauth; |
80e8aa14 | 54 | |
55 | struct string_list *alias_add_queue, *alias_remove_queue; | |
56 | struct string_list *map_add_queue, *map_remove_queue; | |
57 | ||
58 | char *hostname, *whoami; | |
59 | ||
60 | char *newname, *address, *network, *h_status, *vendor, *model; | |
080e5e2c | 61 | char *os, *location, *contact, *billing_contact, *adm_cmt, *op_cmt; |
80e8aa14 | 62 | |
63 | struct owner_type *owner; | |
64 | ||
65 | void usage(char **argv); | |
66 | int store_host_info(int argc, char **argv, void *hint); | |
67 | void show_host_info(char **argv); | |
68 | int show_machine_in_cluster(int argc, char **argv, void *hint); | |
69 | struct owner_type *parse_member(char *s); | |
70 | struct string_list *add_to_string_list(struct string_list *old_list, char *s); | |
71 | int wrap_mr_query(char *handle, int argc, char **argv, | |
72 | int (*callback)(int, char **, void *), void *callarg); | |
73 | void print_query(char *query_name, int argc, char **argv); | |
74 | ||
75 | int main(int argc, char **argv) | |
76 | { | |
77 | int status, success; | |
78 | char **arg = argv; | |
533bacb3 | 79 | char *server = NULL; |
80e8aa14 | 80 | |
81 | /* clear all flags & lists */ | |
82 | info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0; | |
7e689fac | 83 | update_alias_flag = verbose = noauth = 0; |
80e8aa14 | 84 | newname = address = network = h_status = vendor = model = NULL; |
080e5e2c | 85 | os = location = contact = billing_contact = adm_cmt = op_cmt = NULL; |
80e8aa14 | 86 | owner = NULL; |
87 | alias_add_queue = alias_remove_queue = NULL; | |
88 | map_add_queue = map_remove_queue = NULL; | |
89 | whoami = argv[0]; | |
90 | ||
91 | success = 1; | |
92 | ||
93 | /* parse args, building addlist, dellist, & synclist */ | |
94 | while (++arg - argv < argc) | |
95 | { | |
96 | if (**arg == '-') | |
97 | { | |
98 | if (argis("i", "info")) | |
99 | info_flag++; | |
100 | else if (argis("C", "create")) | |
101 | create_flag++; | |
102 | else if (argis("D", "delete")) | |
103 | delete_flag++; | |
104 | else if (argis("R", "rename")) { | |
105 | if (arg - argv < argc - 1) { | |
106 | arg++; | |
107 | update_flag++; | |
108 | newname = *arg; | |
109 | } else | |
110 | usage(argv); | |
111 | } | |
112 | else if (argis("A", "address")) { | |
113 | if (arg - argv < argc - 1) { | |
114 | arg++; | |
115 | update_flag++; | |
116 | address = *arg; | |
117 | } else | |
118 | usage(argv); | |
119 | } | |
120 | else if (argis("O", "owner")) { | |
121 | if (arg - argv < argc - 1) { | |
122 | arg++; | |
123 | update_flag++; | |
124 | owner = parse_member(*arg); | |
125 | } else | |
126 | usage(argv); | |
127 | } | |
128 | else if (argis("N", "network")) { | |
129 | if (arg - argv < argc - 1) { | |
130 | arg++; | |
131 | update_flag++; | |
132 | network = *arg; | |
133 | } else | |
134 | usage(argv); | |
135 | } | |
136 | else if (argis("S", "status")) { | |
137 | if (arg - argv < argc - 1) { | |
138 | int i; | |
533bacb3 | 139 | int len; |
80e8aa14 | 140 | |
141 | arg++; | |
142 | update_flag++; | |
143 | h_status = *arg; | |
144 | ||
533bacb3 | 145 | len = strlen(h_status); |
146 | for(i = 0; i < len; i++) { | |
80e8aa14 | 147 | if(!isdigit(h_status[i])) { |
148 | printf("Error: status code %s is not numeric.\n", h_status); | |
149 | exit(1); | |
150 | } | |
151 | } | |
152 | } else | |
153 | usage(argv); | |
154 | } | |
155 | else if (argis("V", "vendor")) { | |
156 | if (arg - argv < argc - 1) { | |
157 | arg++; | |
158 | update_flag++; | |
159 | vendor = *arg; | |
160 | } else | |
161 | usage(argv); | |
162 | } | |
163 | else if (argis("M", "model")) { | |
164 | if (arg - argv < argc - 1) { | |
165 | arg++; | |
166 | update_flag++; | |
167 | model = *arg; | |
168 | } else | |
169 | usage(argv); | |
170 | } | |
171 | else if (argis("o", "os")) { | |
172 | if (arg - argv < argc - 1) { | |
173 | arg++; | |
174 | update_flag++; | |
175 | os = *arg; | |
176 | } else | |
177 | usage(argv); | |
178 | } | |
179 | else if (argis("L", "location")) { | |
180 | if (arg - argv < argc - 1) { | |
181 | arg++; | |
182 | update_flag++; | |
183 | location = *arg; | |
184 | } else | |
185 | usage(argv); | |
186 | } | |
187 | else if (argis("c", "contact")) { | |
188 | if (arg - argv < argc - 1) { | |
189 | arg++; | |
190 | update_flag++; | |
191 | contact = *arg; | |
192 | } else | |
193 | usage(argv); | |
194 | } | |
080e5e2c | 195 | else if (argis("bc", "billingcontact")) { |
196 | if (arg - argv < argc - 1) { | |
197 | arg++; | |
198 | update_flag++; | |
199 | billing_contact = *arg; | |
200 | } else | |
201 | usage(argv); | |
202 | } | |
80e8aa14 | 203 | else if (argis("ac", "admcmt")) { |
204 | if (arg - argv < argc - 1) { | |
205 | arg++; | |
206 | update_flag++; | |
207 | adm_cmt = *arg; | |
208 | } else | |
209 | usage(argv); | |
210 | } | |
211 | else if (argis("oc", "opcmt")) { | |
212 | if (arg - argv < argc - 1) { | |
213 | arg++; | |
214 | update_flag++; | |
215 | op_cmt = *arg; | |
216 | } else | |
217 | usage(argv); | |
218 | } | |
219 | else if (argis("a", "aliasadd")) { | |
220 | if (arg - argv < argc - 1) { | |
221 | arg++; | |
222 | alias_add_queue=add_to_string_list(alias_add_queue, *arg); | |
223 | } else | |
224 | usage(argv); | |
7e689fac | 225 | update_alias_flag++; |
80e8aa14 | 226 | } |
227 | else if (argis("d", "aliasdelete")) { | |
228 | if (arg - argv < argc - 1) { | |
229 | arg++; | |
230 | alias_remove_queue=add_to_string_list(alias_remove_queue, *arg); | |
231 | } else | |
232 | usage(argv); | |
7e689fac | 233 | update_alias_flag++; |
80e8aa14 | 234 | } |
235 | else if (argis("am", "addmap")) { | |
236 | if (arg - argv < argc - 1) { | |
237 | arg++; | |
238 | map_add_queue=add_to_string_list(map_add_queue, *arg); | |
239 | } else | |
240 | usage(argv); | |
241 | update_map_flag++; | |
242 | } | |
243 | else if (argis("dm", "deletemap")) { | |
244 | if (arg - argv < argc - 1) { | |
245 | arg++; | |
246 | map_remove_queue=add_to_string_list(map_remove_queue, *arg); | |
247 | } else | |
248 | usage(argv); | |
249 | update_map_flag++; | |
250 | } | |
251 | else if (argis("lm", "listmap")) | |
252 | list_map_flag++; | |
253 | else if (argis("n", "noauth")) | |
254 | noauth++; | |
255 | else if (argis("v", "verbose")) | |
256 | verbose++; | |
257 | else if (argis("db", "database")) | |
258 | { | |
259 | if (arg - argv < argc - 1) | |
260 | { | |
261 | ++arg; | |
262 | server = *arg; | |
263 | } | |
264 | else | |
265 | usage(argv); | |
266 | } | |
267 | else | |
268 | usage(argv); | |
269 | } | |
270 | else if (hostname == NULL) | |
271 | hostname = *arg; | |
272 | else | |
273 | usage(argv); | |
274 | } | |
275 | if (hostname == NULL) | |
276 | usage(argv); | |
277 | ||
278 | /* default to info_flag if nothing else was specified */ | |
279 | if(!(info_flag || update_flag || create_flag || \ | |
7e689fac | 280 | delete_flag || list_map_flag || update_map_flag || \ |
281 | update_alias_flag)) { | |
80e8aa14 | 282 | info_flag++; |
283 | } | |
284 | ||
285 | /* fire up Moira */ | |
080e5e2c | 286 | status = mrcl_connect(server, "stella", 6, !noauth); |
80e8aa14 | 287 | if (status == MRCL_AUTH_ERROR) |
288 | { | |
289 | com_err(whoami, 0, "Try the -noauth flag if you don't " | |
290 | "need authentication."); | |
291 | } | |
292 | if (status) | |
293 | exit(2); | |
294 | ||
295 | /* create if needed */ | |
296 | if (create_flag) | |
297 | { | |
298 | char *argv[30]; | |
299 | int cnt; | |
300 | ||
080e5e2c | 301 | for (cnt = 0; cnt < 16; cnt++) { |
80e8aa14 | 302 | argv[cnt] = ""; |
303 | } | |
304 | ||
305 | argv[0] = canonicalize_hostname(strdup(hostname)); | |
306 | ||
307 | if (vendor) | |
308 | argv[1] = vendor; | |
309 | if (model) | |
310 | argv[2] = model; | |
311 | if (os) | |
312 | argv[3] = os; | |
313 | if (location) | |
314 | argv[4] = location; | |
315 | if (contact) | |
316 | argv[5] = contact; | |
080e5e2c | 317 | if (billing_contact) |
318 | argv[6] = billing_contact; | |
80e8aa14 | 319 | /* The use field always gets set to "0" */ |
080e5e2c | 320 | argv[7] = "0"; |
80e8aa14 | 321 | if (h_status) |
080e5e2c | 322 | argv[8] = h_status; |
80e8aa14 | 323 | else |
080e5e2c | 324 | argv[8] = "1"; |
80e8aa14 | 325 | if (network) |
080e5e2c | 326 | argv[9] = network; |
80e8aa14 | 327 | if (address) |
080e5e2c | 328 | argv[10] = address; |
d55f933a | 329 | else |
080e5e2c | 330 | argv[10] = "unique"; |
80e8aa14 | 331 | if (adm_cmt) |
080e5e2c | 332 | argv[13] = adm_cmt; |
80e8aa14 | 333 | if (op_cmt) |
080e5e2c | 334 | argv[14] = op_cmt; |
80e8aa14 | 335 | |
336 | if (owner) | |
337 | { | |
080e5e2c | 338 | argv[12] = owner->name; |
80e8aa14 | 339 | switch (owner->type) |
340 | { | |
341 | case M_ANY: | |
342 | case M_USER: | |
080e5e2c | 343 | argv[11] = "USER"; |
344 | status = wrap_mr_query("add_host", 15, argv, NULL, NULL); | |
80e8aa14 | 345 | if (owner->type != M_ANY || status != MR_USER) |
346 | break; | |
347 | ||
348 | case M_LIST: | |
080e5e2c | 349 | argv[11] = "LIST"; |
350 | status = wrap_mr_query("add_host", 15, argv, NULL, NULL); | |
80e8aa14 | 351 | break; |
352 | ||
353 | case M_KERBEROS: | |
080e5e2c | 354 | argv[11] = "KERBEROS"; |
355 | status = wrap_mr_query("add_host", 15, argv, NULL, NULL); | |
80e8aa14 | 356 | break; |
357 | ||
358 | case M_NONE: | |
080e5e2c | 359 | argv[11] = "NONE"; |
360 | status = wrap_mr_query("add_host", 15, argv, NULL, NULL); | |
80e8aa14 | 361 | break; |
362 | } | |
363 | } | |
364 | else | |
365 | { | |
80e8aa14 | 366 | argv[11] = "NONE"; |
080e5e2c | 367 | argv[12] = "NONE"; |
80e8aa14 | 368 | |
080e5e2c | 369 | status = wrap_mr_query("add_host", 15, argv, NULL, NULL); |
80e8aa14 | 370 | } |
371 | ||
372 | if (status) | |
373 | { | |
374 | com_err(whoami, status, "while creating host."); | |
375 | exit(1); | |
376 | } | |
377 | ||
378 | } | |
379 | else if (update_flag) | |
380 | { | |
381 | char *old_argv[30]; | |
382 | char *argv[16]; | |
383 | char *args[5]; | |
80e8aa14 | 384 | |
385 | args[0] = canonicalize_hostname(strdup(hostname)); | |
386 | args[1] = args[2] = args[3] = "*"; | |
387 | ||
388 | status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv); | |
389 | if (status) | |
390 | { | |
391 | com_err(whoami, status, "while getting list information"); | |
392 | exit(1); | |
393 | } | |
394 | ||
395 | argv[1] = old_argv[0]; | |
396 | argv[2] = old_argv[1]; | |
397 | argv[3] = old_argv[2]; | |
398 | argv[4] = old_argv[3]; | |
399 | argv[5] = old_argv[4]; | |
400 | argv[6] = old_argv[5]; | |
401 | argv[7] = old_argv[6]; | |
402 | argv[8] = old_argv[7]; | |
080e5e2c | 403 | argv[9] = old_argv[8]; |
80e8aa14 | 404 | argv[10] = old_argv[10]; |
405 | argv[11] = old_argv[11]; | |
406 | argv[12] = old_argv[12]; | |
407 | argv[13] = old_argv[13]; | |
408 | argv[14] = old_argv[14]; | |
080e5e2c | 409 | argv[15] = old_argv[15]; |
80e8aa14 | 410 | |
411 | argv[0] = canonicalize_hostname(strdup(hostname)); | |
412 | if (newname) | |
6c114a34 | 413 | argv[1] = canonicalize_hostname(strdup(newname)); |
80e8aa14 | 414 | if (vendor) |
415 | argv[2] = vendor; | |
416 | if (model) | |
417 | argv[3] = model; | |
418 | if (os) | |
419 | argv[4] = os; | |
420 | if (location) | |
421 | argv[5] = location; | |
422 | if (contact) | |
423 | argv[6] = contact; | |
080e5e2c | 424 | if (billing_contact) |
425 | argv[7] = billing_contact; | |
80e8aa14 | 426 | if (h_status) |
080e5e2c | 427 | argv[9] = h_status; |
80e8aa14 | 428 | if (network) |
080e5e2c | 429 | argv[10] = network; |
80e8aa14 | 430 | if (address) |
080e5e2c | 431 | argv[11] = address; |
80e8aa14 | 432 | if (adm_cmt) |
080e5e2c | 433 | argv[14] = adm_cmt; |
80e8aa14 | 434 | if (op_cmt) |
080e5e2c | 435 | argv[15] = op_cmt; |
80e8aa14 | 436 | |
437 | if (owner) | |
438 | { | |
080e5e2c | 439 | argv[13] = owner->name; |
80e8aa14 | 440 | switch (owner->type) |
441 | { | |
442 | case M_ANY: | |
443 | case M_USER: | |
080e5e2c | 444 | argv[12] = "USER"; |
445 | status = wrap_mr_query("update_host", 16, argv, NULL, NULL); | |
80e8aa14 | 446 | if (owner->type != M_ANY || status != MR_USER) |
447 | break; | |
448 | ||
449 | case M_LIST: | |
080e5e2c | 450 | argv[12] = "LIST"; |
451 | status = wrap_mr_query("update_host", 16, argv, NULL, NULL); | |
80e8aa14 | 452 | break; |
453 | ||
454 | case M_KERBEROS: | |
080e5e2c | 455 | argv[12] = "KERBEROS"; |
456 | status = wrap_mr_query("update_host", 16, argv, NULL, NULL); | |
80e8aa14 | 457 | break; |
458 | ||
459 | case M_NONE: | |
080e5e2c | 460 | argv[12] = "NONE"; |
461 | status = wrap_mr_query("update_host", 16, argv, NULL, NULL); | |
80e8aa14 | 462 | break; |
463 | } | |
464 | } | |
465 | else | |
080e5e2c | 466 | status = wrap_mr_query("update_host", 16, argv, NULL, NULL); |
80e8aa14 | 467 | |
468 | if (status) | |
469 | com_err(whoami, status, "while updating host."); | |
470 | else if (newname) | |
471 | hostname = newname; | |
472 | } | |
473 | ||
474 | /* create aliases if necessary */ | |
475 | if (alias_add_queue) { | |
476 | struct string_list *q = alias_add_queue; | |
477 | ||
478 | while(q) { | |
479 | char *alias = q->string; | |
480 | char *args[2]; | |
481 | ||
5aa7fdfd | 482 | args[0] = partial_canonicalize_hostname(strdup(alias)); |
80e8aa14 | 483 | args[1] = canonicalize_hostname(strdup(hostname)); |
484 | status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL); | |
485 | if (status) { | |
486 | com_err(whoami, status, "while adding host alias"); | |
487 | exit(1); | |
488 | } | |
489 | ||
490 | q = q->next; | |
491 | } | |
492 | } | |
493 | ||
494 | /* delete aliases if necessary */ | |
495 | if (alias_remove_queue) { | |
496 | struct string_list *q = alias_remove_queue; | |
497 | ||
498 | while(q) { | |
499 | char *alias = q->string; | |
500 | char *args[2]; | |
501 | ||
5aa7fdfd | 502 | args[0] = partial_canonicalize_hostname(strdup(alias)); |
80e8aa14 | 503 | args[1] = canonicalize_hostname(strdup(hostname)); |
504 | status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL); | |
505 | if (status) { | |
506 | com_err(whoami, status, "while deleting host alias"); | |
507 | exit(1); | |
508 | } | |
509 | ||
510 | q = q->next; | |
511 | } | |
512 | } | |
513 | ||
514 | /* create cluster mappings */ | |
515 | if (map_add_queue) { | |
516 | struct string_list *q = map_add_queue; | |
517 | ||
518 | while(q) { | |
519 | char *clustername = q->string; | |
520 | char *args[2]; | |
521 | ||
522 | args[0] = canonicalize_hostname(strdup(hostname)); | |
523 | args[1] = clustername; | |
524 | status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL); | |
525 | if (status) { | |
526 | com_err(whoami, status, "while adding cluster mapping"); | |
527 | exit(1); | |
528 | } | |
529 | ||
530 | q = q->next; | |
531 | } | |
532 | } | |
533 | ||
534 | /* delete cluster mappings */ | |
535 | if (map_remove_queue) { | |
536 | struct string_list *q = map_remove_queue; | |
537 | ||
538 | while(q) { | |
539 | char *clustername = q->string; | |
540 | char *args[2]; | |
541 | ||
542 | args[0] = canonicalize_hostname(strdup(hostname)); | |
543 | args[1] = clustername; | |
544 | status = wrap_mr_query("delete_machine_from_cluster", 2, args, | |
545 | NULL, NULL); | |
546 | if (status) { | |
547 | com_err(whoami, status, "while deleting cluster mapping"); | |
548 | exit(1); | |
549 | } | |
550 | ||
551 | q = q->next; | |
552 | } | |
553 | } | |
554 | ||
555 | /* display list info if requested to */ | |
556 | if (info_flag) { | |
557 | struct mqelem *elem = NULL; | |
558 | char *args[5]; | |
559 | char *argv[30]; | |
560 | ||
561 | args[0] = canonicalize_hostname(strdup(hostname)); | |
562 | args[1] = args[2] = args[3] = "*"; | |
563 | status = wrap_mr_query("get_host", 4, args, store_host_info, argv); | |
564 | if (status) { | |
565 | com_err(whoami, status, "while getting host information"); | |
566 | exit(1); | |
567 | } | |
568 | show_host_info(argv); | |
569 | } | |
570 | ||
571 | /* list cluster mappings if needed */ | |
572 | if (list_map_flag) { | |
573 | char *args[3]; | |
574 | ||
575 | args[0] = canonicalize_hostname(strdup(hostname)); | |
576 | args[1] = "*"; | |
577 | status = wrap_mr_query("get_machine_to_cluster_map", 2, args, | |
578 | show_machine_in_cluster, NULL); | |
579 | if (status) | |
580 | if (status != MR_NO_MATCH) { | |
581 | com_err(whoami, status, "while getting cluster mappings"); | |
582 | exit(1); | |
583 | } | |
584 | } | |
585 | ||
586 | if (delete_flag) { | |
587 | char *argv[1]; | |
588 | ||
589 | argv[0] = canonicalize_hostname(strdup(hostname)); | |
590 | status = wrap_mr_query("delete_host", 1, argv, NULL, NULL); | |
591 | if (status) { | |
592 | com_err(whoami, status, "while deleting host"); | |
593 | exit(1); | |
594 | } | |
595 | } | |
596 | ||
597 | /* We're done! */ | |
598 | mr_disconnect(); | |
599 | exit(success ? 0 : 1); | |
600 | } | |
601 | ||
602 | void usage(char **argv) | |
603 | { | |
533bacb3 | 604 | #define USAGE_OPTIONS_FORMAT " %-39s%s\n" |
80e8aa14 | 605 | fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]); |
606 | fprintf(stderr, "Options are\n"); | |
533bacb3 | 607 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create", |
80e8aa14 | 608 | "-O | -owner owner"); |
533bacb3 | 609 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete", |
80e8aa14 | 610 | "-S | -status status"); |
533bacb3 | 611 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname", |
80e8aa14 | 612 | "-V | -vendor vendor"); |
533bacb3 | 613 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias", |
80e8aa14 | 614 | "-M | -model model"); |
533bacb3 | 615 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias", |
80e8aa14 | 616 | "-L | -location location"); |
533bacb3 | 617 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info", |
80e8aa14 | 618 | "-o | -os os"); |
533bacb3 | 619 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt", |
80e8aa14 | 620 | "-c | -contact contact"); |
533bacb3 | 621 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt", |
080e5e2c | 622 | "-bc | -billingcontact billing_contact"); |
623 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-A | -address address", | |
80e8aa14 | 624 | "-N | -network network"); |
080e5e2c | 625 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-am | -addmap cluster", |
626 | "-dm | deletemap cluster"); | |
627 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-lm | -listmap", | |
628 | "-db | -database host[:port]"); | |
533bacb3 | 629 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose", |
080e5e2c | 630 | "-n | -noauth"); |
80e8aa14 | 631 | exit(1); |
632 | } | |
633 | ||
634 | /* Show alias information */ | |
635 | ||
636 | static int show_has_aliases; | |
637 | ||
638 | int show_alias_info(int argc, char **argv, void *hint) | |
639 | { | |
640 | if(!show_has_aliases++) | |
641 | printf("Aliases: %s", argv[0]); | |
642 | else | |
643 | printf(", %s", argv[0]); | |
644 | ||
645 | return MR_CONT; | |
646 | } | |
647 | ||
648 | static char *states[] = { | |
649 | "Reserved (0)", | |
650 | "Active (1)", | |
651 | "None (2)", | |
652 | "Deleted (3)" | |
653 | }; | |
654 | ||
655 | static char *MacState(int state) | |
656 | { | |
657 | static char buf[BUFSIZ]; | |
658 | ||
659 | if (state < 0 || state > 3) | |
660 | { | |
661 | sprintf(buf, "Unknown (%d)", state); | |
662 | return buf; | |
663 | } | |
664 | return states[state]; | |
665 | } | |
666 | ||
667 | /* Retrieve information about a host */ | |
668 | ||
669 | int store_host_info(int argc, char **argv, void *hint) | |
670 | { | |
671 | int i; | |
672 | char **nargv = hint; | |
673 | ||
674 | for(i=0; i<argc; i++) | |
675 | nargv[i] = strdup(argv[i]); | |
676 | ||
677 | return MR_CONT; | |
678 | } | |
679 | ||
680 | void show_host_info(char **argv) | |
681 | { | |
682 | char tbuf[256]; | |
683 | char *args[3]; | |
684 | struct mqelem *elem = NULL; | |
685 | int stat; | |
686 | ||
687 | printf("Machine: %s\n", argv[M_NAME]); | |
688 | args[0] = "*"; | |
689 | args[1] = argv[M_NAME]; | |
690 | show_has_aliases = 0; | |
691 | stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem); | |
692 | printf("\n"); | |
693 | if (stat) { | |
694 | if (stat != MR_NO_MATCH) | |
695 | com_err(whoami, stat, "while getting aliases"); | |
696 | } else { | |
697 | printf("\n"); | |
698 | } | |
699 | sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE], | |
700 | strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : ""); | |
701 | printf("Address: %-16s Network: %-16s\n", | |
702 | argv[M_ADDR], argv[M_SUBNET]); | |
703 | printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]); | |
704 | printf("Status: %-16s Changed: %s\n", | |
705 | MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]); | |
706 | printf("\n"); | |
080e5e2c | 707 | printf("Vendor: %-16s Location: %s\n", argv[M_VENDOR], |
708 | argv[M_LOC]); | |
709 | printf("Model: %-16s Contact: %s\n", argv[M_MODEL], | |
710 | argv[M_CONTACT]); | |
711 | printf("OS: %-16s Billing Contact: %s\n", argv[M_OS], | |
712 | argv[M_BILL_CONTACT]); | |
713 | printf("\nOpt: %s\n", argv[M_USE]); | |
80e8aa14 | 714 | printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]); |
715 | printf("Op cmt: %s\n", argv[M_OCOMMENT]); | |
716 | printf("\n"); | |
717 | printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]); | |
718 | printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]); | |
719 | } | |
720 | ||
721 | int show_machine_in_cluster(int argc, char **argv, void *hint) | |
722 | { | |
723 | printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]); | |
724 | ||
725 | return MR_CONT; | |
726 | } | |
727 | ||
728 | /* Parse a line of input, fetching a member. NULL is returned if a member | |
729 | * is not found. ';' is a comment character. | |
730 | */ | |
731 | ||
732 | struct owner_type *parse_member(char *s) | |
733 | { | |
734 | struct owner_type *m; | |
735 | char *p, *lastchar; | |
736 | ||
737 | while (*s && isspace(*s)) | |
738 | s++; | |
739 | lastchar = p = s; | |
740 | while (*p && *p != '\n' && *p != ';') | |
741 | { | |
742 | if (isprint(*p) && !isspace(*p)) | |
743 | lastchar = p++; | |
744 | else | |
745 | p++; | |
746 | } | |
747 | lastchar++; | |
748 | *lastchar = '\0'; | |
749 | if (p == s || strlen(s) == 0) | |
750 | return NULL; | |
751 | ||
752 | if (!(m = malloc(sizeof(struct owner_type)))) | |
753 | return NULL; | |
754 | ||
755 | if ((p = strchr(s, ':'))) | |
756 | { | |
757 | *p = '\0'; | |
758 | m->name = ++p; | |
759 | if (!strcasecmp("user", s)) | |
760 | m->type = M_USER; | |
761 | else if (!strcasecmp("list", s)) | |
762 | m->type = M_LIST; | |
80e8aa14 | 763 | else if (!strcasecmp("kerberos", s)) |
764 | m->type = M_KERBEROS; | |
765 | else if (!strcasecmp("none", s)) | |
766 | m->type = M_NONE; | |
767 | else | |
768 | { | |
769 | m->type = M_ANY; | |
770 | *(--p) = ':'; | |
771 | m->name = s; | |
772 | } | |
773 | m->name = strdup(m->name); | |
774 | } | |
775 | else | |
776 | { | |
777 | m->name = strdup(s); | |
778 | m->type = strcasecmp(s, "none") ? M_ANY : M_NONE; | |
779 | } | |
780 | return m; | |
781 | } | |
782 | ||
783 | struct string_list *add_to_string_list(struct string_list *old_list, char *s) { | |
784 | struct string_list *new_list; | |
785 | ||
786 | new_list = (struct string_list *)malloc(sizeof(struct string_list *)); | |
787 | new_list->next = old_list; | |
788 | new_list->string = s; | |
789 | ||
790 | return new_list; | |
791 | } | |
792 | ||
793 | int wrap_mr_query(char *handle, int argc, char **argv, | |
794 | int (*callback)(int, char **, void *), void *callarg) { | |
795 | if (verbose) | |
796 | print_query(handle, argc, argv); | |
797 | ||
798 | return mr_query(handle, argc, argv, callback, callarg); | |
799 | } | |
800 | ||
801 | void print_query(char *query_name, int argc, char **argv) { | |
802 | int cnt; | |
803 | ||
804 | printf("qy %s", query_name); | |
805 | for(cnt=0; cnt<argc; cnt++) | |
806 | printf(" <%s>", argv[cnt]); | |
807 | printf("\n"); | |
808 | } |