b77a2f68299ec4f47dc6c90e6b1d95ed4dde017b
2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file client.c the client program used to connect to para_server */
23 #include "client.cmdline.h"
26 #include <openssl/rc4.h>
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
};
34 struct private_client_data
{
37 struct client_args_info conf
;
52 static struct private_client_data
*pcd
;
57 void para_log(int ll
, const char* fmt
,...)
61 /* ignore log message if loglevel is not high enough */
62 if (pcd
&& ll
< pcd
->conf
.loglevel_arg
)
65 vfprintf(stderr
, fmt
, argp
);
69 static void client_close(struct private_client_data
*pcd
)
76 free(pcd
->config_file
);
81 static int client_parse_config(int argc
, char *argv
[],
82 struct private_client_data
**pcd_ptr
)
84 char *home
= para_homedir();
87 struct private_client_data
*p
=
88 para_calloc(sizeof(struct private_client_data
));
91 cmdline_parser(argc
, argv
, &p
->conf
);
92 ret
= - E_CLIENT_SYNTAX
;
93 if (!p
->conf
.inputs_num
)
95 p
->user
= p
->conf
.user_given
?
96 para_strdup(p
->conf
.user_arg
) : para_logname();
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
);
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
) {
111 cmdline_parser_configfile(p
->config_file
, &p
->conf
, 0, 0, 0);
122 static void rc4_send(unsigned long len
, const unsigned char *indata
,
123 unsigned char *outdata
)
125 RC4(&pcd
->rc4_send_key
, len
, indata
, outdata
);
128 static void rc4_recv(unsigned long len
, const unsigned char *indata
,
129 unsigned char *outdata
)
131 RC4(&pcd
->rc4_recv_key
, len
, indata
, outdata
);
134 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
135 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
138 static void append_str(char **data
, const char* append
)
141 char *tmp
= make_message("%s\n%s", *data
, append
);
145 *data
= para_strdup(append
);
149 static int send_stdin(int fd
)
154 PARA_NOTICE_LOG("%s", "sending stdin\n");
156 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
159 ret
= send_bin_buffer(fd
, buf
, ret
);
169 int main(int argc
, char *argv
[])
172 int numbytes
, i
, received
, ret
;
174 struct sockaddr_in their_addr
;
175 char *command
= NULL
;
178 long unsigned challenge_nr
;
179 unsigned char rc4_buf
[2 * RC4_KEY_LEN
] = "";
181 ret
= client_parse_config(argc
, argv
, &pcd
);
184 if (pcd
->conf
.loglevel_arg
<= NOTICE
)
185 cmdline_parser_print_version();
187 "current loglevel: %d\n"
188 "using config_file: %s\n"
189 "using key_file: %s\n"
190 "connecting to %s:%d\n",
191 pcd
->conf
.loglevel_arg
,
194 pcd
->conf
.hostname_arg
,
195 pcd
->conf
.server_port_arg
198 for (i
= 0; i
< pcd
->conf
.inputs_num
; i
++)
199 append_str(&command
, pcd
->conf
.inputs
[i
]);
200 crypt_function_recv
= NULL
;
201 crypt_function_send
= NULL
;
202 /* get the host info */
203 PARA_NOTICE_LOG("getting host info of %s\n",
204 pcd
->conf
.hostname_arg
);
205 ret
= get_host_info(pcd
->conf
.hostname_arg
, &he
);
213 /* init their_addr */
214 init_sockaddr(&their_addr
, pcd
->conf
.server_port_arg
, he
);
216 PARA_NOTICE_LOG("connecting to %s\n", pcd
->conf
.hostname_arg
);
217 ret
= para_connect(pcd
->fd
, &their_addr
);
220 /* receive welcome message */
221 ret
= recv_buffer(pcd
->fd
, buf
, sizeof(buf
));
224 /* send auth command */
225 auth_str
= make_message("auth %s%s", pcd
->conf
.plain_given
? "" : "rc4 ",
227 PARA_INFO_LOG("<-- %s--> %s\n", buf
, auth_str
);
228 ret
= send_buffer(pcd
->fd
, auth_str
);
231 /* receive challenge number */
232 ret
= recv_buffer(pcd
->fd
, buf
, sizeof(buf
));
236 ret
= -E_INVALID_CHALLENGE
;
237 PARA_ERROR_LOG("received the following: %s\n", buf
);
240 PARA_INFO_LOG("%s", "<-- [challenge]\n");
241 /* decrypt challenge number */
242 ret
= para_decrypt_challenge(pcd
->key_file
, &challenge_nr
,
243 (unsigned char *) buf
, 64);
246 /* send decrypted challenge */
247 PARA_INFO_LOG("--> %lu\n", challenge_nr
);
248 ret
= send_va_buffer(pcd
->fd
, "%s%lu", CHALLENGE_RESPONSE_MSG
, challenge_nr
);
251 /* wait for approval */
252 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
253 ret
= recv_buffer(pcd
->fd
, buf
, sizeof(buf
));
257 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
259 /* check if server has sent "Proceed" message */
260 ret
= -E_CLIENT_AUTH
;
261 if (!strstr(buf
, PROCEED_MSG
))
263 if (numbytes
>= PROCEED_MSG_LEN
+ 32) {
264 PARA_INFO_LOG("%s", "decrypting session key\n");
265 ret
= para_decrypt_buffer(pcd
->key_file
, rc4_buf
,
266 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1,
267 numbytes
- PROCEED_MSG_LEN
- 1);
270 RC4_set_key(&pcd
->rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
271 RC4_set_key(&pcd
->rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
272 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
273 rc4_buf
[0], rc4_buf
[1], rc4_buf
[2], rc4_buf
[3]);
274 crypt_function_recv
= rc4_recv
;
275 crypt_function_send
= rc4_send
;
278 PARA_INFO_LOG("--> %s\n", command
);
279 ret
= send_buffer(pcd
->fd
, command
);
284 ret
= send_buffer(pcd
->fd
, EOC_MSG
"\n");
287 PARA_NOTICE_LOG("%s", "command sent.\n");
290 ret
= recv_bin_buffer(pcd
->fd
, buf
, sizeof(buf
) - 1);
293 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
298 if (!received
&& strstr(buf
, AWAITING_DATA_MSG
)) {
299 ret
= send_stdin(pcd
->fd
);
303 ret
= write(STDOUT_FILENO
, buf
, numbytes
);
304 if (ret
!= numbytes
) {
305 ret
= -E_SHORT_CLIENT_WRITE
;
312 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
313 return ret
>= 0? EXIT_SUCCESS
: EXIT_FAILURE
;