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