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