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>
30 struct gengetopt_args_info args_info
;
35 void para_log(int ll
, const char* fmt
,...)
40 /* ignore log message if loglevel is not high enough */
41 if (ll
< args_info
.loglevel_arg
)
48 vfprintf(stdout
, fmt
, argp
);
52 void get_options(int argc
, char *argv
[],
53 char **config_file
, char **key_file
)
56 static char default_key_file
[_POSIX_PATH_MAX
] = "";
57 static char default_config_file
[_POSIX_PATH_MAX
] = "";
61 cmdline_parser(argc
, argv
, &args_info
);
62 if (!args_info
.user_given
)
63 args_info
.user_arg
= para_logname();
64 if (!args_info
.key_file_given
) {
65 home
= para_homedir();
66 sprintf(default_key_file
, "%s/.paraslash/key.%s", home
,
70 if (!args_info
.config_file_given
) {
71 home
= para_homedir();
72 sprintf(default_config_file
, "%s/.paraslash/client.conf",
76 if (!args_info
.config_file_given
)
77 *config_file
= default_config_file
;
79 *config_file
= args_info
.config_file_arg
;
80 ret
= stat(*config_file
, &statbuf
);
81 if (ret
&& args_info
.config_file_given
) {
82 fprintf(stderr
, "can not stat config file %s\n",
83 args_info
.config_file_arg
);
87 cmdline_parser_configfile(*config_file
, &args_info
, 0, 0, 0);
88 if (!args_info
.key_file_given
)
89 *key_file
= default_key_file
;
91 *key_file
= args_info
.key_file_arg
;
94 static RC4_KEY rc4_recv_key
;
95 static RC4_KEY rc4_send_key
;
96 static unsigned char rc4_buf
[2 * RC4_KEY_LEN
];
98 static void rc4_send(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
100 RC4(&rc4_send_key
, len
, indata
, outdata
);
103 static void rc4_recv(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
105 RC4(&rc4_recv_key
, len
, indata
, outdata
);
107 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
108 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
111 static void append_str(char **data
, const char* append
)
114 char *tmp
= make_message("%s\n%s", *data
, append
);
118 *data
= para_strdup(append
);
124 int main(int argc
, char *argv
[])
127 int sockfd
, numbytes
, i
, received
, ret
;
129 struct sockaddr_in their_addr
;
130 char *command
= NULL
;
133 char *key_file
, *config_file
;
134 long unsigned challenge_nr
;
136 get_options(argc
, argv
, &config_file
, &key_file
);
137 if (args_info
.loglevel_arg
<= NOTICE
)
138 cmdline_parser_print_version();
140 "current loglevel: %d\n"
141 "using config_file: %s\n"
142 "using key_file: %s\n"
143 "connecting to %s:%d\n",
144 args_info
.loglevel_arg
,
147 args_info
.hostname_arg
,
148 args_info
.server_port_arg
150 if (!args_info
.inputs_num
) {
151 PARA_ERROR_LOG("%s", "syntax error\n");
155 for (i
= 0; i
< args_info
.inputs_num
; i
++)
156 append_str(&command
, args_info
.inputs
[i
]);
158 crypt_function_recv
= NULL
;
159 crypt_function_send
= NULL
;
160 /* get the host info */
161 PARA_NOTICE_LOG("getting host info of %s\n",
162 args_info
.hostname_arg
);
163 if (!(he
= get_host_info(args_info
.hostname_arg
)))
166 if ((sockfd
= get_socket()) < 0)
168 /* init their_addr */
169 init_sockaddr(&their_addr
, args_info
.server_port_arg
, he
);
171 PARA_NOTICE_LOG("connecting to %s...\n",
172 args_info
.hostname_arg
);
173 if (para_connect(sockfd
, &their_addr
) < 0)
175 /* Receive Welcome message */
176 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
178 /* send auth command */
179 auth_str
= make_message("auth %s%s", args_info
.plain_given
? "" : "rc4 ",
181 PARA_INFO_LOG("<-- %s--> %s\n", buf
, auth_str
);
182 if (send_buffer(sockfd
, auth_str
) < 0)
184 /* receive challenge number */
185 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
187 if (numbytes
!= 64) {
188 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
190 buf
[numbytes
] = '\0';
191 PARA_ERROR_LOG("received the following instead: %s\n", buf
);
194 PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes
);
195 /* decrypt challenge number */
196 ret
= para_decrypt_challenge(key_file
, &challenge_nr
, (unsigned char *) buf
,
199 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret
);
202 /* send decrypted challenge */
203 PARA_INFO_LOG("--> %lu\n", challenge_nr
);
204 if (send_va_buffer(sockfd
, "%s%lu", CHALLENGE_RESPONSE_MSG
, challenge_nr
) < 0)
206 /* Wait for approval */
207 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
208 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
210 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
212 /* Check if server has sent "Proceed" message */
213 if (!strstr(buf
, PROCEED_MSG
)) {
214 PARA_EMERG_LOG("%s", "authentication failed\n");
217 if (numbytes
>= PROCEED_MSG_LEN
+ 32) {
218 PARA_INFO_LOG("%s", "decrypting session key\n");
219 if (para_decrypt_buffer(key_file
, rc4_buf
,
220 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1,
221 numbytes
- PROCEED_MSG_LEN
- 1) < 0) {
222 PARA_EMERG_LOG("%s", "error receiving rc4 key\n");
225 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
226 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
227 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
228 rc4_buf
[0], rc4_buf
[1], rc4_buf
[2], rc4_buf
[3]);
229 crypt_function_recv
= rc4_recv
;
230 crypt_function_send
= rc4_send
;
233 PARA_INFO_LOG("--> %s\n", command
);
234 if (send_buffer(sockfd
, command
) < 0)
238 if (send_buffer(sockfd
, EOC_MSG
"\n") < 0)
240 PARA_NOTICE_LOG("%s", "command sent.\n");
242 while ((numbytes
= recv_bin_buffer(sockfd
, buf
, sizeof(buf
) - 1)) > 0) {
243 buf
[numbytes
] = '\0';
244 if (!received
&& strstr(buf
, AWAITING_DATA_MSG
)) {
245 PARA_NOTICE_LOG("%s", "sending stdin\n");
246 while ((ret
= read(STDIN_FILENO
, buf
,
248 if (send_bin_buffer(sockfd
, buf
, ret
) < 0)
251 PARA_NOTICE_LOG("%s", "closing connection\n");
256 if (write(STDOUT_FILENO
, buf
, numbytes
) != numbytes
)
260 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
262 return ret
>= 0? 0: 1;