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"
33 void rc4_send(unsigned long len
, const unsigned char *indata
,
34 unsigned char *outdata
, void *private_data
)
36 struct private_client_data
*pcd
= private_data
;
37 RC4(&pcd
->rc4_send_key
, len
, indata
, outdata
);
40 void rc4_recv(unsigned long len
, const unsigned char *indata
,
41 unsigned char *outdata
, void *private_data
)
43 struct private_client_data
*pcd
= private_data
;
44 RC4(&pcd
->rc4_recv_key
, len
, indata
, outdata
);
48 void client_close(struct private_client_data
*pcd
)
55 free(pcd
->config_file
);
60 int client_parse_config(int argc
, char *argv
[],
61 struct private_client_data
**pcd_ptr
)
63 char *home
= para_homedir();
66 struct private_client_data
*pcd
=
67 para_calloc(sizeof(struct private_client_data
));
70 cmdline_parser(argc
, argv
, &pcd
->conf
);
71 ret
= - E_CLIENT_SYNTAX
;
72 if (!pcd
->conf
.inputs_num
)
74 pcd
->user
= pcd
->conf
.user_given
?
75 para_strdup(pcd
->conf
.user_arg
) : para_logname();
77 pcd
->key_file
= pcd
->conf
.key_file_given
?
78 para_strdup(pcd
->conf
.key_file_arg
) :
79 make_message("%s/.paraslash/key.%s", home
, pcd
->user
);
81 pcd
->config_file
= pcd
->conf
.config_file_given
?
82 para_strdup(pcd
->conf
.config_file_arg
) :
83 make_message("%s/.paraslash/client.conf", home
);
84 ret
= stat(pcd
->config_file
, &statbuf
);
85 if (ret
&& pcd
->conf
.config_file_given
) {
90 cmdline_parser_configfile(pcd
->config_file
, &pcd
->conf
, 0, 0, 0);
94 "current loglevel: %d\n"
95 "using config_file: %s\n"
96 "using key_file: %s\n"
97 "connecting to %s:%d\n" ,
98 pcd
->conf
.loglevel_arg
,
101 pcd
->conf
.hostname_arg
, pcd
->conf
.server_port_arg
110 void client_pre_select(struct sched
*s
, struct task
*t
)
112 struct private_client_data
*pcd
= t
->private_data
;
114 PARA_INFO_LOG("status %d\n", pcd
->status
);
120 switch (pcd
->status
) {
123 case CL_SENT_CH_RESPONSE
:
124 case CL_SENT_COMMAND
:
125 para_fd_set(pcd
->fd
, &s
->rfds
, &s
->max_fileno
);
129 case CL_RECEIVED_WELCOME
:
130 case CL_RECEIVED_CHALLENGE
:
131 case CL_RECEIVED_PROCEED
:
132 para_fd_set(pcd
->fd
, &s
->wfds
, &s
->max_fileno
);
136 case CL_RECEIVING_SERVER_OUTPUT
:
137 if (pcd
->loaded
< CLIENT_BUFSIZE
- 1) {
138 para_fd_set(pcd
->fd
, &s
->rfds
, &s
->max_fileno
);
142 case CL_SENDING_STDIN
:
143 if (*pcd
->in_loaded
) {
144 PARA_INFO_LOG("loaded: %zd\n", *pcd
->in_loaded
);
145 para_fd_set(pcd
->fd
, &s
->wfds
, &s
->max_fileno
);
149 t
->ret
= -E_INPUT_EOF
;
150 s
->timeout
.tv_sec
= 0;
151 s
->timeout
.tv_usec
= 1;
158 static ssize_t
client_recv_buffer(struct private_client_data
*pcd
)
160 ssize_t ret
= recv_buffer(pcd
->fd
, pcd
->buf
+ pcd
->loaded
,
161 CLIENT_BUFSIZE
- pcd
->loaded
);
163 return -E_SERVER_EOF
;
170 void client_post_select(struct sched
*s
, struct task
*t
)
172 struct private_client_data
*pcd
= t
->private_data
;
174 PARA_INFO_LOG("status %d\n", pcd
->status
);
178 if (!pcd
->check_r
&& !pcd
->check_w
)
180 if (pcd
->check_r
&& !FD_ISSET(pcd
->fd
, &s
->rfds
))
182 if (pcd
->check_w
&& !FD_ISSET(pcd
->fd
, &s
->wfds
))
184 switch (pcd
->status
) {
185 case CL_CONNECTED
: /* receive welcome message */
186 t
->ret
= client_recv_buffer(pcd
);
188 pcd
->status
= CL_RECEIVED_WELCOME
;
190 case CL_RECEIVED_WELCOME
: /* send auth command */
191 sprintf(pcd
->buf
, "auth %s%s", pcd
->conf
.plain_given
?
192 "" : "rc4 ", pcd
->user
);
193 PARA_INFO_LOG("--> %s\n", pcd
->buf
);
194 t
->ret
= send_buffer(pcd
->fd
, pcd
->buf
);
196 pcd
->status
= CL_SENT_AUTH
;
198 case CL_SENT_AUTH
: /* receive challenge number */
200 t
->ret
= client_recv_buffer(pcd
);
204 t
->ret
= -E_INVALID_CHALLENGE
;
205 PARA_ERROR_LOG("received the following: %s\n", pcd
->buf
);
208 PARA_INFO_LOG("%s", "<-- [challenge]\n");
209 /* decrypt challenge number */
210 t
->ret
= para_decrypt_challenge(pcd
->key_file
, &pcd
->challenge_nr
,
211 (unsigned char *) pcd
->buf
, 64);
213 pcd
->status
= CL_RECEIVED_CHALLENGE
;
215 case CL_RECEIVED_CHALLENGE
: /* send decrypted challenge */
216 PARA_INFO_LOG("--> %lu\n", pcd
->challenge_nr
);
217 t
->ret
= send_va_buffer(pcd
->fd
, "%s%lu", CHALLENGE_RESPONSE_MSG
,
220 pcd
->status
= CL_SENT_CH_RESPONSE
;
222 case CL_SENT_CH_RESPONSE
: /* read server response */
224 size_t bytes_received
;
225 unsigned char rc4_buf
[2 * RC4_KEY_LEN
] = "";
227 t
->ret
= client_recv_buffer(pcd
);
230 bytes_received
= t
->ret
;
231 PARA_INFO_LOG("++++ server info ++++\n%s\n++++ end of server "
232 "info ++++\n", pcd
->buf
);
233 /* check if server has sent "Proceed" message */
234 t
->ret
= -E_CLIENT_AUTH
;
235 if (!strstr(pcd
->buf
, PROCEED_MSG
))
238 pcd
->status
= CL_RECEIVED_PROCEED
;
239 if (bytes_received
< PROCEED_MSG_LEN
+ 32)
241 PARA_INFO_LOG("%s", "decrypting session key\n");
242 t
->ret
= para_decrypt_buffer(pcd
->key_file
, rc4_buf
,
243 (unsigned char *)pcd
->buf
+ PROCEED_MSG_LEN
+ 1,
244 bytes_received
- PROCEED_MSG_LEN
- 1);
247 RC4_set_key(&pcd
->rc4_send_key
, RC4_KEY_LEN
, rc4_buf
);
248 RC4_set_key(&pcd
->rc4_recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
249 enable_crypt(pcd
->fd
, rc4_recv
, rc4_send
, pcd
);
251 case CL_RECEIVED_PROCEED
: /* concat args and send command */
254 char *command
= NULL
;
255 for (i
= 0; i
< pcd
->conf
.inputs_num
; i
++) {
257 command
= make_message("%s\n%s", command
?
258 command
: "", pcd
->conf
.inputs
[i
]);
261 command
= para_strcat(command
, EOC_MSG
"\n");
262 PARA_INFO_LOG("--> %s\n", command
);
263 t
->ret
= send_buffer(pcd
->fd
, command
);
266 pcd
->status
= CL_SENT_COMMAND
;
269 case CL_SENT_COMMAND
:
271 t
->ret
= client_recv_buffer(pcd
);
274 t
->ret
= -E_HANDSHAKE_COMPLETE
;
275 if (strstr(pcd
->buf
, AWAITING_DATA_MSG
))
276 pcd
->status
= CL_SENDING_STDIN
;
278 pcd
->status
= CL_RECEIVING_SERVER_OUTPUT
;
280 case CL_SENDING_STDIN
: /* FIXME: might block */
281 PARA_INFO_LOG("loaded: %zd\n", *pcd
->in_loaded
);
282 t
->ret
= send_bin_buffer(pcd
->fd
, pcd
->inbuf
, *pcd
->in_loaded
);
288 *pcd
->in_loaded
= 0; /* FIXME: short writes */
290 case CL_RECEIVING_SERVER_OUTPUT
:
291 t
->ret
= client_recv_buffer(pcd
);
297 int client_open(struct private_client_data
*pcd
)
301 struct sockaddr_in their_addr
;
303 /* get the host info */
304 PARA_NOTICE_LOG("getting host info of %s\n",
305 pcd
->conf
.hostname_arg
);
306 ret
= get_host_info(pcd
->conf
.hostname_arg
, &he
);
314 /* init their_addr */
315 init_sockaddr(&their_addr
, pcd
->conf
.server_port_arg
, he
);
317 PARA_NOTICE_LOG("connecting to %s\n", pcd
->conf
.hostname_arg
);
318 ret
= para_connect(pcd
->fd
, &their_addr
);
321 pcd
->status
= CL_CONNECTED
;
322 pcd
->task
.pre_select
= client_pre_select
;
323 pcd
->task
.post_select
= client_post_select
;
324 pcd
->task
.private_data
= pcd
;
325 sprintf(pcd
->task
.status
, "client");
326 register_task(&pcd
->task
);