1 | /* $Id: stella.c 3968 2010-01-27 23:00:11Z zacheiss $ |
---|
2 | * |
---|
3 | * Command line oriented Moira host tool. |
---|
4 | * |
---|
5 | * kolya@MIT.EDU, January 2000 |
---|
6 | * |
---|
7 | * Somewhat based on blanche |
---|
8 | * |
---|
9 | * Copyright (C) 2000, 2001 by the Massachusetts Institute of Technology. |
---|
10 | * For copying and distribution information, please see the file |
---|
11 | * <mit-copyright.h>. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <mit-copyright.h> |
---|
15 | #include <moira.h> |
---|
16 | #include <moira_site.h> |
---|
17 | #include <mrclient.h> |
---|
18 | |
---|
19 | #include <ctype.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #ifdef _WIN32 |
---|
26 | typedef unsigned long in_addr_t; |
---|
27 | #else |
---|
28 | #include <sys/types.h> |
---|
29 | #include <sys/socket.h> |
---|
30 | |
---|
31 | #include <netinet/in.h> |
---|
32 | #include <arpa/inet.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/clients/stella/stella.c $ $Id: stella.c 3968 2010-01-27 23:00:11Z zacheiss $"); |
---|
36 | |
---|
37 | struct owner_type { |
---|
38 | int type; |
---|
39 | char *name; |
---|
40 | }; |
---|
41 | |
---|
42 | struct mqelem { |
---|
43 | struct mqelem *q_forw; |
---|
44 | struct mqelem *q_back; |
---|
45 | void *q_data; |
---|
46 | }; |
---|
47 | |
---|
48 | struct string_list { |
---|
49 | char *string; |
---|
50 | struct string_list *next; |
---|
51 | }; |
---|
52 | |
---|
53 | #define M_ANY 0 |
---|
54 | #define M_USER 1 |
---|
55 | #define M_LIST 2 |
---|
56 | #define M_KERBEROS 3 |
---|
57 | #define M_NONE 4 |
---|
58 | |
---|
59 | /* argument parsing macro */ |
---|
60 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) |
---|
61 | |
---|
62 | /* flags from command line */ |
---|
63 | int info_flag, update_flag, create_flag, delete_flag, list_map_flag; |
---|
64 | int update_alias_flag, update_map_flag, verbose, noauth; |
---|
65 | int list_container_flag, update_container_flag, unformatted_flag; |
---|
66 | |
---|
67 | struct string_list *alias_add_queue, *alias_remove_queue; |
---|
68 | struct string_list *map_add_queue, *map_remove_queue; |
---|
69 | struct string_list *container_add_queue, *container_remove_queue; |
---|
70 | |
---|
71 | char *hostname, *whoami; |
---|
72 | |
---|
73 | char *newname, *address, *network, *h_status, *vendor, *model; |
---|
74 | char *os, *location, *contact, *billing_contact, *account_number; |
---|
75 | char *adm_cmt, *op_cmt; |
---|
76 | |
---|
77 | in_addr_t ipaddress; |
---|
78 | struct owner_type *owner; |
---|
79 | |
---|
80 | void usage(char **argv); |
---|
81 | int store_host_info(int argc, char **argv, void *hint); |
---|
82 | void show_host_info(char **argv); |
---|
83 | void show_host_info_unformatted(char **argv); |
---|
84 | int show_machine_in_cluster(int argc, char **argv, void *hint); |
---|
85 | int show_machine_in_container(int argc, char **argv, void *hint); |
---|
86 | struct owner_type *parse_member(char *s); |
---|
87 | struct string_list *add_to_string_list(struct string_list *old_list, char *s); |
---|
88 | int wrap_mr_query(char *handle, int argc, char **argv, |
---|
89 | int (*callback)(int, char **, void *), void *callarg); |
---|
90 | void print_query(char *query_name, int argc, char **argv); |
---|
91 | |
---|
92 | int main(int argc, char **argv) |
---|
93 | { |
---|
94 | int status, success; |
---|
95 | char **arg = argv; |
---|
96 | char *server = NULL; |
---|
97 | |
---|
98 | /* clear all flags & lists */ |
---|
99 | info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0; |
---|
100 | update_alias_flag = verbose = noauth = 0; |
---|
101 | list_container_flag = update_container_flag = 0; |
---|
102 | newname = address = network = h_status = vendor = model = NULL; |
---|
103 | os = location = contact = billing_contact = account_number = adm_cmt = NULL; |
---|
104 | op_cmt = NULL; |
---|
105 | owner = NULL; |
---|
106 | alias_add_queue = alias_remove_queue = NULL; |
---|
107 | map_add_queue = map_remove_queue = NULL; |
---|
108 | container_add_queue = container_remove_queue = NULL; |
---|
109 | whoami = argv[0]; |
---|
110 | |
---|
111 | success = 1; |
---|
112 | |
---|
113 | /* parse args, building addlist, dellist, & synclist */ |
---|
114 | while (++arg - argv < argc) |
---|
115 | { |
---|
116 | if (**arg == '-') |
---|
117 | { |
---|
118 | if (argis("i", "info")) |
---|
119 | info_flag++; |
---|
120 | else if (argis("C", "create")) |
---|
121 | create_flag++; |
---|
122 | else if (argis("D", "delete")) |
---|
123 | delete_flag++; |
---|
124 | else if (argis("R", "rename")) { |
---|
125 | if (arg - argv < argc - 1) { |
---|
126 | arg++; |
---|
127 | update_flag++; |
---|
128 | newname = *arg; |
---|
129 | } else |
---|
130 | usage(argv); |
---|
131 | } |
---|
132 | else if (argis("A", "address")) { |
---|
133 | if (arg - argv < argc - 1) { |
---|
134 | arg++; |
---|
135 | update_flag++; |
---|
136 | address = *arg; |
---|
137 | } else |
---|
138 | usage(argv); |
---|
139 | } |
---|
140 | else if (argis("O", "owner")) { |
---|
141 | if (arg - argv < argc - 1) { |
---|
142 | arg++; |
---|
143 | update_flag++; |
---|
144 | owner = parse_member(*arg); |
---|
145 | } else |
---|
146 | usage(argv); |
---|
147 | } |
---|
148 | else if (argis("N", "network")) { |
---|
149 | if (arg - argv < argc - 1) { |
---|
150 | arg++; |
---|
151 | update_flag++; |
---|
152 | network = *arg; |
---|
153 | } else |
---|
154 | usage(argv); |
---|
155 | } |
---|
156 | else if (argis("S", "status")) { |
---|
157 | if (arg - argv < argc - 1) { |
---|
158 | int i; |
---|
159 | int len; |
---|
160 | |
---|
161 | arg++; |
---|
162 | update_flag++; |
---|
163 | h_status = *arg; |
---|
164 | |
---|
165 | len = strlen(h_status); |
---|
166 | for(i = 0; i < len; i++) { |
---|
167 | if(!isdigit(h_status[i])) { |
---|
168 | printf("Error: status code %s is not numeric.\n", h_status); |
---|
169 | exit(1); |
---|
170 | } |
---|
171 | } |
---|
172 | } else |
---|
173 | usage(argv); |
---|
174 | } |
---|
175 | else if (argis("V", "vendor")) { |
---|
176 | if (arg - argv < argc - 1) { |
---|
177 | arg++; |
---|
178 | update_flag++; |
---|
179 | vendor = *arg; |
---|
180 | } else |
---|
181 | usage(argv); |
---|
182 | } |
---|
183 | else if (argis("M", "model")) { |
---|
184 | if (arg - argv < argc - 1) { |
---|
185 | arg++; |
---|
186 | update_flag++; |
---|
187 | model = *arg; |
---|
188 | } else |
---|
189 | usage(argv); |
---|
190 | } |
---|
191 | else if (argis("o", "os")) { |
---|
192 | if (arg - argv < argc - 1) { |
---|
193 | arg++; |
---|
194 | update_flag++; |
---|
195 | os = *arg; |
---|
196 | } else |
---|
197 | usage(argv); |
---|
198 | } |
---|
199 | else if (argis("L", "location")) { |
---|
200 | if (arg - argv < argc - 1) { |
---|
201 | arg++; |
---|
202 | update_flag++; |
---|
203 | location = *arg; |
---|
204 | } else |
---|
205 | usage(argv); |
---|
206 | } |
---|
207 | else if (argis("c", "contact")) { |
---|
208 | if (arg - argv < argc - 1) { |
---|
209 | arg++; |
---|
210 | update_flag++; |
---|
211 | contact = *arg; |
---|
212 | } else |
---|
213 | usage(argv); |
---|
214 | } |
---|
215 | else if (argis("bc", "billingcontact")) { |
---|
216 | if (arg - argv < argc - 1) { |
---|
217 | arg++; |
---|
218 | update_flag++; |
---|
219 | billing_contact = *arg; |
---|
220 | } else |
---|
221 | usage(argv); |
---|
222 | } |
---|
223 | else if (argis("an", "accountnumber")) { |
---|
224 | if (arg - argv < argc - 1) { |
---|
225 | arg++; |
---|
226 | update_flag++; |
---|
227 | account_number = *arg; |
---|
228 | } else |
---|
229 | usage(argv); |
---|
230 | } |
---|
231 | else if (argis("ac", "admcmt")) { |
---|
232 | if (arg - argv < argc - 1) { |
---|
233 | arg++; |
---|
234 | update_flag++; |
---|
235 | adm_cmt = *arg; |
---|
236 | } else |
---|
237 | usage(argv); |
---|
238 | } |
---|
239 | else if (argis("oc", "opcmt")) { |
---|
240 | if (arg - argv < argc - 1) { |
---|
241 | arg++; |
---|
242 | update_flag++; |
---|
243 | op_cmt = *arg; |
---|
244 | } else |
---|
245 | usage(argv); |
---|
246 | } |
---|
247 | else if (argis("a", "addalias")) { |
---|
248 | if (arg - argv < argc - 1) { |
---|
249 | arg++; |
---|
250 | alias_add_queue=add_to_string_list(alias_add_queue, *arg); |
---|
251 | } else |
---|
252 | usage(argv); |
---|
253 | update_alias_flag++; |
---|
254 | } |
---|
255 | else if (argis("d", "deletealias")) { |
---|
256 | if (arg - argv < argc - 1) { |
---|
257 | arg++; |
---|
258 | alias_remove_queue=add_to_string_list(alias_remove_queue, *arg); |
---|
259 | } else |
---|
260 | usage(argv); |
---|
261 | update_alias_flag++; |
---|
262 | } |
---|
263 | else if (argis("am", "addmap")) { |
---|
264 | if (arg - argv < argc - 1) { |
---|
265 | arg++; |
---|
266 | map_add_queue=add_to_string_list(map_add_queue, *arg); |
---|
267 | } else |
---|
268 | usage(argv); |
---|
269 | update_map_flag++; |
---|
270 | } |
---|
271 | else if (argis("dm", "deletemap")) { |
---|
272 | if (arg - argv < argc - 1) { |
---|
273 | arg++; |
---|
274 | map_remove_queue=add_to_string_list(map_remove_queue, *arg); |
---|
275 | } else |
---|
276 | usage(argv); |
---|
277 | update_map_flag++; |
---|
278 | } |
---|
279 | else if (argis("lm", "listmap")) |
---|
280 | list_map_flag++; |
---|
281 | else if (argis("acn", "addcontainer")) { |
---|
282 | if (arg - argv < argc - 1) { |
---|
283 | arg++; |
---|
284 | container_add_queue = |
---|
285 | add_to_string_list(container_add_queue, *arg); |
---|
286 | } else |
---|
287 | usage(argv); |
---|
288 | update_container_flag++; |
---|
289 | } |
---|
290 | else if (argis("dcn", "deletecontainer")) { |
---|
291 | if (arg - argv < argc - 1) { |
---|
292 | arg++; |
---|
293 | container_remove_queue = |
---|
294 | add_to_string_list(container_remove_queue, *arg); |
---|
295 | } else |
---|
296 | usage(argv); |
---|
297 | update_container_flag++; |
---|
298 | } |
---|
299 | else if (argis("lcn", "listcontainer")) |
---|
300 | list_container_flag++; |
---|
301 | else if (argis("u", "unformatted")) |
---|
302 | unformatted_flag++; |
---|
303 | else if (argis("n", "noauth")) |
---|
304 | noauth++; |
---|
305 | else if (argis("v", "verbose")) |
---|
306 | verbose++; |
---|
307 | else if (argis("db", "database")) |
---|
308 | { |
---|
309 | if (arg - argv < argc - 1) |
---|
310 | { |
---|
311 | ++arg; |
---|
312 | server = *arg; |
---|
313 | } |
---|
314 | else |
---|
315 | usage(argv); |
---|
316 | } |
---|
317 | else |
---|
318 | usage(argv); |
---|
319 | } |
---|
320 | else if (hostname == NULL) |
---|
321 | hostname = *arg; |
---|
322 | else |
---|
323 | usage(argv); |
---|
324 | } |
---|
325 | if (hostname == NULL) |
---|
326 | usage(argv); |
---|
327 | |
---|
328 | /* default to info_flag if nothing else was specified */ |
---|
329 | if(!(info_flag || update_flag || create_flag || \ |
---|
330 | delete_flag || list_map_flag || update_map_flag || \ |
---|
331 | update_alias_flag || update_container_flag || \ |
---|
332 | list_container_flag)) { |
---|
333 | info_flag++; |
---|
334 | } |
---|
335 | |
---|
336 | /* fire up Moira */ |
---|
337 | status = mrcl_connect(server, "stella", 8, !noauth); |
---|
338 | if (status == MRCL_AUTH_ERROR) |
---|
339 | { |
---|
340 | com_err(whoami, 0, "Try the -noauth flag if you don't " |
---|
341 | "need authentication."); |
---|
342 | } |
---|
343 | if (status) |
---|
344 | exit(2); |
---|
345 | |
---|
346 | /* Perform the lookup by IP address if that's what we've been handed */ |
---|
347 | if ((ipaddress=inet_addr(hostname)) != -1) { |
---|
348 | char *args[5]; |
---|
349 | char *argv[30]; |
---|
350 | |
---|
351 | args[1] = strdup(hostname); |
---|
352 | args[0] = args[2] = args[3] = "*"; |
---|
353 | status = wrap_mr_query("get_host", 4, args, store_host_info, argv); |
---|
354 | |
---|
355 | if (status) { |
---|
356 | com_err(whoami, status, "while looking up IP address."); |
---|
357 | } else { |
---|
358 | hostname = argv[0]; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | /* create if needed */ |
---|
363 | if (create_flag) |
---|
364 | { |
---|
365 | char *argv[30]; |
---|
366 | int cnt; |
---|
367 | |
---|
368 | for (cnt = 0; cnt < 17; cnt++) { |
---|
369 | argv[cnt] = ""; |
---|
370 | } |
---|
371 | |
---|
372 | argv[0] = canonicalize_hostname(strdup(hostname)); |
---|
373 | |
---|
374 | if (vendor) |
---|
375 | argv[1] = vendor; |
---|
376 | if (model) |
---|
377 | argv[2] = model; |
---|
378 | if (os) |
---|
379 | argv[3] = os; |
---|
380 | if (location) |
---|
381 | argv[4] = location; |
---|
382 | if (contact) |
---|
383 | argv[5] = contact; |
---|
384 | if (billing_contact) |
---|
385 | argv[6] = billing_contact; |
---|
386 | if (account_number) |
---|
387 | argv[7] = account_number; |
---|
388 | /* The use field always gets set to "0" */ |
---|
389 | argv[8] = "0"; |
---|
390 | if (h_status) |
---|
391 | argv[9] = h_status; |
---|
392 | else |
---|
393 | argv[9] = "1"; |
---|
394 | if (network) |
---|
395 | argv[10] = network; |
---|
396 | if (address) |
---|
397 | argv[11] = address; |
---|
398 | else |
---|
399 | argv[11] = "unique"; |
---|
400 | if (adm_cmt) |
---|
401 | argv[14] = adm_cmt; |
---|
402 | if (op_cmt) |
---|
403 | argv[15] = op_cmt; |
---|
404 | |
---|
405 | if (owner) |
---|
406 | { |
---|
407 | argv[13] = owner->name; |
---|
408 | switch (owner->type) |
---|
409 | { |
---|
410 | case M_ANY: |
---|
411 | case M_USER: |
---|
412 | argv[12] = "USER"; |
---|
413 | status = wrap_mr_query("add_host", 16, argv, NULL, NULL); |
---|
414 | if (owner->type != M_ANY || status != MR_USER) |
---|
415 | break; |
---|
416 | |
---|
417 | case M_LIST: |
---|
418 | argv[12] = "LIST"; |
---|
419 | status = wrap_mr_query("add_host", 16, argv, NULL, NULL); |
---|
420 | break; |
---|
421 | |
---|
422 | case M_KERBEROS: |
---|
423 | argv[12] = "KERBEROS"; |
---|
424 | status = mrcl_validate_kerberos_member(argv[13], &argv[13]); |
---|
425 | if (mrcl_get_message()) |
---|
426 | mrcl_com_err(whoami); |
---|
427 | if (status == MRCL_REJECT) |
---|
428 | exit(1); |
---|
429 | status = wrap_mr_query("add_host", 16, argv, NULL, NULL); |
---|
430 | break; |
---|
431 | |
---|
432 | case M_NONE: |
---|
433 | argv[12] = "NONE"; |
---|
434 | status = wrap_mr_query("add_host", 16, argv, NULL, NULL); |
---|
435 | break; |
---|
436 | } |
---|
437 | } |
---|
438 | else |
---|
439 | { |
---|
440 | argv[12] = "NONE"; |
---|
441 | argv[13] = "NONE"; |
---|
442 | |
---|
443 | status = wrap_mr_query("add_host", 16, argv, NULL, NULL); |
---|
444 | } |
---|
445 | |
---|
446 | if (status) |
---|
447 | { |
---|
448 | com_err(whoami, status, "while creating host."); |
---|
449 | exit(1); |
---|
450 | } |
---|
451 | |
---|
452 | } |
---|
453 | else if (update_flag) |
---|
454 | { |
---|
455 | char *old_argv[30]; |
---|
456 | char *argv[17]; |
---|
457 | char *args[5]; |
---|
458 | |
---|
459 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
460 | args[1] = args[2] = args[3] = "*"; |
---|
461 | |
---|
462 | status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv); |
---|
463 | if (status) |
---|
464 | { |
---|
465 | com_err(whoami, status, "while getting list information"); |
---|
466 | exit(1); |
---|
467 | } |
---|
468 | |
---|
469 | argv[1] = old_argv[0]; |
---|
470 | argv[2] = old_argv[1]; |
---|
471 | argv[3] = old_argv[2]; |
---|
472 | argv[4] = old_argv[3]; |
---|
473 | argv[5] = old_argv[4]; |
---|
474 | argv[6] = old_argv[5]; |
---|
475 | argv[7] = old_argv[6]; |
---|
476 | argv[8] = old_argv[7]; |
---|
477 | argv[9] = old_argv[8]; |
---|
478 | argv[10] = old_argv[9]; |
---|
479 | argv[11] = old_argv[11]; |
---|
480 | argv[12] = old_argv[12]; |
---|
481 | argv[13] = old_argv[13]; |
---|
482 | argv[14] = old_argv[14]; |
---|
483 | argv[15] = old_argv[15]; |
---|
484 | argv[16] = old_argv[16]; |
---|
485 | |
---|
486 | argv[0] = canonicalize_hostname(strdup(hostname)); |
---|
487 | if (newname) |
---|
488 | argv[1] = canonicalize_hostname(strdup(newname)); |
---|
489 | if (vendor) |
---|
490 | argv[2] = vendor; |
---|
491 | if (model) |
---|
492 | argv[3] = model; |
---|
493 | if (os) |
---|
494 | argv[4] = os; |
---|
495 | if (location) |
---|
496 | argv[5] = location; |
---|
497 | if (contact) |
---|
498 | argv[6] = contact; |
---|
499 | if (billing_contact) |
---|
500 | argv[7] = billing_contact; |
---|
501 | if (account_number) |
---|
502 | argv[8] = account_number; |
---|
503 | if (h_status) |
---|
504 | argv[10] = h_status; |
---|
505 | if (network) |
---|
506 | argv[11] = network; |
---|
507 | if (address) |
---|
508 | argv[12] = address; |
---|
509 | if (adm_cmt) |
---|
510 | argv[15] = adm_cmt; |
---|
511 | if (op_cmt) |
---|
512 | argv[16] = op_cmt; |
---|
513 | |
---|
514 | if (owner) |
---|
515 | { |
---|
516 | argv[14] = owner->name; |
---|
517 | switch (owner->type) |
---|
518 | { |
---|
519 | case M_ANY: |
---|
520 | case M_USER: |
---|
521 | argv[13] = "USER"; |
---|
522 | status = wrap_mr_query("update_host", 17, argv, NULL, NULL); |
---|
523 | if (owner->type != M_ANY || status != MR_USER) |
---|
524 | break; |
---|
525 | |
---|
526 | case M_LIST: |
---|
527 | argv[13] = "LIST"; |
---|
528 | status = wrap_mr_query("update_host", 17, argv, NULL, NULL); |
---|
529 | break; |
---|
530 | |
---|
531 | case M_KERBEROS: |
---|
532 | argv[13] = "KERBEROS"; |
---|
533 | status = mrcl_validate_kerberos_member(argv[14], &argv[14]); |
---|
534 | if (mrcl_get_message()) |
---|
535 | mrcl_com_err(whoami); |
---|
536 | if (status == MRCL_REJECT) |
---|
537 | exit(1); |
---|
538 | status = wrap_mr_query("update_host", 17, argv, NULL, NULL); |
---|
539 | break; |
---|
540 | |
---|
541 | case M_NONE: |
---|
542 | argv[13] = "NONE"; |
---|
543 | status = wrap_mr_query("update_host", 17, argv, NULL, NULL); |
---|
544 | break; |
---|
545 | } |
---|
546 | } |
---|
547 | else |
---|
548 | status = wrap_mr_query("update_host", 17, argv, NULL, NULL); |
---|
549 | |
---|
550 | if (status) |
---|
551 | com_err(whoami, status, "while updating host."); |
---|
552 | else if (newname) |
---|
553 | hostname = newname; |
---|
554 | } |
---|
555 | |
---|
556 | /* create aliases if necessary */ |
---|
557 | if (alias_add_queue) { |
---|
558 | struct string_list *q = alias_add_queue; |
---|
559 | |
---|
560 | while(q) { |
---|
561 | char *alias = q->string; |
---|
562 | char *args[2]; |
---|
563 | |
---|
564 | args[0] = partial_canonicalize_hostname(strdup(alias)); |
---|
565 | args[1] = canonicalize_hostname(strdup(hostname)); |
---|
566 | status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL); |
---|
567 | if (status) { |
---|
568 | com_err(whoami, status, "while adding host alias"); |
---|
569 | exit(1); |
---|
570 | } |
---|
571 | |
---|
572 | q = q->next; |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | /* delete aliases if necessary */ |
---|
577 | if (alias_remove_queue) { |
---|
578 | struct string_list *q = alias_remove_queue; |
---|
579 | |
---|
580 | while(q) { |
---|
581 | char *alias = q->string; |
---|
582 | char *args[2]; |
---|
583 | |
---|
584 | args[0] = partial_canonicalize_hostname(strdup(alias)); |
---|
585 | args[1] = canonicalize_hostname(strdup(hostname)); |
---|
586 | status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL); |
---|
587 | if (status) { |
---|
588 | com_err(whoami, status, "while deleting host alias"); |
---|
589 | exit(1); |
---|
590 | } |
---|
591 | |
---|
592 | q = q->next; |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | /* create cluster mappings */ |
---|
597 | if (map_add_queue) { |
---|
598 | struct string_list *q = map_add_queue; |
---|
599 | |
---|
600 | while(q) { |
---|
601 | char *clustername = q->string; |
---|
602 | char *args[2]; |
---|
603 | |
---|
604 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
605 | args[1] = clustername; |
---|
606 | status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL); |
---|
607 | if (status) { |
---|
608 | com_err(whoami, status, "while adding cluster mapping"); |
---|
609 | exit(1); |
---|
610 | } |
---|
611 | |
---|
612 | q = q->next; |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | /* delete cluster mappings */ |
---|
617 | if (map_remove_queue) { |
---|
618 | struct string_list *q = map_remove_queue; |
---|
619 | |
---|
620 | while(q) { |
---|
621 | char *clustername = q->string; |
---|
622 | char *args[2]; |
---|
623 | |
---|
624 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
625 | args[1] = clustername; |
---|
626 | status = wrap_mr_query("delete_machine_from_cluster", 2, args, |
---|
627 | NULL, NULL); |
---|
628 | if (status) { |
---|
629 | com_err(whoami, status, "while deleting cluster mapping"); |
---|
630 | exit(1); |
---|
631 | } |
---|
632 | |
---|
633 | q = q->next; |
---|
634 | } |
---|
635 | } |
---|
636 | |
---|
637 | /* add container mappings */ |
---|
638 | if (container_add_queue) { |
---|
639 | struct string_list *q = container_add_queue; |
---|
640 | |
---|
641 | while (q) { |
---|
642 | char *containername = q->string; |
---|
643 | char *args[2]; |
---|
644 | |
---|
645 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
646 | args[1] = containername; |
---|
647 | status = wrap_mr_query("add_machine_to_container", 2, args, |
---|
648 | NULL, NULL); |
---|
649 | |
---|
650 | if (status) { |
---|
651 | com_err(whoami, status, "while adding container mapping"); |
---|
652 | exit(1); |
---|
653 | } |
---|
654 | |
---|
655 | q = q->next; |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | /* delete container mappings */ |
---|
660 | if (container_remove_queue) { |
---|
661 | struct string_list *q = container_remove_queue; |
---|
662 | |
---|
663 | while (q) { |
---|
664 | char *containername = q->string; |
---|
665 | char *args[2]; |
---|
666 | |
---|
667 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
668 | args[1] = containername; |
---|
669 | status = wrap_mr_query("delete_machine_from_container", 2, args, |
---|
670 | NULL, NULL); |
---|
671 | |
---|
672 | if (status) { |
---|
673 | com_err(whoami, status, "while deleting container mapping"); |
---|
674 | exit(1); |
---|
675 | } |
---|
676 | |
---|
677 | q = q->next; |
---|
678 | } |
---|
679 | } |
---|
680 | |
---|
681 | /* display list info if requested to */ |
---|
682 | if (info_flag) { |
---|
683 | struct mqelem *elem = NULL; |
---|
684 | char *args[5]; |
---|
685 | char *argv[30]; |
---|
686 | |
---|
687 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
688 | args[1] = args[2] = args[3] = "*"; |
---|
689 | status = wrap_mr_query("get_host", 4, args, store_host_info, argv); |
---|
690 | |
---|
691 | /* We might be looking for an alias of a deleted host. */ |
---|
692 | if (status && status == MR_NO_MATCH) { |
---|
693 | status = wrap_mr_query("get_hostalias", 2, args, store_host_info, argv); |
---|
694 | if (!status) { |
---|
695 | args[0] = strdup(argv[1]); |
---|
696 | status = wrap_mr_query("get_host", 4, args, store_host_info, argv); |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | if (status) { |
---|
701 | com_err(whoami, status, "while getting host information"); |
---|
702 | exit(1); |
---|
703 | } |
---|
704 | |
---|
705 | if (unformatted_flag) |
---|
706 | show_host_info_unformatted(argv); |
---|
707 | else |
---|
708 | show_host_info(argv); |
---|
709 | args[0] = argv[M_SUBNET]; |
---|
710 | status = wrap_mr_query("get_subnet", 1, args, store_host_info, argv); |
---|
711 | if (status) |
---|
712 | com_err(whoami, status, "while getting subnet information"); |
---|
713 | if (atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_10MBPS || |
---|
714 | atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_100MBPS || |
---|
715 | atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_1000MBPS) |
---|
716 | { |
---|
717 | fprintf(stderr, "\nWarning: This host is on a private subnet.\n"); |
---|
718 | fprintf(stderr, "Billing information shown is superseded by billing information for the subnet.\n"); |
---|
719 | } |
---|
720 | } |
---|
721 | |
---|
722 | /* list cluster mappings if needed */ |
---|
723 | if (list_map_flag) { |
---|
724 | char *args[3]; |
---|
725 | |
---|
726 | args[0] = canonicalize_hostname(strdup(hostname)); |
---|
727 | args[1] = "*"; |
---|
728 | status = wrap_mr_query("get_machine_to_cluster_map", 2, args, |
---|
729 | show_machine_in_cluster, NULL); |
---|
730 | if (status) |
---|
731 | if (status != MR_NO_MATCH) { |
---|
732 | com_err(whoami, status, "while getting cluster mappings"); |
---|
733 | exit(1); |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | /* list container mappings if needed */ |
---|
738 | if (list_container_flag) { |
---|
739 | char *argv[1]; |
---|
740 | |
---|
741 | argv[0] = canonicalize_hostname(strdup(hostname)); |
---|
742 | status = wrap_mr_query("get_machine_to_container_map", 1, argv, |
---|
743 | show_machine_in_container, NULL); |
---|
744 | |
---|
745 | if (status) |
---|
746 | if (status != MR_NO_MATCH) { |
---|
747 | com_err(whoami, status, "while getting container mappings"); |
---|
748 | exit(1); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | if (delete_flag) { |
---|
753 | char *argv[1]; |
---|
754 | |
---|
755 | argv[0] = canonicalize_hostname(strdup(hostname)); |
---|
756 | status = wrap_mr_query("delete_host", 1, argv, NULL, NULL); |
---|
757 | if (status) { |
---|
758 | com_err(whoami, status, "while deleting host"); |
---|
759 | exit(1); |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | /* We're done! */ |
---|
764 | mr_disconnect(); |
---|
765 | exit(success ? 0 : 1); |
---|
766 | } |
---|
767 | |
---|
768 | void usage(char **argv) |
---|
769 | { |
---|
770 | #define USAGE_OPTIONS_FORMAT " %-39s%s\n" |
---|
771 | fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]); |
---|
772 | fprintf(stderr, "Options are\n"); |
---|
773 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create", |
---|
774 | "-O | -owner owner"); |
---|
775 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete", |
---|
776 | "-S | -status status"); |
---|
777 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname", |
---|
778 | "-V | -vendor vendor"); |
---|
779 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias", |
---|
780 | "-M | -model model"); |
---|
781 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias", |
---|
782 | "-L | -location location"); |
---|
783 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info", |
---|
784 | "-o | -os os"); |
---|
785 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt", |
---|
786 | "-c | -contact contact"); |
---|
787 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt", |
---|
788 | "-bc | -billingcontact billing_contact"); |
---|
789 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-an | -accountnumber account_number", "-A | -address address"); |
---|
790 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-N | -network network", |
---|
791 | "-am | -addmap cluster"); |
---|
792 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dm | deletemap cluster", |
---|
793 | "-acn | -addcontainer container"); |
---|
794 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dcn | -deletecontainer container", |
---|
795 | "-lm | -listmap"); |
---|
796 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-lcn | -listcontainer", |
---|
797 | "-u | -unformatted"); |
---|
798 | fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose", |
---|
799 | "-n | -noauth"); |
---|
800 | fprintf(stderr, " %-39s\n" , "-db | -database host[:port]"); |
---|
801 | exit(1); |
---|
802 | } |
---|
803 | |
---|
804 | /* Show alias information */ |
---|
805 | |
---|
806 | static int show_has_aliases; |
---|
807 | |
---|
808 | int show_alias_info(int argc, char **argv, void *hint) |
---|
809 | { |
---|
810 | if(!show_has_aliases++) |
---|
811 | printf("Aliases: %s", argv[0]); |
---|
812 | else |
---|
813 | printf(", %s", argv[0]); |
---|
814 | |
---|
815 | return MR_CONT; |
---|
816 | } |
---|
817 | |
---|
818 | int show_alias_info_unformatted(int argc, char **argv, void *hint) |
---|
819 | { |
---|
820 | if(!show_has_aliases++) |
---|
821 | printf("Alias: %s", argv[0]); |
---|
822 | else |
---|
823 | printf(", %s", argv[0]); |
---|
824 | |
---|
825 | return MR_CONT; |
---|
826 | } |
---|
827 | |
---|
828 | static char *states[] = { |
---|
829 | "Reserved (0)", |
---|
830 | "Active (1)", |
---|
831 | "Local (2)", |
---|
832 | "Deleted (3)" |
---|
833 | }; |
---|
834 | |
---|
835 | static char *MacState(int state) |
---|
836 | { |
---|
837 | static char buf[BUFSIZ]; |
---|
838 | |
---|
839 | if (state < 0 || state > 3) |
---|
840 | { |
---|
841 | sprintf(buf, "Unknown (%d)", state); |
---|
842 | return buf; |
---|
843 | } |
---|
844 | return states[state]; |
---|
845 | } |
---|
846 | |
---|
847 | /* Retrieve information about a host */ |
---|
848 | |
---|
849 | int store_host_info(int argc, char **argv, void *hint) |
---|
850 | { |
---|
851 | int i; |
---|
852 | char **nargv = hint; |
---|
853 | |
---|
854 | for(i=0; i<argc; i++) |
---|
855 | nargv[i] = strdup(argv[i]); |
---|
856 | |
---|
857 | return MR_CONT; |
---|
858 | } |
---|
859 | |
---|
860 | void show_host_info(char **argv) |
---|
861 | { |
---|
862 | char tbuf[256]; |
---|
863 | char *args[3]; |
---|
864 | struct mqelem *elem = NULL; |
---|
865 | int stat; |
---|
866 | |
---|
867 | printf("Machine: %s\n", argv[M_NAME]); |
---|
868 | args[0] = "*"; |
---|
869 | args[1] = argv[M_NAME]; |
---|
870 | show_has_aliases = 0; |
---|
871 | stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem); |
---|
872 | printf("\n"); |
---|
873 | if (stat) { |
---|
874 | if (stat != MR_NO_MATCH) |
---|
875 | com_err(whoami, stat, "while getting aliases"); |
---|
876 | } else { |
---|
877 | printf("\n"); |
---|
878 | } |
---|
879 | sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE], |
---|
880 | strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : ""); |
---|
881 | printf("Address: %-16s Network: %-16s\n", |
---|
882 | argv[M_ADDR], argv[M_SUBNET]); |
---|
883 | printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]); |
---|
884 | printf("Status: %-16s Changed: %s\n", |
---|
885 | MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]); |
---|
886 | printf("\n"); |
---|
887 | printf("Vendor: %-16s Location: %s\n", argv[M_VENDOR], |
---|
888 | argv[M_LOC]); |
---|
889 | printf("Model: %-16s Contact: %s\n", argv[M_MODEL], |
---|
890 | argv[M_CONTACT]); |
---|
891 | printf("OS: %-16s Billing Contact: %s\n", argv[M_OS], |
---|
892 | argv[M_BILL_CONTACT]); |
---|
893 | printf("Opt: %-16s Account Number: %s\n", argv[M_USE], |
---|
894 | argv[M_ACCT_NUMBER]); |
---|
895 | printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]); |
---|
896 | printf("Op cmt: %s\n", argv[M_OCOMMENT]); |
---|
897 | printf("\n"); |
---|
898 | printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]); |
---|
899 | printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]); |
---|
900 | } |
---|
901 | |
---|
902 | void show_host_info_unformatted(char **argv) |
---|
903 | { |
---|
904 | char *args[3]; |
---|
905 | struct mqelem *elem = NULL; |
---|
906 | int stat; |
---|
907 | |
---|
908 | printf("Machine: %s\n", argv[M_NAME]); |
---|
909 | args[0] = "*"; |
---|
910 | args[1] = argv[M_NAME]; |
---|
911 | show_has_aliases = 0; |
---|
912 | stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info_unformatted, |
---|
913 | &elem); |
---|
914 | if (stat && stat != MR_NO_MATCH) |
---|
915 | com_err(whoami, stat, "while getting aliases"); |
---|
916 | else |
---|
917 | printf("\n"); |
---|
918 | printf("Address: %s\n", argv[M_ADDR]); |
---|
919 | printf("Network: %s\n", argv[M_SUBNET]); |
---|
920 | printf("Owner Type: %s\n", argv[M_OWNER_TYPE]); |
---|
921 | printf("Owner: %s\n", argv[M_OWNER_NAME]); |
---|
922 | printf("Status: %s\n", MacState(atoi(argv[M_STAT]))); |
---|
923 | printf("Changed: %s\n", argv[M_STAT_CHNG]); |
---|
924 | printf("Use data: %s\n", argv[M_INUSE]); |
---|
925 | printf("Vendor: %s\n", argv[M_VENDOR]); |
---|
926 | printf("Model: %s\n", argv[M_MODEL]); |
---|
927 | printf("OS: %s\n", argv[M_OS]); |
---|
928 | printf("Location: %s\n", argv[M_LOC]); |
---|
929 | printf("Contact: %s\n", argv[M_CONTACT]); |
---|
930 | printf("Billing Contact: %s\n", argv[M_BILL_CONTACT]); |
---|
931 | printf("Account Number: %s\n", argv[M_ACCT_NUMBER]); |
---|
932 | printf("Opt: %s\n", argv[M_USE]); |
---|
933 | printf("Adm cmt: %s\n", argv[M_ACOMMENT]); |
---|
934 | printf("Op cmt: %s\n", argv[M_OCOMMENT]); |
---|
935 | printf("Created by: %s\n", argv[M_CREATOR]); |
---|
936 | printf("Created on: %s\n", argv[M_CREATED]); |
---|
937 | printf("Last mod by: %s\n", argv[M_MODBY]); |
---|
938 | printf("Last mod on: %s\n", argv[M_MODTIME]); |
---|
939 | printf("Last mod with: %s\n", argv[M_MODWITH]); |
---|
940 | } |
---|
941 | |
---|
942 | int show_machine_in_cluster(int argc, char **argv, void *hint) |
---|
943 | { |
---|
944 | printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]); |
---|
945 | |
---|
946 | return MR_CONT; |
---|
947 | } |
---|
948 | |
---|
949 | int show_machine_in_container(int argc, char **argv, void *hint) |
---|
950 | { |
---|
951 | printf("Machine: %-30s Container: %-25s\n", argv[0], argv[1]); |
---|
952 | |
---|
953 | return MR_CONT; |
---|
954 | } |
---|
955 | |
---|
956 | /* Parse a line of input, fetching a member. NULL is returned if a member |
---|
957 | * is not found. ';' is a comment character. |
---|
958 | */ |
---|
959 | |
---|
960 | struct owner_type *parse_member(char *s) |
---|
961 | { |
---|
962 | struct owner_type *m; |
---|
963 | char *p, *lastchar; |
---|
964 | |
---|
965 | while (*s && isspace(*s)) |
---|
966 | s++; |
---|
967 | lastchar = p = s; |
---|
968 | while (*p && *p != '\n' && *p != ';') |
---|
969 | { |
---|
970 | if (isprint(*p) && !isspace(*p)) |
---|
971 | lastchar = p++; |
---|
972 | else |
---|
973 | p++; |
---|
974 | } |
---|
975 | lastchar++; |
---|
976 | *lastchar = '\0'; |
---|
977 | if (p == s || strlen(s) == 0) |
---|
978 | return NULL; |
---|
979 | |
---|
980 | if (!(m = malloc(sizeof(struct owner_type)))) |
---|
981 | return NULL; |
---|
982 | |
---|
983 | if ((p = strchr(s, ':'))) |
---|
984 | { |
---|
985 | *p = '\0'; |
---|
986 | m->name = ++p; |
---|
987 | if (!strcasecmp("user", s)) |
---|
988 | m->type = M_USER; |
---|
989 | else if (!strcasecmp("list", s)) |
---|
990 | m->type = M_LIST; |
---|
991 | else if (!strcasecmp("kerberos", s)) |
---|
992 | m->type = M_KERBEROS; |
---|
993 | else if (!strcasecmp("none", s)) |
---|
994 | m->type = M_NONE; |
---|
995 | else |
---|
996 | { |
---|
997 | m->type = M_ANY; |
---|
998 | *(--p) = ':'; |
---|
999 | m->name = s; |
---|
1000 | } |
---|
1001 | m->name = strdup(m->name); |
---|
1002 | } |
---|
1003 | else |
---|
1004 | { |
---|
1005 | m->name = strdup(s); |
---|
1006 | m->type = strcasecmp(s, "none") ? M_ANY : M_NONE; |
---|
1007 | } |
---|
1008 | return m; |
---|
1009 | } |
---|
1010 | |
---|
1011 | struct string_list *add_to_string_list(struct string_list *old_list, char *s) { |
---|
1012 | struct string_list *new_list; |
---|
1013 | |
---|
1014 | new_list = (struct string_list *)malloc(sizeof(struct string_list *)); |
---|
1015 | new_list->next = old_list; |
---|
1016 | new_list->string = s; |
---|
1017 | |
---|
1018 | return new_list; |
---|
1019 | } |
---|
1020 | |
---|
1021 | int wrap_mr_query(char *handle, int argc, char **argv, |
---|
1022 | int (*callback)(int, char **, void *), void *callarg) { |
---|
1023 | if (verbose) |
---|
1024 | print_query(handle, argc, argv); |
---|
1025 | |
---|
1026 | return mr_query(handle, argc, argv, callback, callarg); |
---|
1027 | } |
---|
1028 | |
---|
1029 | void print_query(char *query_name, int argc, char **argv) { |
---|
1030 | int cnt; |
---|
1031 | |
---|
1032 | printf("qy %s", query_name); |
---|
1033 | for(cnt=0; cnt<argc; cnt++) |
---|
1034 | printf(" <%s>", argv[cnt]); |
---|
1035 | printf("\n"); |
---|
1036 | } |
---|