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
)
44 vfprintf(stderr
, fmt
, argp
);
48 void get_options(int argc
, char *argv
[],
49 char **config_file
, char **key_file
)
52 static char default_key_file
[_POSIX_PATH_MAX
] = "";
53 static char default_config_file
[_POSIX_PATH_MAX
] = "";
57 cmdline_parser(argc
, argv
, &args_info
);
58 if (!args_info
.user_given
)
59 args_info
.user_arg
= para_logname();
60 if (!args_info
.key_file_given
) {
61 home
= para_homedir();
62 sprintf(default_key_file
, "%s/.paraslash/key.%s", home
,
66 if (!args_info
.config_file_given
) {
67 home
= para_homedir();
68 sprintf(default_config_file
, "%s/.paraslash/client.conf",
72 if (!args_info
.config_file_given
)
73 *config_file
= default_config_file
;
75 *config_file
= args_info
.config_file_arg
;
76 ret
= stat(*config_file
, &statbuf
);
77 if (ret
&& args_info
.config_file_given
) {
78 fprintf(stderr
, "can not stat config file %s\n",
79 args_info
.config_file_arg
);
83 cmdline_parser_configfile(*config_file
, &args_info
, 0, 0, 0);
84 if (!args_info
.key_file_given
)
85 *key_file
= default_key_file
;
87 *key_file
= args_info
.key_file_arg
;
90 static RC4_KEY rc4_recv_key
;
91 static RC4_KEY rc4_send_key
;
92 static unsigned char rc4_buf
[2 * RC4_KEY_LEN
];
94 static void rc4_send(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
96 RC4(&rc4_send_key
, len
, indata
, outdata
);
99 static void rc4_recv(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
)
101 RC4(&rc4_recv_key
, len
, indata
, outdata
);
103 void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
104 void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
107 static void append_str(char **data
, const char* append
)
110 char *tmp
= make_message("%s\n%s", *data
, append
);
114 *data
= para_strdup(append
);
120 int main(int argc
, char *argv
[])
123 int sockfd
, numbytes
, i
, received
, ret
;
125 struct sockaddr_in their_addr
;
126 char *command
= NULL
;
129 char *key_file
, *config_file
;
130 long unsigned challenge_nr
;
132 get_options(argc
, argv
, &config_file
, &key_file
);
133 if (args_info
.loglevel_arg
<= NOTICE
)
134 cmdline_parser_print_version();
136 "current loglevel: %d\n"
137 "using config_file: %s\n"
138 "using key_file: %s\n"
139 "connecting to %s:%d\n",
140 args_info
.loglevel_arg
,
143 args_info
.hostname_arg
,
144 args_info
.server_port_arg
146 if (!args_info
.inputs_num
) {
147 PARA_ERROR_LOG("%s", "syntax error\n");
151 for (i
= 0; i
< args_info
.inputs_num
; i
++)
152 append_str(&command
, args_info
.inputs
[i
]);
154 crypt_function_recv
= NULL
;
155 crypt_function_send
= NULL
;
156 /* get the host info */
157 PARA_NOTICE_LOG("getting host info of %s\n",
158 args_info
.hostname_arg
);
159 if (!(he
= get_host_info(args_info
.hostname_arg
)))
162 if ((sockfd
= get_socket()) < 0)
164 /* init their_addr */
165 init_sockaddr(&their_addr
, args_info
.server_port_arg
, he
);
167 PARA_NOTICE_LOG("connecting to %s...\n",
168 args_info
.hostname_arg
);
169 if (para_connect(sockfd
, &their_addr
) < 0)
171 /* Receive Welcome message */
172 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
174 /* send auth command */
175 auth_str
= make_message("auth %s%s", args_info
.plain_given
? "" : "rc4 ",
177 PARA_INFO_LOG("<-- %s--> %s\n", buf
, auth_str
);
178 if (send_buffer(sockfd
, auth_str
) < 0)
180 /* receive challenge number */
181 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
183 if (numbytes
!= 64) {
184 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
186 buf
[numbytes
] = '\0';
187 PARA_ERROR_LOG("received the following instead: %s\n", buf
);
190 PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes
);
191 /* decrypt challenge number */
192 ret
= para_decrypt_challenge(key_file
, &challenge_nr
, (unsigned char *) buf
,
195 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret
);
198 /* send decrypted challenge */
199 PARA_INFO_LOG("--> %lu\n", challenge_nr
);
200 if (send_va_buffer(sockfd
, "%s%lu", CHALLENGE_RESPONSE_MSG
, challenge_nr
) < 0)
202 /* Wait for approval */
203 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
204 if ((numbytes
= recv_buffer(sockfd
, buf
, sizeof(buf
))) < 0)
206 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
208 /* Check if server has sent "Proceed" message */
209 if (!strstr(buf
, PROCEED_MSG
)) {
210 PARA_EMERG_LOG("%s", "authentication failed\n");
213 if (numbytes
>= PROCEED_MSG_LEN
+ 32) {
214 PARA_INFO_LOG("%s", "decrypting session key\n");
215 if (para_decrypt_buffer(key_file
, rc4_buf
,
216 (unsigned char *)buf
+ PROCEED_MSG_LEN
+ 1,
217 numbytes
- PROCEED_MSG_LEN
- 1) < 0) {
218 PARA_EMERG_LOG("%s", "error receiving rc4 key\n");
221 RC4_set_key(&rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
222 RC4_set_key(&rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
223 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
224 rc4_buf
[0], rc4_buf
[1], rc4_buf
[2], rc4_buf
[3]);
225 crypt_function_recv
= rc4_recv
;
226 crypt_function_send
= rc4_send
;
229 PARA_INFO_LOG("--> %s\n", command
);
230 if (send_buffer(sockfd
, command
) < 0)
234 if (send_buffer(sockfd
, EOC_MSG
"\n") < 0)
236 PARA_NOTICE_LOG("%s", "command sent.\n");
238 while ((numbytes
= recv_bin_buffer(sockfd
, buf
, sizeof(buf
) - 1)) > 0) {
239 buf
[numbytes
] = '\0';
240 if (!received
&& strstr(buf
, AWAITING_DATA_MSG
)) {
241 PARA_NOTICE_LOG("%s", "sending stdin\n");
242 while ((ret
= read(STDIN_FILENO
, buf
,
244 if (send_bin_buffer(sockfd
, buf
, ret
) < 0)
247 PARA_NOTICE_LOG("%s", "closing connection\n");
252 if (write(STDOUT_FILENO
, buf
, numbytes
) != numbytes
)
256 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
258 return ret
>= 0? 0: 1;