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