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