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 struct gengetopt_args_info args_info;
38 void para_log(int ll, const char* fmt,...)
42 /* ignore log message if loglevel is not high enough */
43 if (ll < args_info.loglevel_arg)
46 vfprintf(stderr, fmt, argp);
50 static int get_options(int argc, char *argv[],
51 char **config_file, char **key_file)
54 static char default_key_file[_POSIX_PATH_MAX] = "";
55 static char default_config_file[_POSIX_PATH_MAX] = "";
59 cmdline_parser(argc, argv, &args_info);
60 if (!args_info.user_given)
61 args_info.user_arg = para_logname();
62 if (!args_info.key_file_given) {
63 home = para_homedir();
64 sprintf(default_key_file, "%s/.paraslash/key.%s", home,
68 if (!args_info.config_file_given) {
69 home = para_homedir();
70 sprintf(default_config_file, "%s/.paraslash/client.conf",
74 if (!args_info.config_file_given)
75 *config_file = default_config_file;
77 *config_file = args_info.config_file_arg;
78 ret = stat(*config_file, &statbuf);
79 if (ret && args_info.config_file_given)
82 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
83 if (!args_info.key_file_given)
84 *key_file = default_key_file;
86 *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);
118 static int send_stdin(int fd)
123 PARA_NOTICE_LOG("%s", "sending stdin\n");
125 ret = read(STDIN_FILENO, buf, sizeof(buf));
128 ret = send_bin_buffer(fd, buf, ret);
137 int main(int argc, char *argv[])
140 int sockfd = -1, numbytes, i, received, ret;
142 struct sockaddr_in their_addr;
143 char *command = NULL;
146 char *key_file, *config_file;
147 long unsigned challenge_nr;
149 ret = get_options(argc, argv, &config_file, &key_file);
152 if (args_info.loglevel_arg <= NOTICE)
153 cmdline_parser_print_version();
155 "current loglevel: %d\n"
156 "using config_file: %s\n"
157 "using key_file: %s\n"
158 "connecting to %s:%d\n",
159 args_info.loglevel_arg,
162 args_info.hostname_arg,
163 args_info.server_port_arg
165 ret = - E_CLIENT_SYNTAX;
166 if (!args_info.inputs_num)
169 for (i = 0; i < args_info.inputs_num; i++)
170 append_str(&command, args_info.inputs[i]);
171 crypt_function_recv = NULL;
172 crypt_function_send = NULL;
173 /* get the host info */
174 PARA_NOTICE_LOG("getting host info of %s\n",
175 args_info.hostname_arg);
176 ret = get_host_info(args_info.hostname_arg, &he);
184 /* init their_addr */
185 init_sockaddr(&their_addr, args_info.server_port_arg, he);
187 PARA_NOTICE_LOG("connecting to %s\n", args_info.hostname_arg);
188 ret = para_connect(sockfd, &their_addr);
191 /* receive welcome message */
192 ret = recv_buffer(sockfd, buf, sizeof(buf));
195 /* send auth command */
196 auth_str = make_message("auth %s%s", args_info.plain_given? "" : "rc4 ",
198 PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
199 ret = send_buffer(sockfd, auth_str);
202 /* receive challenge number */
203 ret = recv_buffer(sockfd, buf, sizeof(buf));
207 ret = -E_INVALID_CHALLENGE;
208 PARA_ERROR_LOG("received the following: %s\n", buf);
211 PARA_INFO_LOG("%s", "<-- [challenge]\n");
212 /* decrypt challenge number */
213 ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf, 64);
216 /* send decrypted challenge */
217 PARA_INFO_LOG("--> %lu\n", challenge_nr);
218 ret = send_va_buffer(sockfd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr);
221 /* wait for approval */
222 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
223 ret = recv_buffer(sockfd, buf, sizeof(buf));
227 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
229 /* check if server has sent "Proceed" message */
230 ret = -E_CLIENT_AUTH;
231 if (!strstr(buf, PROCEED_MSG))
233 if (numbytes >= PROCEED_MSG_LEN + 32) {
234 PARA_INFO_LOG("%s", "decrypting session key\n");
235 ret = para_decrypt_buffer(key_file, rc4_buf,
236 (unsigned char *)buf + PROCEED_MSG_LEN + 1,
237 numbytes - PROCEED_MSG_LEN - 1);
240 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
241 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
242 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
243 rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
244 crypt_function_recv = rc4_recv;
245 crypt_function_send = rc4_send;
248 PARA_INFO_LOG("--> %s\n", command);
249 ret = send_buffer(sockfd, command);
254 ret = send_buffer(sockfd, EOC_MSG "\n");
257 PARA_NOTICE_LOG("%s", "command sent.\n");
260 ret = recv_bin_buffer(sockfd, buf, sizeof(buf) - 1);
263 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
268 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
269 ret = send_stdin(sockfd);
273 ret = write(STDOUT_FILENO, buf, numbytes);
274 if (ret != numbytes) {
275 ret = -E_SHORT_CLIENT_WRITE;
283 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
284 return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;