]> git.tuebingen.mpg.de Git - paraslash.git/blob - client.c
client: preparations for sched conversion
[paraslash.git] / client.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.c the client program used to connect to para_server */
20
21 #include "para.h"
22 #include "config.h"
23 #include "client.cmdline.h"
24 #include "crypt.h"
25 #include "rc4.h"
26 #include <openssl/rc4.h>
27 #include "net.h"
28 #include "string.h"
29 #include "error.h"
30
31 enum {CL_CONNECTED, CL_SENT_AUTH, CL_RECEIVED_CHALLENGE, CL_SENT_CH_RESPONSE,
32         CL_RECEIVED_PROCEED, CL_SENT_COMMAND, CL_SENDING_STDIN, CL_RECV_DATA};
33
34 struct private_client_data {
35         int status;
36         int fd;
37         struct client_args_info conf;
38         char *config_file;
39         char *key_file;
40         char *user;
41         char *in_buf;
42         size_t *in_loaded;
43         char *out_buf;
44         size_t *out_loaded;
45 };
46
47 INIT_CLIENT_ERRLISTS;
48
49 static struct private_client_data *pcd;
50
51 /*
52  * client log function
53  */
54 void para_log(int ll, const char* fmt,...)
55 {
56         va_list argp;
57
58         /* ignore log message if loglevel is not high enough */
59         if (pcd && ll < pcd->conf.loglevel_arg)
60                 return;
61         va_start(argp, fmt);
62         vfprintf(stderr, fmt, argp);
63         va_end(argp);
64 }
65
66 static void client_close(struct private_client_data *pcd)
67 {
68         if (pcd)
69                 return;
70         if (pcd->fd >= 0)
71                 close(pcd->fd);
72         free(pcd->user);
73         free(pcd->config_file);
74         free(pcd->key_file);
75         free(pcd);
76 }
77
78 static int client_parse_config(int argc, char *argv[],
79                 struct private_client_data **pcd_ptr)
80 {
81         char *home = para_homedir();
82         struct stat statbuf;
83         int ret;
84         struct private_client_data *p =
85                 para_calloc(sizeof(struct private_client_data));
86
87         p->fd = -1;
88         cmdline_parser(argc, argv, &p->conf);
89         ret = - E_CLIENT_SYNTAX;
90         if (!p->conf.inputs_num)
91                 goto out;
92         p->user = p->conf.user_given?
93                 para_strdup(p->conf.user_arg) : para_logname();
94
95         p->key_file = p->conf.key_file_given?
96                 para_strdup(p->conf.key_file_arg) :
97                 make_message("%s/.paraslash/key.%s", home, p->user);
98
99         p->config_file = p->conf.config_file_given?
100                 para_strdup(p->conf.config_file_arg) :
101                 make_message("%s/.paraslash/client.conf", home);
102         ret = stat(p->config_file, &statbuf);
103         if (ret && p->conf.config_file_given) {
104                 ret = -E_NO_CONFIG;
105                 goto out;
106         }
107         if (!ret)
108                 cmdline_parser_configfile(p->config_file, &p->conf, 0, 0, 0);
109         ret = 1;
110 out:
111         free(home);
112         if (ret < 0)
113                 client_close(p);
114         else
115                 *pcd_ptr = p;
116         return ret;
117 }
118
119 static RC4_KEY rc4_recv_key;
120 static RC4_KEY rc4_send_key;
121 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
122
123 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
124 {
125         RC4(&rc4_send_key, len, indata, outdata);
126 }
127
128 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
129 {
130         RC4(&rc4_recv_key, len, indata, outdata);
131 }
132 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
133 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
134
135
136 static void append_str(char **data, const char* append)
137 {
138         if (*data) {
139                 char *tmp = make_message("%s\n%s", *data, append);
140                 free(*data);
141                 *data = tmp;
142         } else
143                 *data = para_strdup(append);
144 }
145
146
147 static int send_stdin(int fd)
148 {
149         char buf[8192];
150         int ret;
151
152         PARA_NOTICE_LOG("%s", "sending stdin\n");
153         for (;;) {
154                 ret = read(STDIN_FILENO, buf, sizeof(buf));
155                 if (ret <= 0)
156                         return ret;
157                 ret = send_bin_buffer(fd, buf, ret);
158                 if (ret < 0)
159                         return ret;
160         }
161         return 1;
162 }
163
164 /*
165  * MAIN
166  */
167 int main(int argc, char *argv[])
168 {
169
170         int numbytes, i, received, ret;
171         struct hostent *he;
172         struct sockaddr_in their_addr;
173         char *command = NULL;
174         char buf[8192];
175         char *auth_str;
176         long unsigned challenge_nr;
177
178         ret = client_parse_config(argc, argv, &pcd);
179         if (ret < 0)
180                 goto out;
181         if (pcd->conf.loglevel_arg <= NOTICE)
182                 cmdline_parser_print_version();
183         PARA_INFO_LOG(
184                 "current loglevel: %d\n"
185                 "using config_file: %s\n"
186                 "using key_file: %s\n"
187                 "connecting to %s:%d\n",
188                 pcd->conf.loglevel_arg,
189                 pcd->config_file,
190                 pcd->key_file,
191                 pcd->conf.hostname_arg,
192                 pcd->conf.server_port_arg
193         );
194         /* concat args */
195         for (i = 0; i < pcd->conf.inputs_num; i++)
196                 append_str(&command, pcd->conf.inputs[i]);
197         crypt_function_recv = NULL;
198         crypt_function_send = NULL;
199         /* get the host info */
200         PARA_NOTICE_LOG("getting host info of %s\n",
201                 pcd->conf.hostname_arg);
202         ret = get_host_info(pcd->conf.hostname_arg, &he);
203         if (ret < 0)
204                 goto out;
205         /* get new socket */
206         ret = get_socket();
207         if (ret < 0)
208                 goto out;
209         pcd->fd = ret;
210         /* init their_addr */
211         init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
212         /* connect */
213         PARA_NOTICE_LOG("connecting to %s\n", pcd->conf.hostname_arg);
214         ret = para_connect(pcd->fd, &their_addr);
215         if (ret < 0)
216                 goto out;
217         /* receive welcome message */
218         ret = recv_buffer(pcd->fd, buf, sizeof(buf));
219         if (ret < 0)
220                 goto out;
221         /* send auth command */
222         auth_str = make_message("auth %s%s", pcd->conf.plain_given?  "" : "rc4 ",
223                 pcd->user);
224         PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
225         ret = send_buffer(pcd->fd, auth_str);
226         if (ret < 0)
227                 goto out;
228         /* receive challenge number */
229         ret = recv_buffer(pcd->fd, buf, sizeof(buf));
230         if (ret < 0)
231                 goto out;
232         if (ret != 64) {
233                 ret = -E_INVALID_CHALLENGE;
234                 PARA_ERROR_LOG("received the following: %s\n", buf);
235                 goto out;
236         }
237         PARA_INFO_LOG("%s", "<-- [challenge]\n");
238         /* decrypt challenge number */
239         ret = para_decrypt_challenge(pcd->key_file, &challenge_nr,
240                 (unsigned char *) buf, 64);
241         if (ret < 0)
242                 goto out;
243         /* send decrypted challenge */
244         PARA_INFO_LOG("--> %lu\n", challenge_nr);
245         ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr);
246         if (ret < 0)
247                 goto out;
248         /* wait for approval */
249         PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
250         ret = recv_buffer(pcd->fd, buf, sizeof(buf));
251         if (ret < 0)
252                 goto out;
253         numbytes = ret;
254         PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
255                 "info ++++\n", buf);
256         /* check if server has sent "Proceed" message */
257         ret = -E_CLIENT_AUTH;
258         if (!strstr(buf, PROCEED_MSG))
259                 goto out;
260         if (numbytes >= PROCEED_MSG_LEN + 32) {
261                 PARA_INFO_LOG("%s", "decrypting session key\n");
262                 ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
263                         (unsigned char *)buf + PROCEED_MSG_LEN + 1,
264                         numbytes - PROCEED_MSG_LEN - 1);
265                 if (ret < 0)
266                         goto out;
267                 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
268                 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
269                 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
270                         rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
271                 crypt_function_recv = rc4_recv;
272                 crypt_function_send = rc4_send;
273         }
274         /* send command */
275         PARA_INFO_LOG("--> %s\n", command);
276         ret = send_buffer(pcd->fd, command);
277         if (ret < 0)
278                 goto out;
279         free(command);
280         command = NULL;
281         ret = send_buffer(pcd->fd, EOC_MSG "\n");
282         if (ret < 0)
283                 goto out;
284         PARA_NOTICE_LOG("%s", "command sent.\n");
285         received = 0;
286         for (;;) {
287                 ret = recv_bin_buffer(pcd->fd, buf, sizeof(buf) - 1);
288                 if (ret <= 0) {
289                         if (!ret)
290                                 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
291                         goto out;
292                 }
293                 buf[ret] = '\0';
294                 numbytes = ret;
295                 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
296                         ret = send_stdin(pcd->fd);
297                         goto out;
298                 }
299                 received = 1;
300                 ret = write(STDOUT_FILENO, buf, numbytes);
301                 if (ret != numbytes) {
302                         ret = -E_SHORT_CLIENT_WRITE;
303                         goto out;
304                 }
305         }
306         client_close(pcd);
307 out:
308         if (ret < 0)
309                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
310         return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
311 }