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 <readline/readline.h>
24 #include <readline/history.h>
25 #include "client.cmdline.h"
28 #include <openssl/rc4.h>
32 /* A static variable for holding the line. */
33 static char *line_read;
35 struct gengetopt_args_info args_info;
40 void para_log(int ll, char* fmt,...)
45 /* ignore log message if loglevel is not high enough */
46 if (ll < args_info.loglevel_arg)
53 vfprintf(stdout, fmt, argp);
58 * Read a string, and return a pointer to it. Returns NULL on EOF.
60 static char *rl_gets(void)
63 /* Get a line from the user. */
64 line_read = readline("para_client> ");
65 /* If the line has any text in it, save it on the history. */
66 if (line_read && *line_read)
67 add_history(line_read);
72 * do several cleanups on sigint
74 static void sigint_handler(__unused int i)
76 rl_cleanup_after_signal();
77 rl_reset_after_signal();
80 void get_options(int argc, char *argv[],
81 char **config_file, char **key_file)
84 static char default_key_file[_POSIX_PATH_MAX] = "";
85 static char default_config_file[_POSIX_PATH_MAX] = "";
89 cmdline_parser(argc, argv, &args_info);
90 if (!args_info.user_given)
91 args_info.user_arg = para_logname();
92 if (!args_info.key_file_given) {
93 home = para_homedir();
94 sprintf(default_key_file, "%s/.paraslash/key.%s", home,
98 if (!args_info.config_file_given) {
99 home = para_homedir();
100 sprintf(default_config_file, "%s/.paraslash/client.conf",
104 if (!args_info.config_file_given)
105 *config_file = default_config_file;
107 *config_file = args_info.config_file_arg;
108 ret = stat(*config_file, &statbuf);
109 if (ret && args_info.config_file_given) {
110 fprintf(stderr, "can not stat config file %s\n",
111 args_info.config_file_arg);
115 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
116 if (!args_info.key_file_given)
117 *key_file = default_key_file;
119 *key_file = args_info.key_file_arg;
122 static RC4_KEY rc4_recv_key;
123 static RC4_KEY rc4_send_key;
124 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
126 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
128 RC4(&rc4_send_key, len, indata, outdata);
131 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
133 RC4(&rc4_recv_key, len, indata, outdata);
135 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
136 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
139 static void append_str(char **data, const char* append)
142 char *tmp = make_message("%s\n%s", *data, append);
146 *data = para_strdup(append);
152 int main(int argc, char *argv[])
155 int sockfd, numbytes, i, interactive, received, ret;
157 struct sockaddr_in their_addr;
158 char *command = NULL;
161 char *key_file, *config_file;
162 long unsigned challenge_nr;
165 get_options(argc, argv, &config_file, &key_file);
166 if (args_info.loglevel_arg <= NOTICE)
167 cmdline_parser_print_version();
169 "current loglevel: %d\n"
170 "using config_file: %s\n"
171 "using key_file: %s\n"
172 "connecting to %s:%d\n",
173 args_info.loglevel_arg,
176 args_info.hostname_arg,
177 args_info.server_port_arg
179 interactive = args_info.inputs_num == 0? 1 : 0;
181 PARA_NOTICE_LOG("%s", "no command, entering interactive mode\n");
182 signal(SIGINT, sigint_handler);
184 /* not interactive, concat args */
185 for (i = 0; i < args_info.inputs_num; i++)
186 append_str(&command, args_info.inputs[i]);
189 crypt_function_recv = NULL;
190 crypt_function_send = NULL;
197 rl_kill_full_line(0, 0);
198 rl_free_line_state();
199 /* read a line via readline */
204 goto interactive_loop;
206 while (sscanf(p, "%200s%n", buf, &i) == 1) {
207 append_str(&command, buf);
211 /* get the host info */
212 PARA_NOTICE_LOG("getting host info of %s\n",
213 args_info.hostname_arg);
214 if (!(he = get_host_info(args_info.hostname_arg)))
217 if ((sockfd = get_socket()) < 0)
219 /* init their_addr */
220 init_sockaddr(&their_addr, args_info.server_port_arg, he);
222 PARA_NOTICE_LOG("connecting to %s...\n",
223 args_info.hostname_arg);
224 if (para_connect(sockfd, &their_addr) < 0)
226 /* Receive Welcome message */
227 if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
229 /* send auth command */
230 auth_str = make_message("auth %s%s", args_info.plain_given? "" : "rc4 ",
232 PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
233 if (send_buffer(sockfd, auth_str) < 0)
235 /* receive challenge number */
236 if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
238 if (numbytes != 64) {
239 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
241 buf[numbytes] = '\0';
242 PARA_ERROR_LOG("received the following instead: %s\n", buf);
245 PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes);
246 /* decrypt challenge number */
247 ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf,
250 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret);
253 /* send decrypted challenge */
254 PARA_INFO_LOG("--> %lu\n", challenge_nr);
255 if (send_va_buffer(sockfd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr) < 0)
257 /* Wait for approval */
258 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
259 if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
261 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
263 /* Check if server has sent "Proceed" message */
264 if (!strstr(buf, PROCEED_MSG)) {
265 PARA_EMERG_LOG("%s", "authentication failed\n");
268 if (numbytes >= PROCEED_MSG_LEN + 32) {
269 PARA_INFO_LOG("%s", "decrypting session key\n");
270 if (para_decrypt_buffer(key_file, rc4_buf,
271 (unsigned char *)buf + PROCEED_MSG_LEN + 1,
272 numbytes - PROCEED_MSG_LEN - 1) < 0) {
273 PARA_EMERG_LOG("%s", "error receiving rc4 key\n");
276 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
277 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
278 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
279 rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
280 crypt_function_recv = rc4_recv;
281 crypt_function_send = rc4_send;
284 PARA_INFO_LOG("--> %s\n", command);
285 if (send_buffer(sockfd, command) < 0)
289 if (send_buffer(sockfd, EOC_MSG "\n") < 0)
291 PARA_NOTICE_LOG("%s", "command sent.\n");
294 /* write server output to stdout */
295 while ((numbytes = recv_bin_buffer(sockfd, buf, sizeof(buf))) > 0) {
298 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
299 PARA_NOTICE_LOG("%s", "<-- awaiting data\n");
300 PARA_NOTICE_LOG("%s", "--> sending stdin\n");
301 while ((ret = read(STDIN_FILENO, buf,
303 send_bin_buffer(sockfd, buf, ret);
304 PARA_NOTICE_LOG("%s", "closing connection\n");
309 if (write(STDOUT_FILENO, buf, numbytes) != numbytes)
313 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
316 goto interactive_loop;