some audiod fixes
[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         if (ll < WARNING)
44                 outfd = stdout;
45         else
46                 outfd = stderr;
47         va_start(argp, fmt);
48         vfprintf(stdout, fmt, argp);
49         va_end(argp);
50 }
51
52 void get_options(int argc, char *argv[],
53         char **config_file, char **key_file)
54 {
55         char *home;
56         static char default_key_file[_POSIX_PATH_MAX] = "";
57         static char default_config_file[_POSIX_PATH_MAX] = "";
58         struct stat statbuf;
59         int ret;
60
61         cmdline_parser(argc, argv, &args_info);
62         if (!args_info.user_given)
63                 args_info.user_arg = para_logname();
64         if (!args_info.key_file_given) {
65                 home = para_homedir();
66                 sprintf(default_key_file, "%s/.paraslash/key.%s", home,
67                         args_info.user_arg);
68                 free(home);
69         }
70         if (!args_info.config_file_given) {
71                 home = para_homedir();
72                 sprintf(default_config_file, "%s/.paraslash/client.conf",
73                         home);
74                 free(home);
75         }
76         if (!args_info.config_file_given)
77                 *config_file = default_config_file;
78         else
79                 *config_file = args_info.config_file_arg;
80         ret = stat(*config_file, &statbuf);
81         if (ret && args_info.config_file_given) {
82                 fprintf(stderr, "can not stat config file %s\n",
83                         args_info.config_file_arg);
84                 exit(EXIT_FAILURE);
85         }
86         if (!ret)
87                 cmdline_parser_configfile(*config_file, &args_info, 0, 0, 0);
88         if (!args_info.key_file_given)
89                 *key_file = default_key_file;
90         else
91                 *key_file = args_info.key_file_arg;
92 }
93
94 static RC4_KEY rc4_recv_key;
95 static RC4_KEY rc4_send_key;
96 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
97
98 static void rc4_send(unsigned long len, const unsigned char *indata, unsigned char *outdata)
99 {
100         RC4(&rc4_send_key, len, indata, outdata);
101 }
102
103 static void rc4_recv(unsigned long len, const unsigned char *indata, unsigned char *outdata)
104 {
105         RC4(&rc4_recv_key, len, indata, outdata);
106 }
107 void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
108 void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata);
109
110
111 static void append_str(char **data, const char* append)
112 {
113         if (*data) {
114                 char *tmp = make_message("%s\n%s", *data, append);
115                 free(*data);
116                 *data = tmp;
117         } else
118                 *data = para_strdup(append);
119 }
120
121 /*
122  * MAIN
123  */
124 int main(int argc, char *argv[])
125 {
126
127         int sockfd, numbytes, i, received, ret;
128         struct hostent *he;
129         struct sockaddr_in their_addr;
130         char *command = NULL;
131         char buf[8192];
132         char *auth_str;
133         char *key_file, *config_file;
134         long unsigned challenge_nr;
135
136         get_options(argc, argv, &config_file, &key_file);
137         if (args_info.loglevel_arg <= NOTICE)
138                 cmdline_parser_print_version();
139         PARA_INFO_LOG(
140                 "current loglevel: %d\n"
141                 "using config_file: %s\n"
142                 "using key_file: %s\n"
143                 "connecting to %s:%d\n",
144                 args_info.loglevel_arg,
145                 config_file,
146                 key_file,
147                 args_info.hostname_arg,
148                 args_info.server_port_arg
149         );
150         if (!args_info.inputs_num) {
151                 PARA_ERROR_LOG("%s", "syntax error\n");
152                 exit(EXIT_FAILURE);
153         }
154         /* concat args */
155         for (i = 0; i < args_info.inputs_num; i++)
156                 append_str(&command, args_info.inputs[i]);
157
158         crypt_function_recv = NULL;
159         crypt_function_send = NULL;
160         /* get the host info */
161         PARA_NOTICE_LOG("getting host info of %s\n",
162                 args_info.hostname_arg);
163         if (!(he = get_host_info(args_info.hostname_arg)))
164                 exit(EXIT_FAILURE);
165         /* get new socket */
166         if ((sockfd = get_socket()) < 0)
167                 exit(EXIT_FAILURE);
168         /* init their_addr */
169         init_sockaddr(&their_addr, args_info.server_port_arg, he);
170         /* Connect */
171         PARA_NOTICE_LOG("connecting to %s...\n",
172                 args_info.hostname_arg);
173         if (para_connect(sockfd, &their_addr) < 0)
174                 exit(EXIT_FAILURE);
175         /* Receive Welcome message */
176         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
177                 exit(EXIT_FAILURE);
178         /* send auth command */
179         auth_str = make_message("auth %s%s", args_info.plain_given?  "" : "rc4 ",
180                 args_info.user_arg);
181         PARA_INFO_LOG("<-- %s--> %s\n", buf, auth_str);
182         if (send_buffer(sockfd, auth_str) < 0)
183                 exit(EXIT_FAILURE);
184         /* receive challenge number */
185         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
186                 exit(EXIT_FAILURE);
187         if (numbytes != 64) {
188                 PARA_EMERG_LOG("did not receive valid challenge (got %i bytes)\n",
189                         numbytes);
190                 buf[numbytes] = '\0';
191                 PARA_ERROR_LOG("received the following instead: %s\n", buf);
192                 exit(EXIT_FAILURE);
193         }
194         PARA_INFO_LOG("<-- [challenge (%i bytes)]\n", numbytes);
195         /* decrypt challenge number */
196         ret = para_decrypt_challenge(key_file, &challenge_nr, (unsigned char *) buf,
197                 numbytes);
198         if (ret < 0) {
199                 PARA_EMERG_LOG("decrypt error (%d). Bad secret key?\n", ret);
200                 exit(EXIT_FAILURE);
201         }
202         /* send decrypted challenge */
203         PARA_INFO_LOG("--> %lu\n", challenge_nr);
204         if (send_va_buffer(sockfd, "%s%lu", CHALLENGE_RESPONSE_MSG, challenge_nr) < 0)
205                 exit(EXIT_FAILURE);
206         /* Wait for approval */
207         PARA_NOTICE_LOG("%s", "waiting for approval from server\n");
208         if ((numbytes = recv_buffer(sockfd, buf, sizeof(buf))) < 0)
209                 exit(EXIT_FAILURE);
210         PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
211                 "info ++++\n", buf);
212         /* Check if server has sent "Proceed" message */
213         if (!strstr(buf, PROCEED_MSG)) {
214                 PARA_EMERG_LOG("%s", "authentication failed\n");
215                 exit(EXIT_FAILURE);
216         }
217         if (numbytes >= PROCEED_MSG_LEN + 32) {
218                 PARA_INFO_LOG("%s", "decrypting session key\n");
219                 if (para_decrypt_buffer(key_file, rc4_buf,
220                                 (unsigned char *)buf + PROCEED_MSG_LEN + 1,
221                                 numbytes - PROCEED_MSG_LEN - 1) < 0) {
222                         PARA_EMERG_LOG("%s", "error receiving rc4 key\n");
223                         exit(EXIT_FAILURE);
224                 }
225                 RC4_set_key(&rc4_send_key, RC4_KEY_LEN, rc4_buf);
226                 RC4_set_key(&rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
227                 PARA_INFO_LOG("rc4 encrytion activated: %x:%x:%x:%x\n",
228                         rc4_buf[0], rc4_buf[1], rc4_buf[2], rc4_buf[3]);
229                 crypt_function_recv = rc4_recv;
230                 crypt_function_send = rc4_send;
231         }
232         /* send command */
233         PARA_INFO_LOG("--> %s\n", command);
234         if (send_buffer(sockfd, command) < 0)
235                 exit(EXIT_FAILURE);
236         free(command);
237         command = NULL;
238         if (send_buffer(sockfd, EOC_MSG "\n") < 0)
239                 exit(EXIT_FAILURE);
240         PARA_NOTICE_LOG("%s", "command sent.\n");
241         received = 0;
242         while ((numbytes = recv_bin_buffer(sockfd, buf, sizeof(buf) - 1)) > 0) {
243                 buf[numbytes] = '\0';
244                 if (!received && strstr(buf, AWAITING_DATA_MSG)) {
245                         PARA_NOTICE_LOG("%s", "sending stdin\n");
246                         while ((ret = read(STDIN_FILENO, buf,
247                                         sizeof(buf))) > 0) {
248                                 if (send_bin_buffer(sockfd, buf, ret) < 0)
249                                         break;
250                         }
251                         PARA_NOTICE_LOG("%s", "closing connection\n");
252                         numbytes = 1;
253                         break;
254                 }
255                 received = 1;
256                 if (write(STDOUT_FILENO, buf, numbytes) != numbytes)
257                         break;
258         }
259         if (!numbytes)
260                 PARA_NOTICE_LOG("%s", "connection closed by peer\n");
261         close(sockfd);
262         return ret >= 0? 0: 1;
263 }