the paraslash-0.2.13 release tarball
[paraslash.git] / client_common.c
1 /*
2  * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file client_common.c common functions of para_client and para_audiod */
20
21 #include "para.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "client.cmdline.h"
25 #include "crypt.h"
26 #include "rc4.h"
27 #include "net.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "client.cmdline.h"
31 #include "client.h"
32 #include "error.h"
33
34 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 void rc4_recv(unsigned long len, const unsigned char *indata,
42                 unsigned char *outdata, void *private_data)
43 {
44         struct private_client_data *pcd = private_data;
45         RC4(&pcd->rc4_recv_key, len, indata, outdata);
46 }
47
48
49 void client_close(struct private_client_data *pcd)
50 {
51         if (pcd)
52                 return;
53         if (pcd->fd >= 0) {
54                 disable_crypt(pcd->fd);
55                 close(pcd->fd);
56         }
57         free(pcd->user);
58         free(pcd->config_file);
59         free(pcd->key_file);
60         free(pcd);
61 }
62
63 int client_parse_config(int argc, char *argv[],
64                 struct private_client_data **pcd_ptr)
65 {
66         char *home = para_homedir();
67         struct stat statbuf;
68         int ret;
69         struct private_client_data *pcd =
70                 para_calloc(sizeof(struct private_client_data));
71
72         pcd->fd = -1;
73         client_cmdline_parser(argc, argv, &pcd->conf);
74         ret = - E_CLIENT_SYNTAX;
75         if (!pcd->conf.inputs_num)
76                 goto out;
77         pcd->user = pcd->conf.user_given?
78                 para_strdup(pcd->conf.user_arg) : para_logname();
79
80         pcd->key_file = pcd->conf.key_file_given?
81                 para_strdup(pcd->conf.key_file_arg) :
82                 make_message("%s/.paraslash/key.%s", home, pcd->user);
83
84         pcd->config_file = pcd->conf.config_file_given?
85                 para_strdup(pcd->conf.config_file_arg) :
86                 make_message("%s/.paraslash/client.conf", home);
87         ret = stat(pcd->config_file, &statbuf);
88         if (ret && pcd->conf.config_file_given) {
89                 ret = -E_NO_CONFIG;
90                 goto out;
91         }
92         if (!ret)
93                 client_cmdline_parser_configfile(pcd->config_file,
94                         &pcd->conf, 0, 0, 0);
95         ret = 1;
96         *pcd_ptr = pcd;
97         PARA_INFO_LOG("loglevel: %d\n", pcd->conf.loglevel_arg);
98         PARA_INFO_LOG("config_file: %s\n", pcd->config_file);
99         PARA_INFO_LOG("key_file: %s\n", pcd->key_file);
100         PARA_NOTICE_LOG("connecting %s:%d\n", pcd->conf.hostname_arg,
101                 pcd->conf.server_port_arg);
102 out:
103         free(home);
104         if (ret < 0)
105                 client_close(pcd);
106         return ret;
107 }
108
109 void client_pre_select(struct sched *s, struct task *t)
110 {
111         struct private_client_data *pcd = t->private_data;
112
113         t->ret = 1;
114         pcd->check_r = 0;
115         pcd->check_w = 0;
116         if (pcd->fd < 0)
117                 return;
118         switch (pcd->status) {
119         case CL_CONNECTED:
120         case CL_SENT_AUTH:
121         case CL_SENT_CH_RESPONSE:
122         case CL_SENT_COMMAND:
123                 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
124                 pcd->check_r = 1;
125                 return;
126
127         case CL_RECEIVED_WELCOME:
128         case CL_RECEIVED_CHALLENGE:
129         case CL_RECEIVED_PROCEED:
130                 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
131                 pcd->check_w = 1;
132                 return;
133
134         case CL_RECEIVING:
135                 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
136                         para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
137                         pcd->check_r = 1;
138                 }
139                 return;
140         case CL_SENDING:
141                 if (*pcd->in_loaded) {
142                         PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
143                         para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
144                         pcd->check_w = 1;
145                 } else {
146                         if (*pcd->in_eof) {
147                                 t->ret = -E_INPUT_EOF;
148                                 s->timeout.tv_sec = 0;
149                                 s->timeout.tv_usec = 1;
150                         }
151                 }
152                 return;
153         }
154 }
155
156 static ssize_t client_recv_buffer(struct private_client_data *pcd)
157 {
158         ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
159                 CLIENT_BUFSIZE - pcd->loaded);
160         if (!ret)
161                 return -E_SERVER_EOF;
162         if (ret > 0)
163                 pcd->loaded += ret;
164         return ret;
165
166 }
167
168 void client_post_select(struct sched *s, struct task *t)
169 {
170         struct private_client_data *pcd = t->private_data;
171
172 //      PARA_INFO_LOG("status %d\n", pcd->status);
173         t->ret = 1;
174         if (pcd->fd < 0)
175                 return;
176         if (!pcd->check_r && !pcd->check_w)
177                 return;
178         if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
179                 return;
180         if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
181                 return;
182         switch (pcd->status) {
183         case CL_CONNECTED: /* receive welcome message */
184                 t->ret = client_recv_buffer(pcd);
185                 if (t->ret > 0)
186                         pcd->status = CL_RECEIVED_WELCOME;
187                 return;
188         case CL_RECEIVED_WELCOME: /* send auth command */
189                 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
190                         "" : "rc4 ", pcd->user);
191                 PARA_INFO_LOG("--> %s\n", pcd->buf);
192                 t->ret = send_buffer(pcd->fd, pcd->buf);
193                 if (t->ret >= 0)
194                         pcd->status = CL_SENT_AUTH;
195                 return;
196         case CL_SENT_AUTH: /* receive challenge number */
197                 pcd->loaded = 0;
198                 t->ret = client_recv_buffer(pcd);
199                 if (t->ret < 0)
200                         return;
201                 if (t->ret != 64) {
202                         t->ret = -E_INVALID_CHALLENGE;
203                         PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
204                         return;
205                 }
206                 PARA_INFO_LOG("%s", "<-- [challenge]\n");
207                 /* decrypt challenge number */
208                 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
209                         (unsigned char *) pcd->buf, 64);
210                 if (t->ret > 0)
211                         pcd->status = CL_RECEIVED_CHALLENGE;
212                 return;
213         case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
214                 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
215                 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
216                         pcd->challenge_nr);
217                 if (t->ret > 0)
218                         pcd->status = CL_SENT_CH_RESPONSE;
219                 return;
220         case CL_SENT_CH_RESPONSE: /* read server response */
221                 {
222                 size_t bytes_received;
223                 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
224                 pcd->loaded = 0;
225                 t->ret = client_recv_buffer(pcd);
226                 if (t->ret < 0)
227                         return;
228                 bytes_received = t->ret;
229                 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
230                         "info ++++\n", pcd->buf);
231                 /* check if server has sent "Proceed" message */
232                 t->ret = -E_CLIENT_AUTH;
233                 if (!strstr(pcd->buf, PROCEED_MSG))
234                         return;
235                 t->ret = 1;
236                 pcd->status = CL_RECEIVED_PROCEED;
237                 if (bytes_received < PROCEED_MSG_LEN + 32)
238                         return;
239                 PARA_INFO_LOG("%s", "decrypting session key\n");
240                 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
241                         (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
242                         bytes_received - PROCEED_MSG_LEN - 1);
243                 if (t->ret < 0)
244                         return;
245                 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
246                 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
247                 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
248                 }
249         case CL_RECEIVED_PROCEED: /* concat args and send command */
250                 {
251                 int i;
252                 char *command = NULL;
253                 for (i = 0; i < pcd->conf.inputs_num; i++) {
254                         char *tmp = command;
255                         command = make_message("%s\n%s", command?
256                                 command : "", pcd->conf.inputs[i]);
257                         free(tmp);
258                 }
259                 command = para_strcat(command, EOC_MSG "\n");
260                 PARA_DEBUG_LOG("--> %s\n", command);
261                 t->ret = send_buffer(pcd->fd, command);
262                 free(command);
263                 if (t->ret > 0)
264                         pcd->status = CL_SENT_COMMAND;
265                 return;
266                 }
267         case CL_SENT_COMMAND:
268                 pcd->loaded = 0;
269                 t->ret = client_recv_buffer(pcd);
270                 if (t->ret < 0)
271                         return;
272                 t->ret = -E_HANDSHAKE_COMPLETE;
273                 if (strstr(pcd->buf, AWAITING_DATA_MSG))
274                         pcd->status = CL_SENDING;
275                 else
276                         pcd->status = CL_RECEIVING;
277                 return;
278         case CL_SENDING: /* FIXME: might block */
279                 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
280                 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
281                 if (t->ret < 0)
282                         return;
283                 *pcd->in_loaded = 0; /* FIXME: short writes */
284                 return;
285         case CL_RECEIVING:
286                 t->ret = client_recv_buffer(pcd);
287                 return;
288         }
289
290 }
291
292 int client_open(struct private_client_data *pcd)
293 {
294         int ret;
295         struct hostent *he;
296         struct sockaddr_in their_addr;
297
298         pcd->fd = -1;
299         ret = get_host_info(pcd->conf.hostname_arg, &he);
300         if (ret < 0)
301                 goto err_out;
302         /* get new socket */
303         ret = get_socket();
304         if (ret < 0)
305                 goto err_out;
306         pcd->fd = ret;
307         /* init their_addr */
308         init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
309         ret = para_connect(pcd->fd, &their_addr);
310         if (ret < 0)
311                 goto err_out;
312         pcd->status = CL_CONNECTED;
313         ret = mark_fd_nonblock(pcd->fd);
314         if (ret < 0)
315                 goto err_out;
316         pcd->task.pre_select = client_pre_select;
317         pcd->task.post_select = client_post_select;
318         pcd->task.private_data = pcd;
319         sprintf(pcd->task.status, "client");
320         register_task(&pcd->task);
321         return 1;
322 err_out:
323         if (pcd->fd >= 0)
324                 close(pcd->fd);
325         return ret;
326 }