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