client: rename "args_info" to "conf"
[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 #if 0
32 enum {CL_CONNECTED, CL_SENT_AUTH, CL_RECEIVED_CHALLENGE, CL_SENT_CH_RESPONSE,
33 CL_RECEIVED_PROCEED, CL_SENT_COMMAND, CL_SENDING_STDIN, CL_RECV_DATA};
34
35 struct private_client_data {
36 int status;
37 int fd;
38 struct client_conf *conf;
39 };
40 #endif
41
42 //struct gengetopt_conf conf;
43 struct client_args_info conf;
44
45 INIT_CLIENT_ERRLISTS;
46
47 /*
48 * client log function
49 */
50 void para_log(int ll, const char* fmt,...)
51 {
52 va_list argp;
53
54 /* ignore log message if loglevel is not high enough */
55 if (ll < conf.loglevel_arg)
56 return;
57 va_start(argp, fmt);
58 vfprintf(stderr, fmt, argp);
59 va_end(argp);
60 }
61
62 static int get_options(int argc, char *argv[],
63 char **config_file, char **key_file)
64 {
65 char *home;
66 static char default_key_file[_POSIX_PATH_MAX] = "";
67 static char default_config_file[_POSIX_PATH_MAX] = "";
68 struct stat statbuf;
69 int ret;
70
71 cmdline_parser(argc, argv, &conf);
72 if (!conf.user_given)
73 conf.user_arg = para_logname();
74 if (!conf.key_file_given) {
75 home = para_homedir();
76 sprintf(default_key_file, "%s/.paraslash/key.%s", home,
77 conf.user_arg);
78 free(home);
79 }
80 if (!conf.config_file_given) {
81 home = para_homedir();
82 sprintf(default_config_file, "%s/.paraslash/client.conf",
83 home);
84 free(home);
85 }
86 if (!conf.config_file_given)
87 *config_file = default_config_file;
88 else
89 *config_file = conf.config_file_arg;
90 ret = stat(*config_file, &statbuf);
91 if (ret && conf.config_file_given)
92 return -E_NO_CONFIG;
93 if (!ret)
94 cmdline_parser_configfile(*config_file, &conf, 0, 0, 0);
95 if (!conf.key_file_given)
96 *key_file = default_key_file;
97 else
98 *key_file = conf.key_file_arg;
99 return 1;
100 }
101
102 static RC4_KEY rc4_recv_key;
103 static RC4_KEY rc4_send_key;
104 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
105
106 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
107 {
108 RC4(&rc4_send_key, len, indata, outdata);
109 }
110
111 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
112 {
113 RC4(&rc4_recv_key, len, indata, outdata);
114 }
115 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
116 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
117
118
119 static void append_str(char **data, const char* append)
120 {
121 if (*data) {
122 char *tmp = make_message("%s\n%s", *data, append);
123 free(*data);
124 *data = tmp;
125 } else
126 *data = para_strdup(append);
127 }
128
129
130 static int send_stdin(int fd)
131 {
132 char buf[8192];
133 int ret;
134
135 PARA_NOTICE_LOG("%s", "sending stdin\n");
136 for (;;) {
137 ret = read(STDIN_FILENO, buf, sizeof(buf));
138 if (ret <= 0)
139 return ret;
140 ret = send_bin_buffer(fd, buf, ret);
141 if (ret < 0)
142 return ret;
143 }
144 return 1;
145 }
146
147 /*
148 * MAIN
149 */
150 int main(int argc, char *argv[])
151 {
152
153 int sockfd = -1, numbytes, i, received, ret;
154 struct hostent *he;
155 struct sockaddr_in their_addr;
156 char *command = NULL;
157 char buf[8192];
158 char *auth_str;
159 char *key_file, *config_file;
160 long unsigned challenge_nr;
161
162 ret = get_options(argc, argv, &config_file, &key_file);
163 if (ret < 0)
164 goto out;
165 if (conf.loglevel_arg <= NOTICE)
166 cmdline_parser_print_version();
167 PARA_INFO_LOG(
168 "current loglevel: %d\n"
169 "using config_file: %s\n"
170 "using key_file: %s\n"
171 "connecting to %s:%d\n",
172 conf.loglevel_arg,
173 config_file,
174 key_file,
175 conf.hostname_arg,
176 conf.server_port_arg
177 );
178 ret = - E_CLIENT_SYNTAX;
179 if (!conf.inputs_num)
180 goto out;
181 /* concat args */
182 for (i = 0; i < conf.inputs_num; i++)
183 append_str(&command, conf.inputs[i]);
184 crypt_function_recv = NULL;
185 crypt_function_send = NULL;
186 /* get the host info */
187 PARA_NOTICE_LOG("getting host info of %s\n",
188 conf.hostname_arg);
189 ret = get_host_info(conf.hostname_arg, &he);
190 if (ret < 0)
191 goto out;
192 /* get new socket */
193 ret = get_socket();
194 if (ret < 0)
195 goto out;
196 sockfd = ret;
197 /* init their_addr */
198 init_sockaddr(&their_addr, conf.server_port_arg, he);
199 /* connect */
200 PARA_NOTICE_LOG("connecting to %s\n", conf.hostname_arg);
201 ret = para_connect(sockfd, &their_addr);
202 if (ret < 0)
203 goto out;
204 /* receive welcome message */
205 ret = recv_buffer(sockfd, buf, sizeof(buf));
206 if (ret < 0)
207 goto out;
208 /* send auth command */
209 auth_str = make_message("auth %s%s", conf.plain_given? "" : "rc4 ",
210 conf.user_arg);
211 PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
212 ret = send_buffer(sockfd, auth_str);
213 if (ret < 0)
214 goto out;
215 /* receive challenge number */
216 ret = recv_buffer(sockfd, buf, sizeof(buf));
217 if (ret < 0)
218 goto out;
219 if (ret != 64) {
220 ret = -E_INVALID_CHALLENGE;
221 PARA_ERROR_LOG("received the following: %s\n", buf);
222 goto out;
223 }
224 PARA_INFO_LOG("%s", "<-- [challenge]\n");
225 /* decrypt challenge number */
226 ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf, 64);
227 if (ret < 0)
228 goto out;
229 /* send decrypted challenge */
230 PARA_INFO_LOG("--> %lu\n", challenge_nr);
231 ret = send_va_buffer(sockfd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr);
232 if (ret < 0)
233 goto out;
234 /* wait for approval */
235 PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
236 ret = recv_buffer(sockfd, buf, sizeof(buf));
237 if (ret < 0)
238 goto out;
239 numbytes = ret;
240 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
241 "info ++++\n", buf);
242 /* check if server has sent "Proceed" message */
243 ret = -E_CLIENT_AUTH;
244 if (!strstr(buf, PROCEED_MSG))
245 goto out;
246 if (numbytes >= PROCEED_MSG_LEN + 32) {
247 PARA_INFO_LOG("%s", "decrypting session key\n");
248 ret = para_decrypt_buffer(key_file, rc4_buf,
249 (unsigned char *)buf + PROCEED_MSG_LEN + 1,
250 numbytes - PROCEED_MSG_LEN - 1);
251 if (ret < 0)
252 goto out;
253 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
254 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
255 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
256 rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
257 crypt_function_recv = rc4_recv;
258 crypt_function_send = rc4_send;
259 }
260 /* send command */
261 PARA_INFO_LOG("--> %s\n", command);
262 ret = send_buffer(sockfd, command);
263 if (ret < 0)
264 goto out;
265 free(command);
266 command = NULL;
267 ret = send_buffer(sockfd, EOC_MSG "\n");
268 if (ret < 0)
269 goto out;
270 PARA_NOTICE_LOG("%s", "command sent.\n");
271 received = 0;
272 for (;;) {
273 ret = recv_bin_buffer(sockfd, buf, sizeof(buf) - 1);
274 if (ret <= 0) {
275 if (!ret)
276 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
277 goto out;
278 }
279 buf[ret] = '\0';
280 numbytes = ret;
281 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
282 ret = send_stdin(sockfd);
283 goto out;
284 }
285 received = 1;
286 ret = write(STDOUT_FILENO, buf, numbytes);
287 if (ret != numbytes) {
288 ret = -E_SHORT_CLIENT_WRITE;
289 goto out;
290 }
291 }
292 out:
293 if (sockfd >= 0)
294 close(sockfd);
295 if (ret < 0)
296 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
297 return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
298 }