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