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