914c5fb93d4a58d4cf79ec75475da4d3ccb73640
[paraslash.git] / client.c
1 /*
2 * Copyright (C) 1997-2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file client.c the client program used to connect to para_server */
20
21 #include "para.h"
22 #include "config.h"
23 #include "client.cmdline.h"
24 #include "crypt.h"
25 #include "rc4.h"
26 #include <openssl/rc4.h>
27 #include "net.h"
28 #include "string.h"
29
30 struct gengetopt_args_info args_info;
31
32 /*
33 * client log function
34 */
35 void para_log(int ll, const char* fmt,...)
36 {
37 va_list argp;
38 FILE *outfd;
39
40 /* ignore log message if loglevel is not high enough */
41 if (ll < args_info.loglevel_arg)
42 return;
43 va_start(argp, fmt);
44 vfprintf(stderr, fmt, argp);
45 va_end(argp);
46 }
47
48 void get_options(int argc, char *argv[],
49 char **config_file, char **key_file)
50 {
51 char *home;
52 static char default_key_file[_POSIX_PATH_MAX] = "";
53 static char default_config_file[_POSIX_PATH_MAX] = "";
54 struct stat statbuf;
55 int ret;
56
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,
63 args_info.user_arg);
64 free(home);
65 }
66 if (!args_info.config_file_given) {
67 home = para_homedir();
68 sprintf(default_config_file, "%s/.paraslash/client.conf",
69 home);
70 free(home);
71 }
72 if (!args_info.config_file_given)
73 *config_file = default_config_file;
74 else
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);
80 exit(EXIT_FAILURE);
81 }
82 if (!ret)
83 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
84 if (!args_info.key_file_given)
85 *key_file = default_key_file;
86 else
87 *key_file = args_info.key_file_arg;
88 }
89
90 static RC4_KEY rc4_recv_key;
91 static RC4_KEY rc4_send_key;
92 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
93
94 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
95 {
96 RC4(&rc4_send_key, len, indata, outdata);
97 }
98
99 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
100 {
101 RC4(&rc4_recv_key, len, indata, outdata);
102 }
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);
105
106
107 static void append_str(char **data, const char* append)
108 {
109 if (*data) {
110 char *tmp = make_message("%s\n%s", *data, append);
111 free(*data);
112 *data = tmp;
113 } else
114 *data = para_strdup(append);
115 }
116
117 /*
118 * MAIN
119 */
120 int main(int argc, char *argv[])
121 {
122
123 int sockfd, numbytes, i, received, ret;
124 struct hostent *he;
125 struct sockaddr_in their_addr;
126 char *command = NULL;
127 char buf[8192];
128 char *auth_str;
129 char *key_file, *config_file;
130 long unsigned challenge_nr;
131
132 get_options(argc, argv, &config_file, &key_file);
133 if (args_info.loglevel_arg <= NOTICE)
134 cmdline_parser_print_version();
135 PARA_INFO_LOG(
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,
141 config_file,
142 key_file,
143 args_info.hostname_arg,
144 args_info.server_port_arg
145 );
146 if (!args_info.inputs_num) {
147 PARA_ERROR_LOG("%s", "syntax error\n");
148 exit(EXIT_FAILURE);
149 }
150 /* concat args */
151 for (i = 0; i < args_info.inputs_num; i++)
152 append_str(&command, args_info.inputs[i]);
153
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)))
160 exit(EXIT_FAILURE);
161 /* get new socket */
162 if ((sockfd = get_socket()) < 0)
163 exit(EXIT_FAILURE);
164 /* init their_addr */
165 init_sockaddr(&their_addr, args_info.server_port_arg, he);
166 /* Connect */
167 PARA_NOTICE_LOG("connecting to %s...\n",
168 args_info.hostname_arg);
169 if (para_connect(sockfd, &their_addr) < 0)
170 exit(EXIT_FAILURE);
171 /* Receive Welcome message */
172 if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
173 exit(EXIT_FAILURE);
174 /* send auth command */
175 auth_str = make_message("auth %s%s", args_info.plain_given? "" : "rc4 ",
176 args_info.user_arg);
177 PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
178 if (send_buffer(sockfd, auth_str) < 0)
179 exit(EXIT_FAILURE);
180 /* receive challenge number */
181 if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
182 exit(EXIT_FAILURE);
183 if (numbytes != 64) {
184 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
185 numbytes);
186 buf[numbytes] = '\0';
187 PARA_ERROR_LOG("received the following instead: %s\n", buf);
188 exit(EXIT_FAILURE);
189 }
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,
193 numbytes);
194 if (ret < 0) {
195 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret);
196 exit(EXIT_FAILURE);
197 }
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)
201 exit(EXIT_FAILURE);
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)
205 exit(EXIT_FAILURE);
206 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
207 "info ++++\n", buf);
208 /* Check if server has sent "Proceed" message */
209 if (!strstr(buf, PROCEED_MSG)) {
210 PARA_EMERG_LOG("%s", "authentication failed\n");
211 exit(EXIT_FAILURE);
212 }
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");
219 exit(EXIT_FAILURE);
220 }
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;
227 }
228 /* send command */
229 PARA_INFO_LOG("--> %s\n", command);
230 if (send_buffer(sockfd, command) < 0)
231 exit(EXIT_FAILURE);
232 free(command);
233 command = NULL;
234 if (send_buffer(sockfd, EOC_MSG "\n") < 0)
235 exit(EXIT_FAILURE);
236 PARA_NOTICE_LOG("%s", "command sent.\n");
237 received = 0;
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,
243 sizeof(buf))) > 0) {
244 if (send_bin_buffer(sockfd, buf, ret) < 0)
245 break;
246 }
247 PARA_NOTICE_LOG("%s", "closing connection\n");
248 numbytes = 1;
249 break;
250 }
251 received = 1;
252 if (write(STDOUT_FILENO, buf, numbytes) != numbytes)
253 break;
254 }
255 if (!numbytes)
256 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
257 close(sockfd);
258 return ret >= 0? 0: 1;
259 }