1 | /* |
---|
2 | * Test suite for malformed commands. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2007, 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * |
---|
7 | * See LICENSE for licensing terms. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <config.h> |
---|
11 | #include <portable/system.h> |
---|
12 | |
---|
13 | #include <signal.h> |
---|
14 | #include <sys/wait.h> |
---|
15 | |
---|
16 | #include <client/internal.h> |
---|
17 | #include <client/remctl.h> |
---|
18 | #include <tests/tap/basic.h> |
---|
19 | #include <tests/tap/kerberos.h> |
---|
20 | #include <tests/tap/remctl.h> |
---|
21 | #include <util/util.h> |
---|
22 | |
---|
23 | |
---|
24 | /* |
---|
25 | * Send an invalid token to the given remctl connection and verify that it |
---|
26 | * returns the specified error code and string. |
---|
27 | */ |
---|
28 | static void |
---|
29 | test_bad_token(const char *principal, const char *data, size_t length, |
---|
30 | enum error_codes code, const char *message, |
---|
31 | const char *description) |
---|
32 | { |
---|
33 | struct remctl *r; |
---|
34 | struct remctl_output *output; |
---|
35 | gss_buffer_desc token; |
---|
36 | OM_uint32 major, minor; |
---|
37 | int status; |
---|
38 | |
---|
39 | /* |
---|
40 | * We have to open a new connection for every test since some errors close |
---|
41 | * the connection. |
---|
42 | */ |
---|
43 | r = remctl_new(); |
---|
44 | ok(r != NULL, "remctl_new"); |
---|
45 | ok(remctl_open(r, "localhost", 14373, principal), "remctl_open"); |
---|
46 | token.value = (void *) data; |
---|
47 | token.length = length; |
---|
48 | status = token_send_priv(r->fd, r->context, TOKEN_DATA | TOKEN_PROTOCOL, |
---|
49 | &token, &major, &minor); |
---|
50 | is_int(TOKEN_OK, status, "sent token for %s", description); |
---|
51 | if (status != TOKEN_OK) { |
---|
52 | remctl_close(r); |
---|
53 | ok_block(5, 0, "testing for %s", description); |
---|
54 | } |
---|
55 | r->ready = 1; |
---|
56 | output = remctl_output(r); |
---|
57 | ok(output != NULL, "output is not null"); |
---|
58 | if (output == NULL) { |
---|
59 | remctl_close(r); |
---|
60 | ok_block(4, 0, "testing for %s", description); |
---|
61 | } |
---|
62 | is_int(REMCTL_OUT_ERROR, output->type, "error type"); |
---|
63 | is_int(code, output->error, "right error code for %s", description); |
---|
64 | is_int(strlen(message), output->length, "right length for %s", |
---|
65 | description); |
---|
66 | if (output->length <= strlen(message)) |
---|
67 | ok(memcmp(output->data, message, output->length) == 0, |
---|
68 | "right data for %s", description); |
---|
69 | else |
---|
70 | ok(0, "right data for %s", description); |
---|
71 | remctl_close(r); |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /* |
---|
76 | * Send a chunk of data to the given remctl connection and verify that it |
---|
77 | * returns a bad command error token. Returns the next test number. |
---|
78 | */ |
---|
79 | static void |
---|
80 | test_bad_command(const char *principal, const char *data, size_t length, |
---|
81 | const char *description) |
---|
82 | { |
---|
83 | char buffer[BUFSIZ]; |
---|
84 | size_t buflen; |
---|
85 | static const char prefix[] = { 2, MESSAGE_COMMAND, 1, 0 }; |
---|
86 | |
---|
87 | memcpy(buffer, prefix, sizeof(prefix)); |
---|
88 | memcpy(buffer + sizeof(prefix), data, length); |
---|
89 | buflen = sizeof(prefix) + length; |
---|
90 | return test_bad_token(principal, buffer, buflen, ERROR_BAD_COMMAND, |
---|
91 | "Invalid command token", description); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | int |
---|
96 | main(void) |
---|
97 | { |
---|
98 | char *principal, *config, *path; |
---|
99 | pid_t remctld; |
---|
100 | static const char token_message[] = { |
---|
101 | 2, 47 |
---|
102 | }; |
---|
103 | static const char token_continue[] = { |
---|
104 | 2, MESSAGE_COMMAND, 1, 4, |
---|
105 | 0, 0, 0, 2, |
---|
106 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
107 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', 's' |
---|
108 | }; |
---|
109 | static const char token_argv0[] = { |
---|
110 | 2, MESSAGE_COMMAND, 1, 0, |
---|
111 | 0, 0, 0, 0 |
---|
112 | }; |
---|
113 | static const char data_trunc[] = { |
---|
114 | 0, 0, 0, 2, |
---|
115 | 0, 0, 0, 1, 't' |
---|
116 | }; |
---|
117 | static const char data_trunc_arg[] = { |
---|
118 | 0, 0, 0, 2, |
---|
119 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
120 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u' |
---|
121 | }; |
---|
122 | static const char data_short[] = { |
---|
123 | 0, 0, 0, 3, |
---|
124 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
125 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', 's' |
---|
126 | }; |
---|
127 | static const char data_long[] = { |
---|
128 | 0, 0, 0, 2, |
---|
129 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
130 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', 's', |
---|
131 | 0, 0, 0, 1, '2' |
---|
132 | }; |
---|
133 | static const char data_extra[] = { |
---|
134 | 0, 0, 0, 2, |
---|
135 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
136 | 0, 0, 0, 5, 's', 't', 'a', 't', 'u', 's' |
---|
137 | }; |
---|
138 | static const char data_nul_command[] = { |
---|
139 | 0, 0, 0, 2, |
---|
140 | 0, 0, 0, 4, 't', '\0','s', 't', |
---|
141 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', 's' |
---|
142 | }; |
---|
143 | static const char data_nul_sub[] = { |
---|
144 | 0, 0, 0, 2, |
---|
145 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
146 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', '\0' |
---|
147 | }; |
---|
148 | static const char data_nul_argument[] = { |
---|
149 | 0, 0, 0, 3, |
---|
150 | 0, 0, 0, 4, 't', 'e', 's', 't', |
---|
151 | 0, 0, 0, 6, 's', 't', 'a', 't', 'u', 's', |
---|
152 | 0, 0, 0, 1, '\0' |
---|
153 | }; |
---|
154 | |
---|
155 | /* Unless we have Kerberos available, we can't really do anything. */ |
---|
156 | if (chdir(getenv("SOURCE")) < 0) |
---|
157 | bail("can't chdir to SOURCE"); |
---|
158 | principal = kerberos_setup(); |
---|
159 | if (principal == NULL) |
---|
160 | skip_all("Kerberos tests not configured"); |
---|
161 | plan(11 * 8); |
---|
162 | config = concatpath(getenv("SOURCE"), "data/conf-simple"); |
---|
163 | path = concatpath(getenv("BUILD"), "../server/remctld"); |
---|
164 | remctld = remctld_start(path, principal, config); |
---|
165 | |
---|
166 | /* Test basic token errors. */ |
---|
167 | test_bad_token(principal, token_message, sizeof(token_message), |
---|
168 | ERROR_UNKNOWN_MESSAGE, "Unknown message", |
---|
169 | "unknown message"); |
---|
170 | test_bad_token(principal, token_continue, sizeof(token_continue), |
---|
171 | ERROR_BAD_COMMAND, "Invalid command token", |
---|
172 | "bad command token"); |
---|
173 | test_bad_token(principal, token_argv0, sizeof(token_argv0), |
---|
174 | ERROR_UNKNOWN_COMMAND, "Unknown command", |
---|
175 | "empty command"); |
---|
176 | |
---|
177 | /* Test a bunch of malformatted commands. */ |
---|
178 | test_bad_command(principal, data_trunc, sizeof(data_trunc), |
---|
179 | "truncated command"); |
---|
180 | test_bad_command(principal, data_trunc_arg, sizeof(data_trunc_arg), |
---|
181 | "truncated argument"); |
---|
182 | test_bad_command(principal, data_short, sizeof(data_short), |
---|
183 | "missing argument"); |
---|
184 | test_bad_command(principal, data_long, sizeof(data_long), |
---|
185 | "extra argument"); |
---|
186 | test_bad_command(principal, data_extra, sizeof(data_extra), |
---|
187 | "extra trailing garbage"); |
---|
188 | test_bad_command(principal, data_nul_command, sizeof(data_nul_command), |
---|
189 | "nul in command"); |
---|
190 | test_bad_command(principal, data_nul_sub, sizeof(data_nul_sub), |
---|
191 | "nul in subcommand"); |
---|
192 | test_bad_command(principal, data_nul_argument, sizeof(data_nul_argument), |
---|
193 | "nul in argument"); |
---|
194 | |
---|
195 | remctld_stop(remctld); |
---|
196 | kerberos_cleanup(); |
---|
197 | return 0; |
---|
198 | } |
---|