a0128ad9b4508835caf4d11478b2e340b73118df
[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.h"
31 #include "error.h"
32
33 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 void rc4_recv(unsigned long len, const unsigned char *indata,
41                 unsigned char *outdata, void *private_data)
42 {
43         struct private_client_data *pcd = private_data;
44         RC4(&pcd->rc4_recv_key, len, indata, outdata);
45 }
46
47
48 void client_close(struct private_client_data *pcd)
49 {
50         if (pcd)
51                 return;
52         if (pcd->fd >= 0)
53                 close(pcd->fd);
54         free(pcd->user);
55         free(pcd->config_file);
56         free(pcd->key_file);
57         free(pcd);
58 }
59
60 int client_parse_config(int argc, char *argv[],
61                 struct private_client_data **pcd_ptr)
62 {
63         char *home = para_homedir();
64         struct stat statbuf;
65         int ret;
66         struct private_client_data *pcd =
67                 para_calloc(sizeof(struct private_client_data));
68
69         pcd->fd = -1;
70         cmdline_parser(argc, argv, &pcd->conf);
71         ret = - E_CLIENT_SYNTAX;
72         if (!pcd->conf.inputs_num)
73                 goto out;
74         pcd->user = pcd->conf.user_given?
75                 para_strdup(pcd->conf.user_arg) : para_logname();
76
77         pcd->key_file = pcd->conf.key_file_given?
78                 para_strdup(pcd->conf.key_file_arg) :
79                 make_message("%s/.paraslash/key.%s", home, pcd->user);
80
81         pcd->config_file = pcd->conf.config_file_given?
82                 para_strdup(pcd->conf.config_file_arg) :
83                 make_message("%s/.paraslash/client.conf", home);
84         ret = stat(pcd->config_file, &statbuf);
85         if (ret && pcd->conf.config_file_given) {
86                 ret = -E_NO_CONFIG;
87                 goto out;
88         }
89         if (!ret)
90                 cmdline_parser_configfile(pcd->config_file, &pcd->conf, 0, 0, 0);
91         ret = 1;
92         *pcd_ptr = pcd;
93         PARA_INFO_LOG(
94                 "current loglevel: %d\n"
95                 "using config_file: %s\n"
96                 "using key_file: %s\n"
97                 "connecting to %s:%d\n" ,
98                 pcd->conf.loglevel_arg,
99                 pcd->config_file,
100                 pcd->key_file,
101                 pcd->conf.hostname_arg, pcd->conf.server_port_arg
102         );
103 out:
104         free(home);
105         if (ret < 0)
106                 client_close(pcd);
107         return ret;
108 }
109
110 void client_pre_select(struct sched *s, struct task *t)
111 {
112         struct private_client_data *pcd = t->private_data;
113
114         PARA_INFO_LOG("status %d\n", pcd->status);
115         t->ret = 1;
116         pcd->check_r = 0;
117         pcd->check_w = 0;
118         if (pcd->fd < 0)
119                 return;
120         switch (pcd->status) {
121         case CL_CONNECTED:
122         case CL_SENT_AUTH:
123         case CL_SENT_CH_RESPONSE:
124         case CL_SENT_COMMAND:
125                 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
126                 pcd->check_r = 1;
127                 return;
128
129         case CL_RECEIVED_WELCOME:
130         case CL_RECEIVED_CHALLENGE:
131         case CL_RECEIVED_PROCEED:
132                 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
133                 pcd->check_w = 1;
134                 return;
135
136         case CL_RECEIVING_SERVER_OUTPUT:
137                 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
138                         para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
139                         pcd->check_r = 1;
140                 }
141                 return;
142         case CL_SENDING_STDIN:
143                 if (*pcd->in_loaded) {
144                         PARA_INFO_LOG("loaded: %d\n", *pcd->in_loaded);
145                         para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
146                         pcd->check_w = 1;
147                 } else {
148                         if (*pcd->in_eof) {
149                                 t->ret = -E_INPUT_EOF;
150                                 s->timeout.tv_sec = 0;
151                                 s->timeout.tv_usec = 1;
152                         }
153                 }
154                 return;
155         }
156 }
157
158 static ssize_t client_recv_buffer(struct private_client_data *pcd)
159 {
160         ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
161                 CLIENT_BUFSIZE - pcd->loaded);
162         if (!ret)
163                 return -E_SERVER_EOF;
164         if (ret > 0)
165                 pcd->loaded += ret;
166         return ret;
167
168 }
169
170 void client_post_select(struct sched *s, struct task *t)
171 {
172         struct private_client_data *pcd = t->private_data;
173
174         PARA_INFO_LOG("status %d\n", pcd->status);
175         t->ret = 1;
176         if (pcd->fd < 0)
177                 return;
178         if (!pcd->check_r && !pcd->check_w)
179                 return;
180         if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
181                 return;
182         if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
183                 return;
184         switch (pcd->status) {
185         case CL_CONNECTED: /* receive welcome message */
186                 t->ret = client_recv_buffer(pcd);
187                 if (t->ret > 0)
188                         pcd->status = CL_RECEIVED_WELCOME;
189                 return;
190         case CL_RECEIVED_WELCOME: /* send auth command */
191                 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
192                         "" : "rc4 ", pcd->user);
193                 PARA_INFO_LOG("--> %s\n", pcd->buf);
194                 t->ret = send_buffer(pcd->fd, pcd->buf);
195                 if (t->ret >= 0)
196                         pcd->status = CL_SENT_AUTH;
197                 return;
198         case CL_SENT_AUTH: /* receive challenge number */
199                 pcd->loaded = 0;
200                 t->ret = client_recv_buffer(pcd);
201                 if (t->ret < 0)
202                         return;
203                 if (t->ret != 64) {
204                         t->ret = -E_INVALID_CHALLENGE;
205                         PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
206                         return;
207                 }
208                 PARA_INFO_LOG("%s", "<-- [challenge]\n");
209                 /* decrypt challenge number */
210                 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
211                         (unsigned char *) pcd->buf, 64);
212                 if (t->ret > 0)
213                         pcd->status = CL_RECEIVED_CHALLENGE;
214                 return;
215         case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
216                 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
217                 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
218                         pcd->challenge_nr);
219                 if (t->ret > 0)
220                         pcd->status = CL_SENT_CH_RESPONSE;
221                 return;
222         case CL_SENT_CH_RESPONSE: /* read server response */
223                 {
224                 size_t bytes_received;
225                 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
226                 pcd->loaded = 0;
227                 t->ret = client_recv_buffer(pcd);
228                 if (t->ret < 0)
229                         return;
230                 bytes_received = t->ret;
231                 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
232                         "info ++++\n", pcd->buf);
233                 /* check if server has sent "Proceed" message */
234                 t->ret = -E_CLIENT_AUTH;
235                 if (!strstr(pcd->buf, PROCEED_MSG))
236                         return;
237                 t->ret = 1;
238                 pcd->status = CL_RECEIVED_PROCEED;
239                 if (bytes_received < PROCEED_MSG_LEN + 32)
240                         return;
241                 PARA_INFO_LOG("%s", "decrypting session key\n");
242                 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
243                         (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
244                         bytes_received - PROCEED_MSG_LEN - 1);
245                 if (t->ret < 0)
246                         return;
247                 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
248                 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
249                 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
250                 }
251         case CL_RECEIVED_PROCEED: /* concat args and send command */
252                 {
253                 int i;
254                 char *command = NULL;
255                 for (i = 0; i < pcd->conf.inputs_num; i++) {
256                         char *tmp = command;
257                         command = make_message("%s\n%s", command?
258                                 command : "", pcd->conf.inputs[i]);
259                         free(tmp);
260                 }
261                 command = para_strcat(command, EOC_MSG "\n");
262                 PARA_INFO_LOG("--> %s\n", command);
263                 t->ret = send_buffer(pcd->fd, command);
264                 free(command);
265                 if (t->ret > 0)
266                         pcd->status = CL_SENT_COMMAND;
267                 return;
268                 }
269         case CL_SENT_COMMAND:
270                 pcd->loaded = 0;
271                 t->ret = client_recv_buffer(pcd);
272                 if (t->ret < 0)
273                         return;
274                 t->ret = -E_HANDSHAKE_COMPLETE;
275                 if (strstr(pcd->buf, AWAITING_DATA_MSG))
276                         pcd->status = CL_SENDING_STDIN;
277                 else
278                         pcd->status = CL_RECEIVING_SERVER_OUTPUT;
279                 return;
280         case CL_SENDING_STDIN: /* FIXME: might block */
281                 PARA_INFO_LOG("loaded: %d\n", *pcd->in_loaded);
282                 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
283                 if (t->ret <= 0) {
284                         if (!t->ret)
285                                 t->ret = 1;
286                         return;
287                 }
288                 *pcd->in_loaded = 0; /* FIXME: short writes */
289                 return;
290         case CL_RECEIVING_SERVER_OUTPUT:
291                 t->ret = client_recv_buffer(pcd);
292                 return;
293         }
294
295 }
296
297 int client_open(struct private_client_data *pcd)
298 {
299         int ret;
300         struct hostent *he;
301         struct sockaddr_in their_addr;
302
303         /* get the host info */
304         PARA_NOTICE_LOG("getting host info of %s\n",
305                 pcd->conf.hostname_arg);
306         ret = get_host_info(pcd->conf.hostname_arg, &he);
307         if (ret < 0)
308                 goto out;
309         /* get new socket */
310         ret = get_socket();
311         if (ret < 0)
312                 goto out;
313         pcd->fd = ret;
314         /* init their_addr */
315         init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
316         /* connect */
317         PARA_NOTICE_LOG("connecting to %s\n", pcd->conf.hostname_arg);
318         ret = para_connect(pcd->fd, &their_addr);
319         if (ret < 0)
320                 goto out;
321         pcd->status = CL_CONNECTED;
322         pcd->task.pre_select = client_pre_select;
323         pcd->task.post_select = client_post_select;
324         pcd->task.private_data = pcd;
325         sprintf(pcd->task.status, "client");
326         register_task(&pcd->task);
327         ret = 1;
328 out:
329         return ret;
330 }