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