unify web pages
[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 #include "error.h"
30
31 struct gengetopt_args_info args_info;
32
33 INIT_CLIENT_ERRLISTS;
34
35 /*
36 * client log function
37 */
38 void para_log(int ll, const char* fmt,...)
39 {
40 va_list argp;
41
42 /* ignore log message if loglevel is not high enough */
43 if (ll < args_info.loglevel_arg)
44 return;
45 va_start(argp, fmt);
46 vfprintf(stderr, fmt, argp);
47 va_end(argp);
48 }
49
50 static int get_options(int argc, char *argv[],
51 char **config_file, char **key_file)
52 {
53 char *home;
54 static char default_key_file[_POSIX_PATH_MAX] = "";
55 static char default_config_file[_POSIX_PATH_MAX] = "";
56 struct stat statbuf;
57 int ret;
58
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,
65 args_info.user_arg);
66 free(home);
67 }
68 if (!args_info.config_file_given) {
69 home = para_homedir();
70 sprintf(default_config_file, "%s/.paraslash/client.conf",
71 home);
72 free(home);
73 }
74 if (!args_info.config_file_given)
75 *config_file = default_config_file;
76 else
77 *config_file = args_info.config_file_arg;
78 ret = stat(*config_file, &statbuf);
79 if (ret && args_info.config_file_given)
80 return -E_NO_CONFIG;
81 if (!ret)
82 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
83 if (!args_info.key_file_given)
84 *key_file = default_key_file;
85 else
86 *key_file = args_info.key_file_arg;
87 return 1;
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 static int send_stdin(int fd)
119 {
120 char buf[8192];
121 int ret;
122
123 PARA_NOTICE_LOG("%s", "sending stdin\n");
124 for (;;) {
125 ret = read(STDIN_FILENO, buf, sizeof(buf));
126 if (ret <= 0)
127 return ret;
128 ret = send_bin_buffer(fd, buf, ret);
129 if (ret < 0)
130 return ret;
131 }
132 return 1;
133 }
134 /*
135 * MAIN
136 */
137 int main(int argc, char *argv[])
138 {
139
140 int sockfd = -1, numbytes, i, received, ret;
141 struct hostent *he;
142 struct sockaddr_in their_addr;
143 char *command = NULL;
144 char buf[8192];
145 char *auth_str;
146 char *key_file, *config_file;
147 long unsigned challenge_nr;
148
149 ret = get_options(argc, argv, &config_file, &key_file);
150 if (ret < 0)
151 goto out;
152 if (args_info.loglevel_arg <= NOTICE)
153 cmdline_parser_print_version();
154 PARA_INFO_LOG(
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,
160 config_file,
161 key_file,
162 args_info.hostname_arg,
163 args_info.server_port_arg
164 );
165 ret = - E_CLIENT_SYNTAX;
166 if (!args_info.inputs_num)
167 goto out;
168 /* concat args */
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);
177 if (ret < 0)
178 goto out;
179 /* get new socket */
180 ret = get_socket();
181 if (ret < 0)
182 goto out;
183 sockfd = ret;
184 /* init their_addr */
185 init_sockaddr(&their_addr, args_info.server_port_arg, he);
186 /* connect */
187 PARA_NOTICE_LOG("connecting to %s\n", args_info.hostname_arg);
188 ret = para_connect(sockfd, &their_addr);
189 if (ret < 0)
190 goto out;
191 /* receive welcome message */
192 ret = recv_buffer(sockfd, buf, sizeof(buf));
193 if (ret < 0)
194 goto out;
195 /* send auth command */
196 auth_str = make_message("auth %s%s", args_info.plain_given? "" : "rc4 ",
197 args_info.user_arg);
198 PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
199 ret = send_buffer(sockfd, auth_str);
200 if (ret < 0)
201 goto out;
202 /* receive challenge number */
203 ret = recv_buffer(sockfd, buf, sizeof(buf));
204 if (ret < 0)
205 goto out;
206 if (ret != 64) {
207 ret = -E_INVALID_CHALLENGE;
208 PARA_ERROR_LOG("received the following: %s\n", buf);
209 goto out;
210 }
211 PARA_INFO_LOG("%s", "<-- [challenge]\n");
212 /* decrypt challenge number */
213 ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf, 64);
214 if (ret < 0)
215 goto out;
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);
219 if (ret < 0)
220 goto out;
221 /* wait for approval */
222 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
223 ret = recv_buffer(sockfd, buf, sizeof(buf));
224 if (ret < 0)
225 goto out;
226 numbytes = ret;
227 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
228 "info ++++\n", buf);
229 /* check if server has sent "Proceed" message */
230 ret = -E_CLIENT_AUTH;
231 if (!strstr(buf, PROCEED_MSG))
232 goto out;
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);
238 if (ret < 0)
239 goto out;
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;
246 }
247 /* send command */
248 PARA_INFO_LOG("--> %s\n", command);
249 ret = send_buffer(sockfd, command);
250 if (ret < 0)
251 goto out;
252 free(command);
253 command = NULL;
254 ret = send_buffer(sockfd, EOC_MSG "\n");
255 if (ret < 0)
256 goto out;
257 PARA_NOTICE_LOG("%s", "command sent.\n");
258 received = 0;
259 for (;;) {
260 ret = recv_bin_buffer(sockfd, buf, sizeof(buf) - 1);
261 if (ret <= 0) {
262 if (!ret)
263 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
264 goto out;
265 }
266 buf[ret] = '\0';
267 numbytes = ret;
268 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
269 ret = send_stdin(sockfd);
270 goto out;
271 }
272 received = 1;
273 ret = write(STDOUT_FILENO, buf, numbytes);
274 if (ret != numbytes) {
275 ret = -E_SHORT_CLIENT_WRITE;
276 goto out;
277 }
278 }
279 out:
280 if (sockfd >= 0)
281 close(sockfd);
282 if (ret < 0)
283 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
284 return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
285 }