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_common.c common functions of para_client and para_audiod */
24 #include "client.cmdline.h"
30 #include "client.cmdline.h"
34 void rc4_send(unsigned long len
, const unsigned char *indata
,
35 unsigned char *outdata
, void *private_data
)
37 struct private_client_data
*pcd
= private_data
;
38 RC4(&pcd
->rc4_send_key
, len
, indata
, outdata
);
41 void rc4_recv(unsigned long len
, const unsigned char *indata
,
42 unsigned char *outdata
, void *private_data
)
44 struct private_client_data
*pcd
= private_data
;
45 RC4(&pcd
->rc4_recv_key
, len
, indata
, outdata
);
49 void client_close(struct private_client_data
*pcd
)
56 free(pcd
->config_file
);
61 int client_parse_config(int argc
, char *argv
[],
62 struct private_client_data
**pcd_ptr
)
64 char *home
= para_homedir();
67 struct private_client_data
*pcd
=
68 para_calloc(sizeof(struct private_client_data
));
71 client_cmdline_parser(argc
, argv
, &pcd
->conf
);
72 ret
= - E_CLIENT_SYNTAX
;
73 if (!pcd
->conf
.inputs_num
)
75 pcd
->user
= pcd
->conf
.user_given
?
76 para_strdup(pcd
->conf
.user_arg
) : para_logname();
78 pcd
->key_file
= pcd
->conf
.key_file_given
?
79 para_strdup(pcd
->conf
.key_file_arg
) :
80 make_message("%s/.paraslash/key.%s", home
, pcd
->user
);
82 pcd
->config_file
= pcd
->conf
.config_file_given
?
83 para_strdup(pcd
->conf
.config_file_arg
) :
84 make_message("%s/.paraslash/client.conf", home
);
85 ret
= stat(pcd
->config_file
, &statbuf
);
86 if (ret
&& pcd
->conf
.config_file_given
) {
91 client_cmdline_parser_configfile(pcd
->config_file
,
96 "current loglevel: %d\n"
97 "using config_file: %s\n"
98 "using key_file: %s\n"
99 "connecting to %s:%d\n" ,
100 pcd
->conf
.loglevel_arg
,
103 pcd
->conf
.hostname_arg
, pcd
->conf
.server_port_arg
112 void client_pre_select(struct sched
*s
, struct task
*t
)
114 struct private_client_data
*pcd
= t
->private_data
;
121 switch (pcd
->status
) {
124 case CL_SENT_CH_RESPONSE
:
125 case CL_SENT_COMMAND
:
126 para_fd_set(pcd
->fd
, &s
->rfds
, &s
->max_fileno
);
130 case CL_RECEIVED_WELCOME
:
131 case CL_RECEIVED_CHALLENGE
:
132 case CL_RECEIVED_PROCEED
:
133 para_fd_set(pcd
->fd
, &s
->wfds
, &s
->max_fileno
);
137 case CL_RECEIVING_SERVER_OUTPUT
:
138 if (pcd
->loaded
< CLIENT_BUFSIZE
- 1) {
139 para_fd_set(pcd
->fd
, &s
->rfds
, &s
->max_fileno
);
143 case CL_SENDING_STDIN
:
144 if (*pcd
->in_loaded
) {
145 PARA_INFO_LOG("loaded: %zd\n", *pcd
->in_loaded
);
146 para_fd_set(pcd
->fd
, &s
->wfds
, &s
->max_fileno
);
150 t
->ret
= -E_INPUT_EOF
;
151 s
->timeout
.tv_sec
= 0;
152 s
->timeout
.tv_usec
= 1;
159 static ssize_t
client_recv_buffer(struct private_client_data
*pcd
)
161 ssize_t ret
= recv_buffer(pcd
->fd
, pcd
->buf
+ pcd
->loaded
,
162 CLIENT_BUFSIZE
- pcd
->loaded
);
164 return -E_SERVER_EOF
;
171 void client_post_select(struct sched
*s
, struct task
*t
)
173 struct private_client_data
*pcd
= t
->private_data
;
175 // PARA_INFO_LOG("status %d\n", pcd->status);
179 if (!pcd
->check_r
&& !pcd
->check_w
)
181 if (pcd
->check_r
&& !FD_ISSET(pcd
->fd
, &s
->rfds
))
183 if (pcd
->check_w
&& !FD_ISSET(pcd
->fd
, &s
->wfds
))
185 switch (pcd
->status
) {
186 case CL_CONNECTED
: /* receive welcome message */
187 t
->ret
= client_recv_buffer(pcd
);
189 pcd
->status
= CL_RECEIVED_WELCOME
;
191 case CL_RECEIVED_WELCOME
: /* send auth command */
192 sprintf(pcd
->buf
, "auth %s%s", pcd
->conf
.plain_given
?
193 "" : "rc4 ", pcd
->user
);
194 PARA_INFO_LOG("--> %s\n", pcd
->buf
);
195 t
->ret
= send_buffer(pcd
->fd
, pcd
->buf
);
197 pcd
->status
= CL_SENT_AUTH
;
199 case CL_SENT_AUTH
: /* receive challenge number */
201 t
->ret
= client_recv_buffer(pcd
);
205 t
->ret
= -E_INVALID_CHALLENGE
;
206 PARA_ERROR_LOG("received the following: %s\n", pcd
->buf
);
209 PARA_INFO_LOG("%s", "<-- [challenge]\n");
210 /* decrypt challenge number */
211 t
->ret
= para_decrypt_challenge(pcd
->key_file
, &pcd
->challenge_nr
,
212 (unsigned char *) pcd
->buf
, 64);
214 pcd
->status
= CL_RECEIVED_CHALLENGE
;
216 case CL_RECEIVED_CHALLENGE
: /* send decrypted challenge */
217 PARA_INFO_LOG("--> %lu\n", pcd
->challenge_nr
);
218 t
->ret
= send_va_buffer(pcd
->fd
, "%s%lu", CHALLENGE_RESPONSE_MSG
,
221 pcd
->status
= CL_SENT_CH_RESPONSE
;
223 case CL_SENT_CH_RESPONSE
: /* read server response */
225 size_t bytes_received
;
226 unsigned char rc4_buf
[2 * RC4_KEY_LEN
] = "";
228 t
->ret
= client_recv_buffer(pcd
);
231 bytes_received
= t
->ret
;
232 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
233 "info ++++\n", pcd
->buf
);
234 /* check if server has sent "Proceed" message */
235 t
->ret
= -E_CLIENT_AUTH
;
236 if (!strstr(pcd
->buf
, PROCEED_MSG
))
239 pcd
->status
= CL_RECEIVED_PROCEED
;
240 if (bytes_received
< PROCEED_MSG_LEN
+ 32)
242 PARA_INFO_LOG("%s", "decrypting session key\n");
243 t
->ret
= para_decrypt_buffer(pcd
->key_file
, rc4_buf
,
244 (unsigned char *)pcd
->buf
+ PROCEED_MSG_LEN
+ 1,
245 bytes_received
- PROCEED_MSG_LEN
- 1);
248 RC4_set_key(&pcd
->rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
249 RC4_set_key(&pcd
->rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
250 enable_crypt(pcd
->fd
, rc4_recv
, rc4_send
, pcd
);
252 case CL_RECEIVED_PROCEED
: /* concat args and send command */
255 char *command
= NULL
;
256 for (i
= 0; i
< pcd
->conf
.inputs_num
; i
++) {
258 command
= make_message("%s\n%s", command
?
259 command
: "", pcd
->conf
.inputs
[i
]);
262 command
= para_strcat(command
, EOC_MSG
"\n");
263 PARA_DEBUG_LOG("--> %s\n", command
);
264 t
->ret
= send_buffer(pcd
->fd
, command
);
267 pcd
->status
= CL_SENT_COMMAND
;
270 case CL_SENT_COMMAND
:
272 t
->ret
= client_recv_buffer(pcd
);
275 t
->ret
= -E_HANDSHAKE_COMPLETE
;
276 if (strstr(pcd
->buf
, AWAITING_DATA_MSG
))
277 pcd
->status
= CL_SENDING_STDIN
;
279 pcd
->status
= CL_RECEIVING_SERVER_OUTPUT
;
281 case CL_SENDING_STDIN
: /* FIXME: might block */
282 PARA_INFO_LOG("loaded: %zd\n", *pcd
->in_loaded
);
283 t
->ret
= send_bin_buffer(pcd
->fd
, pcd
->inbuf
, *pcd
->in_loaded
);
289 *pcd
->in_loaded
= 0; /* FIXME: short writes */
291 case CL_RECEIVING_SERVER_OUTPUT
:
292 t
->ret
= client_recv_buffer(pcd
);
298 int client_open(struct private_client_data
*pcd
)
302 struct sockaddr_in their_addr
;
304 /* get the host info */
305 PARA_NOTICE_LOG("getting host info of %s\n",
306 pcd
->conf
.hostname_arg
);
307 ret
= get_host_info(pcd
->conf
.hostname_arg
, &he
);
315 /* init their_addr */
316 init_sockaddr(&their_addr
, pcd
->conf
.server_port_arg
, he
);
318 PARA_NOTICE_LOG("connecting to %s\n", pcd
->conf
.hostname_arg
);
319 ret
= para_connect(pcd
->fd
, &their_addr
);
322 pcd
->status
= CL_CONNECTED
;
323 pcd
->task
.pre_select
= client_pre_select
;
324 pcd
->task
.post_select
= client_post_select
;
325 pcd
->task
.private_data
= pcd
;
326 sprintf(pcd
->task
.status
, "client");
327 register_task(&pcd
->task
);