]>
Commit | Line | Data |
---|---|---|
1 | /* $Id$ | |
2 | * | |
3 | * Test client for update_server protocol. | |
4 | * | |
5 | * Copyright 1992-1998 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <moira.h> | |
12 | #include <update.h> | |
13 | ||
14 | #include <errno.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | ||
18 | void usage(void); | |
19 | ||
20 | char *whoami; | |
21 | ||
22 | int main(int argc, char **argv) | |
23 | { | |
24 | char *host, *file, *rfile, *ibuf = NULL; | |
25 | int code, i, count = 0, conn; | |
26 | ||
27 | whoami = argv[0]; | |
28 | mr_init(); | |
29 | ||
30 | if (argc < 2) | |
31 | usage(); | |
32 | host = argv[1]; | |
33 | ||
34 | conn = mr_connect_internal(host, SERVICE_NAME); | |
35 | if (!conn) | |
36 | { | |
37 | com_err(whoami, errno, ": can't connect to %s:%s", host, SERVICE_NAME); | |
38 | exit(1); | |
39 | } | |
40 | ||
41 | code = send_auth(conn, host); | |
42 | if (code) | |
43 | com_err(whoami, code, "attempting authorization"); | |
44 | ||
45 | for (i = 2; i < argc; i++) | |
46 | { | |
47 | if (argv[i][0] != '-') | |
48 | usage(); | |
49 | switch (argv[i][1]) | |
50 | { | |
51 | case 's': | |
52 | if (i + 2 >= argc) | |
53 | usage(); | |
54 | file = argv[++i]; | |
55 | rfile = argv[++i]; | |
56 | fprintf(stderr, "Sending file %s to %s as %s\n", file, host, rfile); | |
57 | send_file(conn, file, rfile, 0); | |
58 | break; | |
59 | case 'S': | |
60 | if (i + 2 >= argc) | |
61 | usage(); | |
62 | file = argv[++i]; | |
63 | rfile = argv[++i]; | |
64 | fprintf(stderr, "Sending (encrypted) file %s to %s as %s\n", | |
65 | file, host, rfile); | |
66 | send_file(conn, file, rfile, 1); | |
67 | break; | |
68 | case 'i': | |
69 | if (i + 1 >= argc) | |
70 | usage(); | |
71 | file = argv[++i]; | |
72 | ibuf = strdup("/tmp/moira-updateXXXXX"); | |
73 | if (!ibuf) | |
74 | { | |
75 | com_err(whoami, ENOMEM, "sending instructions"); | |
76 | exit(1); | |
77 | } | |
78 | mktemp(ibuf); | |
79 | fprintf(stderr, "Sending instructions %s to %s as %s\n", | |
80 | file, host, ibuf); | |
81 | send_file(conn, file, ibuf, 0); | |
82 | break; | |
83 | case 'I': | |
84 | if (i + 2 >= argc) | |
85 | usage(); | |
86 | file = argv[++i]; | |
87 | ibuf = argv[++i]; | |
88 | strcpy(ibuf, rfile); | |
89 | fprintf(stderr, "Sending instructions %s to %s as %s\n", | |
90 | file, host, ibuf); | |
91 | send_file(conn, file, ibuf, 0); | |
92 | break; | |
93 | case 'x': | |
94 | if (!ibuf) | |
95 | { | |
96 | fprintf(stderr, "No instructions sent."); | |
97 | usage(); | |
98 | } | |
99 | fprintf(stderr, "Executing instructions %s on %s\n", ibuf, host); | |
100 | code = execute(conn, ibuf); | |
101 | if (code) | |
102 | com_err(whoami, code, "executing"); | |
103 | break; | |
104 | case 'X': | |
105 | if (i + 1 >= argc) | |
106 | usage(); | |
107 | file = argv[++i]; | |
108 | fprintf(stderr, "Executing instructions %s on %s\n", file, host); | |
109 | code = execute(conn, file); | |
110 | if (code) | |
111 | com_err(whoami, code, "executing"); | |
112 | break; | |
113 | case 'n': | |
114 | break; | |
115 | default: | |
116 | usage(); | |
117 | } | |
118 | } | |
119 | send_quit(conn); | |
120 | close(conn); | |
121 | exit(code); | |
122 | } | |
123 | ||
124 | void usage(void) | |
125 | { | |
126 | fprintf(stderr, "Usage: test host [commands...]\n"); | |
127 | fprintf(stderr, " Commands are:\n"); | |
128 | fprintf(stderr, "\t-s srcfile dstfile\tsends file\n"); | |
129 | fprintf(stderr, "\t-S srcfile dstfile\tsends encrypted file\n"); | |
130 | fprintf(stderr, "\t-i srcfile\t\tsends instructions\n"); | |
131 | fprintf(stderr, "\t-I srcfile dstfile\tsends instructions\n"); | |
132 | fprintf(stderr, "\t-x\t\texecutes last instructions\n"); | |
133 | fprintf(stderr, "\t-X file\t\texecutes file\n"); | |
134 | exit(1); | |
135 | } |