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