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