client_common.c: Kill invalid FIXME.
[paraslash.git] / client_common.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file client_common.c common functions of para_client and para_audiod */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "client.cmdline.h"
17 #include "crypt.h"
18 #include "rc4.h"
19 #include "net.h"
20 #include "fd.h"
21 #include "string.h"
22 #include "client.cmdline.h"
23 #include "client.h"
24
25 /*
26 * rc4 encrypt data before sending
27 *
28 * \param len the number of bytes to encrypt
29 * \param indata pointer to the input data of length \a len to be encrypted
30 * \param outdata pointer that holds the encrypted data after return
31 * \param private_data pointer to the private client data containing
32 * the rc4 key
33 * */
34 static void rc4_send(unsigned long len, const unsigned char *indata,
35 unsigned char *outdata, void *private_data)
36 {
37 struct private_client_data *pcd = private_data;
38 RC4(&pcd->rc4_send_key, len, indata, outdata);
39 }
40
41 /*
42 * rc4 decrypt received data
43 *
44 * \param len the number of bytes to decrypt
45 * \param indata pointer to the input data of length \a len
46 * \param outdata pointer that holds the decrypted data after return
47 * \param private_data pointer to the private client data containing
48 * the rc4 key
49 * */
50 static void rc4_recv(unsigned long len, const unsigned char *indata,
51 unsigned char *outdata, void *private_data)
52 {
53 struct private_client_data *pcd = private_data;
54 RC4(&pcd->rc4_recv_key, len, indata, outdata);
55 }
56
57 /**
58 * close the connection to para_server and free all resources
59 *
60 * \param pcd pointer to the client data
61 *
62 * \sa client_open.
63 * */
64 void client_close(struct private_client_data *pcd)
65 {
66 if (!pcd)
67 return;
68 if (pcd->fd >= 0) {
69 disable_crypt(pcd->fd);
70 close(pcd->fd);
71 }
72 free(pcd->user);
73 free(pcd->config_file);
74 free(pcd->key_file);
75 client_cmdline_parser_free(&pcd->conf);
76 free(pcd);
77 }
78
79 /* connect to para_server and register the client task */
80 static int client_connect(struct private_client_data *pcd)
81 {
82 int ret;
83
84 pcd->fd = -1;
85 ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, pcd->conf.hostname_arg,
86 pcd->conf.server_port_arg);
87 if (ret < 0)
88 return ret;
89 pcd->fd = ret;
90 pcd->status = CL_CONNECTED;
91 ret = mark_fd_nonblocking(pcd->fd);
92 if (ret < 0)
93 goto err_out;
94 pcd->task.pre_select = client_pre_select;
95 pcd->task.post_select = client_post_select;
96 pcd->task.private_data = pcd;
97 sprintf(pcd->task.status, "client");
98 register_task(&pcd->task);
99 return 1;
100 err_out:
101 close(pcd->fd);
102 pcd->fd = -1;
103 return ret;
104 }
105
106 /**
107 * open connection to para_server
108 *
109 * \param argc usual argument count
110 * \param argv usual argument vector
111 * \param pcd_ptr points to dynamically allocated and initialized private client data
112 * upon successful return
113 *
114 * Check the command line options given by \a argc and argv, set default values
115 * for user name and rsa key file, read further option from the config file.
116 * Finally, establish a connection to para_server.
117 *
118 * \return Positive on success, negative on errors.
119 */
120 int client_open(int argc, char *argv[], struct private_client_data **pcd_ptr)
121 {
122 char *home = para_homedir();
123 struct stat statbuf;
124 int ret;
125 struct private_client_data *pcd =
126 para_calloc(sizeof(struct private_client_data));
127
128 *pcd_ptr = pcd;
129 pcd->fd = -1;
130 ret = client_cmdline_parser(argc, argv, &pcd->conf);
131 HANDLE_VERSION_FLAG("client", pcd->conf);
132 ret = -E_CLIENT_SYNTAX;
133 if (!pcd->conf.inputs_num)
134 goto out;
135 pcd->user = pcd->conf.user_given?
136 para_strdup(pcd->conf.user_arg) : para_logname();
137
138 pcd->key_file = pcd->conf.key_file_given?
139 para_strdup(pcd->conf.key_file_arg) :
140 make_message("%s/.paraslash/key.%s", home, pcd->user);
141
142 pcd->config_file = pcd->conf.config_file_given?
143 para_strdup(pcd->conf.config_file_arg) :
144 make_message("%s/.paraslash/client.conf", home);
145 ret = stat(pcd->config_file, &statbuf);
146 if (ret && pcd->conf.config_file_given) {
147 ret = -E_NO_CONFIG;
148 goto out;
149 }
150 if (!ret) {
151 struct client_cmdline_parser_params params = {
152 .override = 0,
153 .initialize = 0,
154 .check_required = 0,
155 .check_ambiguity = 0
156 };
157 client_cmdline_parser_config_file(pcd->config_file,
158 &pcd->conf, &params);
159 }
160 ret = 1;
161 PARA_INFO_LOG("loglevel: %d\n", pcd->conf.loglevel_arg);
162 PARA_INFO_LOG("config_file: %s\n", pcd->config_file);
163 PARA_INFO_LOG("key_file: %s\n", pcd->key_file);
164 PARA_NOTICE_LOG("connecting %s:%d\n", pcd->conf.hostname_arg,
165 pcd->conf.server_port_arg);
166 ret = client_connect(pcd);
167 out:
168 free(home);
169 if (ret < 0) {
170 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
171 client_close(pcd);
172 *pcd_ptr = NULL;
173 }
174 return ret;
175 }
176
177 /**
178 * the preselect hook for server commands
179 *
180 * \param s pointer to the scheduler
181 * \param t pointer to the task struct for this command
182 *
183 * The task pointer must contain a pointer to the initialized client data
184 * structure as it is returned by client_open().
185 *
186 * This function checks the state of the connection and adds the file descriptor
187 * of the connection to the read or write fd set of \a s accordingly.
188 *
189 * \sa register_task() client_open(), struct sched, struct task
190 */
191 void client_pre_select(struct sched *s, struct task *t)
192 {
193 struct private_client_data *pcd = t->private_data;
194
195 t->ret = 1;
196 pcd->check_r = 0;
197 pcd->check_w = 0;
198 if (pcd->fd < 0)
199 return;
200 switch (pcd->status) {
201 case CL_CONNECTED:
202 case CL_SENT_AUTH:
203 case CL_SENT_CH_RESPONSE:
204 case CL_SENT_COMMAND:
205 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
206 pcd->check_r = 1;
207 return;
208
209 case CL_RECEIVED_WELCOME:
210 case CL_RECEIVED_CHALLENGE:
211 case CL_RECEIVED_PROCEED:
212 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
213 pcd->check_w = 1;
214 return;
215
216 case CL_RECEIVING:
217 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
218 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
219 pcd->check_r = 1;
220 }
221 return;
222 case CL_SENDING:
223 if (*pcd->in_loaded) {
224 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
225 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
226 pcd->check_w = 1;
227 } else {
228 if (*pcd->in_error) {
229 t->ret = *pcd->in_error;
230 s->timeout.tv_sec = 0;
231 s->timeout.tv_usec = 1;
232 }
233 }
234 return;
235 }
236 }
237
238 static ssize_t client_recv_buffer(struct private_client_data *pcd)
239 {
240 ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
241 CLIENT_BUFSIZE - pcd->loaded);
242 if (!ret)
243 return -E_SERVER_EOF;
244 if (ret > 0)
245 pcd->loaded += ret;
246 return ret;
247
248 }
249
250 /**
251 * the post select hook for client commands
252 *
253 * \param s pointer to the scheduler
254 * \param t pointer to the task struct for this command
255 *
256 * Depending on the current state of the connection and the status of the read
257 * and write fd sets of \a s, this function performs the necessary steps to
258 * authenticate the connection, to send the command given by \a
259 * t->private_data and to receive para_server's output, if any.
260 *
261 * \sa struct sched, struct task
262 */
263 void client_post_select(struct sched *s, struct task *t)
264 {
265 struct private_client_data *pcd = t->private_data;
266
267 // PARA_INFO_LOG("status %d\n", pcd->status);
268 t->ret = 1;
269 if (pcd->fd < 0)
270 return;
271 if (!pcd->check_r && !pcd->check_w)
272 return;
273 if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
274 return;
275 if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
276 return;
277 switch (pcd->status) {
278 case CL_CONNECTED: /* receive welcome message */
279 t->ret = client_recv_buffer(pcd);
280 if (t->ret > 0)
281 pcd->status = CL_RECEIVED_WELCOME;
282 return;
283 case CL_RECEIVED_WELCOME: /* send auth command */
284 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
285 "" : "rc4 ", pcd->user);
286 PARA_INFO_LOG("--> %s\n", pcd->buf);
287 t->ret = send_buffer(pcd->fd, pcd->buf);
288 if (t->ret >= 0)
289 pcd->status = CL_SENT_AUTH;
290 return;
291 case CL_SENT_AUTH: /* receive challenge number */
292 pcd->loaded = 0;
293 t->ret = client_recv_buffer(pcd);
294 if (t->ret < 0)
295 return;
296 if (t->ret != 64) {
297 t->ret = -E_INVALID_CHALLENGE;
298 PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
299 return;
300 }
301 PARA_INFO_LOG("%s", "<-- [challenge]\n");
302 /* decrypt challenge number */
303 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
304 (unsigned char *) pcd->buf, 64);
305 if (t->ret > 0)
306 pcd->status = CL_RECEIVED_CHALLENGE;
307 return;
308 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
309 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
310 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
311 pcd->challenge_nr);
312 if (t->ret > 0)
313 pcd->status = CL_SENT_CH_RESPONSE;
314 return;
315 case CL_SENT_CH_RESPONSE: /* read server response */
316 {
317 size_t bytes_received;
318 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
319 pcd->loaded = 0;
320 t->ret = client_recv_buffer(pcd);
321 if (t->ret < 0)
322 return;
323 bytes_received = t->ret;
324 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
325 "info ++++\n", pcd->buf);
326 /* check if server has sent "Proceed" message */
327 t->ret = -E_CLIENT_AUTH;
328 if (!strstr(pcd->buf, PROCEED_MSG))
329 return;
330 t->ret = 1;
331 pcd->status = CL_RECEIVED_PROCEED;
332 if (bytes_received < PROCEED_MSG_LEN + 32)
333 return;
334 PARA_INFO_LOG("%s", "decrypting session key\n");
335 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
336 (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
337 bytes_received - PROCEED_MSG_LEN - 1);
338 if (t->ret < 0)
339 return;
340 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
341 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
342 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
343 }
344 case CL_RECEIVED_PROCEED: /* concat args and send command */
345 {
346 int i;
347 char *command = NULL;
348 for (i = 0; i < pcd->conf.inputs_num; i++) {
349 char *tmp = command;
350 command = make_message("%s\n%s", command?
351 command : "", pcd->conf.inputs[i]);
352 free(tmp);
353 }
354 command = para_strcat(command, EOC_MSG "\n");
355 PARA_DEBUG_LOG("--> %s\n", command);
356 t->ret = send_buffer(pcd->fd, command);
357 free(command);
358 if (t->ret > 0)
359 pcd->status = CL_SENT_COMMAND;
360 return;
361 }
362 case CL_SENT_COMMAND:
363 pcd->loaded = 0;
364 t->ret = client_recv_buffer(pcd);
365 if (t->ret < 0)
366 return;
367 t->ret = -E_HANDSHAKE_COMPLETE;
368 if (strstr(pcd->buf, AWAITING_DATA_MSG))
369 pcd->status = CL_SENDING;
370 else
371 pcd->status = CL_RECEIVING;
372 return;
373 case CL_SENDING: /* FIXME: might block */
374 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
375 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
376 if (t->ret < 0)
377 return;
378 *pcd->in_loaded = 0;
379 return;
380 case CL_RECEIVING:
381 t->ret = client_recv_buffer(pcd);
382 return;
383 }
384 }