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>
32 enum {CL_CONNECTED
, CL_SENT_AUTH
, CL_RECEIVED_CHALLENGE
, CL_SENT_CH_RESPONSE
,
33 CL_RECEIVED_PROCEED
, CL_SENT_COMMAND
, CL_SENDING_STDIN
, CL_RECV_DATA
};
35 struct private_client_data
{
38 struct client_conf
*conf
;
42 //struct gengetopt_conf conf;
43 struct client_args_info conf
;
50 void para_log(int ll
, const char* fmt
,...)
54 /* ignore log message if loglevel is not high enough */
55 if (ll
< conf
.loglevel_arg
)
58 vfprintf(stderr
, fmt
, argp
);
62 static int get_options(int argc
, char *argv
[],
63 char **config_file
, char **key_file
)
66 static char default_key_file
[_POSIX_PATH_MAX
] = "";
67 static char default_config_file
[_POSIX_PATH_MAX
] = "";
71 cmdline_parser(argc
, argv
, &conf
);
73 conf
.user_arg
= para_logname();
74 if (!conf
.key_file_given
) {
75 home
= para_homedir();
76 sprintf(default_key_file
, "%s/.paraslash/key.%s", home
,
80 if (!conf
.config_file_given
) {
81 home
= para_homedir();
82 sprintf(default_config_file
, "%s/.paraslash/client.conf",
86 if (!conf
.config_file_given
)
87 *config_file
= default_config_file
;
89 *config_file
= conf
.config_file_arg
;
90 ret
= stat(*config_file
, &statbuf
);
91 if (ret
&& conf
.config_file_given
)
94 cmdline_parser_configfile(*config_file
, &conf
, 0, 0, 0);
95 if (!conf
.key_file_given
)
96 *key_file
= default_key_file
;
98 *key_file
= conf
.key_file_arg
;
102 static RC4_KEY rc4_recv_key
;
103 static RC4_KEY rc4_send_key
;
104 static unsigned char rc4_buf
[2 * RC4_KEY_LEN
];
106 static void rc4_send(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
108 RC4(&rc4_send_key
, len
, indata
, outdata
);
111 static void rc4_recv(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
113 RC4(&rc4_recv_key
, len
, indata
, outdata
);
115 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
116 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
119 static void append_str(char **data
, const char* append
)
122 char *tmp
= make_message("%s\n%s", *data
, append
);
126 *data
= para_strdup(append
);
130 static int send_stdin(int fd
)
135 PARA_NOTICE_LOG("%s", "sending stdin\n");
137 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
140 ret
= send_bin_buffer(fd
, buf
, ret
);
150 int main(int argc
, char *argv
[])
153 int sockfd
= -1, numbytes
, i
, received
, ret
;
155 struct sockaddr_in their_addr
;
156 char *command
= NULL
;
159 char *key_file
, *config_file
;
160 long unsigned challenge_nr
;
162 ret
= get_options(argc
, argv
, &config_file
, &key_file
);
165 if (conf
.loglevel_arg
<= NOTICE
)
166 cmdline_parser_print_version();
168 "current loglevel: %d\n"
169 "using config_file: %s\n"
170 "using key_file: %s\n"
171 "connecting to %s:%d\n",
178 ret
= - E_CLIENT_SYNTAX
;
179 if (!conf
.inputs_num
)
182 for (i
= 0; i
< conf
.inputs_num
; i
++)
183 append_str(&command
, conf
.inputs
[i
]);
184 crypt_function_recv
= NULL
;
185 crypt_function_send
= NULL
;
186 /* get the host info */
187 PARA_NOTICE_LOG("getting host info of %s\n",
189 ret
= get_host_info(conf
.hostname_arg
, &he
);
197 /* init their_addr */
198 init_sockaddr(&their_addr
, conf
.server_port_arg
, he
);
200 PARA_NOTICE_LOG("connecting to %s\n", conf
.hostname_arg
);
201 ret
= para_connect(sockfd
, &their_addr
);
204 /* receive welcome message */
205 ret
= recv_buffer(sockfd
, buf
, sizeof(buf
));
208 /* send auth command */
209 auth_str
= make_message("auth %s%s", conf
.plain_given
? "" : "rc4 ",
211 PARA_INFO_LOG("<-- %s--> %s\n", buf
, auth_str
);
212 ret
= send_buffer(sockfd
, auth_str
);
215 /* receive challenge number */
216 ret
= recv_buffer(sockfd
, buf
, sizeof(buf
));
220 ret
= -E_INVALID_CHALLENGE
;
221 PARA_ERROR_LOG("received the following: %s\n", buf
);
224 PARA_INFO_LOG("%s", "<-- [challenge]\n");
225 /* decrypt challenge number */
226 ret
= para_decrypt_challenge(key_file
, &challenge_nr
, (unsigned char *) buf
, 64);
229 /* send decrypted challenge */
230 PARA_INFO_LOG("--> %lu\n", challenge_nr
);
231 ret
= send_va_buffer(sockfd
, "%s%lu", CHALLENGE_RESPONSE_MSG
, challenge_nr
);
234 /* wait for approval */
235 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
236 ret
= recv_buffer(sockfd
, buf
, sizeof(buf
));
240 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
242 /* check if server has sent "Proceed" message */
243 ret
= -E_CLIENT_AUTH
;
244 if (!strstr(buf
, PROCEED_MSG
))
246 if (numbytes
>= PROCEED_MSG_LEN
+ 32) {
247 PARA_INFO_LOG("%s", "decrypting session key\n");
248 ret
= para_decrypt_buffer(key_file
, rc4_buf
,
249 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1,
250 numbytes
- PROCEED_MSG_LEN
- 1);
253 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
254 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
255 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
256 rc4_buf
[0], rc4_buf
[1], rc4_buf
[2], rc4_buf
[3]);
257 crypt_function_recv
= rc4_recv
;
258 crypt_function_send
= rc4_send
;
261 PARA_INFO_LOG("--> %s\n", command
);
262 ret
= send_buffer(sockfd
, command
);
267 ret
= send_buffer(sockfd
, EOC_MSG
"\n");
270 PARA_NOTICE_LOG("%s", "command sent.\n");
273 ret
= recv_bin_buffer(sockfd
, buf
, sizeof(buf
) - 1);
276 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
281 if (!received
&& strstr(buf
, AWAITING_DATA_MSG
)) {
282 ret
= send_stdin(sockfd
);
286 ret
= write(STDOUT_FILENO
, buf
, numbytes
);
287 if (ret
!= numbytes
) {
288 ret
= -E_SHORT_CLIENT_WRITE
;
296 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
297 return ret
>= 0? EXIT_SUCCESS
: EXIT_FAILURE
;