2 * Copyright (C) 1997-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file client_common.c Common functions of para_client and para_audiod. */
11 #include <openssl/rc4.h>
17 #include "client.cmdline.h"
23 #include "client.cmdline.h"
27 * Close the connection to para_server and free all resources.
29 * \param ct Pointer to the client data.
33 void client_close(struct client_task
*ct
)
41 free(ct
->config_file
);
43 client_cmdline_parser_free(&ct
->conf
);
48 * The preselect hook for server commands.
50 * \param s Pointer to the scheduler.
51 * \param t Pointer to the task struct for this command.
53 * The task pointer must contain a pointer to the initialized client data
54 * structure as it is returned by client_open().
56 * This function checks the state of the connection and adds the file descriptor
57 * of the connection to the read or write fd set of \a s accordingly.
59 * \sa register_task() client_open(), struct sched, struct task.
61 static void client_pre_select(struct sched
*s
, struct task
*t
)
63 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
72 case CL_SENT_CH_RESPONSE
:
74 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
78 case CL_RECEIVED_WELCOME
:
79 case CL_RECEIVED_CHALLENGE
:
80 case CL_RECEIVED_PROCEED
:
81 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
86 if (ct
->loaded
< CLIENT_BUFSIZE
- 1) {
87 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
92 if (!ct
->in_loaded
) /* stdin task not yet started */
95 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
96 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
100 t
->error
= *ct
->in_error
;
101 s
->timeout
.tv_sec
= 0;
102 s
->timeout
.tv_usec
= 1;
109 static ssize_t
client_recv_buffer(struct client_task
*ct
)
113 if (ct
->status
< CL_RECEIVED_PROCEED
)
114 ret
= recv_buffer(ct
->rc4c
.fd
, ct
->buf
+ ct
->loaded
,
115 CLIENT_BUFSIZE
- ct
->loaded
);
117 ret
= rc4_recv_buffer(&ct
->rc4c
, ct
->buf
+ ct
->loaded
,
118 CLIENT_BUFSIZE
- ct
->loaded
);
120 return -E_SERVER_EOF
;
128 * The post select hook for client commands.
130 * \param s Pointer to the scheduler.
131 * \param t Pointer to the task struct for this command.
133 * Depending on the current state of the connection and the status of the read
134 * and write fd sets of \a s, this function performs the necessary steps to
135 * authenticate the connection, to send the command given by \a t->private_data
136 * and to receive para_server's output, if any.
138 * \sa struct sched, struct task.
140 static void client_post_select(struct sched
*s
, struct task
*t
)
142 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
147 if (!ct
->check_r
&& !ct
->check_w
)
149 if (ct
->check_r
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->rfds
))
151 if (ct
->check_w
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->wfds
))
153 switch (ct
->status
) {
154 case CL_CONNECTED
: /* receive welcome message */
155 t
->error
= client_recv_buffer(ct
);
157 ct
->status
= CL_RECEIVED_WELCOME
;
159 case CL_RECEIVED_WELCOME
: /* send auth command */
160 sprintf(ct
->buf
, "auth rc4 %s", ct
->user
);
161 PARA_INFO_LOG("--> %s\n", ct
->buf
);
162 t
->error
= send_buffer(ct
->rc4c
.fd
, ct
->buf
);
164 ct
->status
= CL_SENT_AUTH
;
166 case CL_SENT_AUTH
: /* receive challenge number */
168 t
->error
= client_recv_buffer(ct
);
171 if (t
->error
!= 64) {
172 t
->error
= -E_INVALID_CHALLENGE
;
173 PARA_ERROR_LOG("received the following: %s\n", ct
->buf
);
176 PARA_INFO_LOG("<-- [challenge]\n");
177 /* decrypt challenge number */
178 t
->error
= para_decrypt_challenge(ct
->key_file
, &ct
->challenge_nr
,
179 (unsigned char *) ct
->buf
, 64);
181 ct
->status
= CL_RECEIVED_CHALLENGE
;
183 case CL_RECEIVED_CHALLENGE
: /* send decrypted challenge */
184 PARA_INFO_LOG("--> %lu\n", ct
->challenge_nr
);
185 t
->error
= send_va_buffer(ct
->rc4c
.fd
, "%s%lu", CHALLENGE_RESPONSE_MSG
,
188 ct
->status
= CL_SENT_CH_RESPONSE
;
190 case CL_SENT_CH_RESPONSE
: /* read server response */
192 size_t bytes_received
;
193 unsigned char rc4_buf
[2 * RC4_KEY_LEN
] = "";
195 t
->error
= client_recv_buffer(ct
);
198 bytes_received
= t
->error
;
199 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
200 "info ++++\n", ct
->buf
);
201 /* check if server has sent "Proceed" message and the rc4 keys */
202 t
->error
= -E_CLIENT_AUTH
;
203 if (bytes_received
< PROCEED_MSG_LEN
+ 2 * RC4_KEY_LEN
)
205 if (!strstr(ct
->buf
, PROCEED_MSG
))
207 PARA_INFO_LOG("decrypting session key\n");
208 t
->error
= para_decrypt_buffer(ct
->key_file
, rc4_buf
,
209 (unsigned char *)ct
->buf
+ PROCEED_MSG_LEN
+ 1,
210 bytes_received
- PROCEED_MSG_LEN
- 1);
213 RC4_set_key(&ct
->rc4c
.send_key
, RC4_KEY_LEN
, rc4_buf
);
214 RC4_set_key(&ct
->rc4c
.recv_key
, RC4_KEY_LEN
, rc4_buf
+ RC4_KEY_LEN
);
215 ct
->status
= CL_RECEIVED_PROCEED
;
218 case CL_RECEIVED_PROCEED
: /* concat args and send command */
221 char *command
= NULL
;
222 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
224 command
= make_message("%s\n%s", command
?
225 command
: "", ct
->conf
.inputs
[i
]);
228 command
= para_strcat(command
, EOC_MSG
"\n");
229 PARA_DEBUG_LOG("--> %s\n", command
);
230 t
->error
= rc4_send_buffer(&ct
->rc4c
, command
);
233 ct
->status
= CL_SENT_COMMAND
;
236 case CL_SENT_COMMAND
:
238 t
->error
= client_recv_buffer(ct
);
241 if (strstr(ct
->buf
, AWAITING_DATA_MSG
))
242 ct
->status
= CL_SENDING
;
244 ct
->status
= CL_RECEIVING
;
246 case CL_SENDING
: /* FIXME: might block */
247 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
248 t
->error
= rc4_send_bin_buffer(&ct
->rc4c
, ct
->inbuf
, *ct
->in_loaded
);
254 t
->error
= client_recv_buffer(ct
);
259 /* connect to para_server and register the client task */
260 static int client_connect(struct client_task
*ct
)
265 ret
= makesock(AF_UNSPEC
, IPPROTO_TCP
, 0, ct
->conf
.hostname_arg
,
266 ct
->conf
.server_port_arg
);
270 ct
->status
= CL_CONNECTED
;
271 ret
= mark_fd_nonblocking(ct
->rc4c
.fd
);
274 ct
->task
.pre_select
= client_pre_select
;
275 ct
->task
.post_select
= client_post_select
;
276 sprintf(ct
->task
.status
, "client");
277 register_task(&ct
->task
);
286 * Open connection to para_server.
288 * \param argc Usual argument count.
289 * \param argv Usual argument vector.
290 * \param ct_ptr Points to dynamically allocated and initialized client task
291 * struct upon successful return.
292 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
294 * Check the command line options given by \a argc and argv, set default values
295 * for user name and rsa key file, read further option from the config file.
296 * Finally, establish a connection to para_server.
300 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
303 char *home
= para_homedir();
305 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
307 ct
->buf
= para_malloc(CLIENT_BUFSIZE
);
310 ret
= -E_CLIENT_SYNTAX
;
311 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
313 HANDLE_VERSION_FLAG("client", ct
->conf
);
314 ret
= -E_CLIENT_SYNTAX
;
315 if (!ct
->conf
.inputs_num
)
317 ct
->user
= ct
->conf
.user_given
?
318 para_strdup(ct
->conf
.user_arg
) : para_logname();
320 ct
->key_file
= ct
->conf
.key_file_given
?
321 para_strdup(ct
->conf
.key_file_arg
) :
322 make_message("%s/.paraslash/key.%s", home
, ct
->user
);
324 ct
->config_file
= ct
->conf
.config_file_given
?
325 para_strdup(ct
->conf
.config_file_arg
) :
326 make_message("%s/.paraslash/client.conf", home
);
327 ret
= file_exists(ct
->config_file
);
328 if (!ret
&& ct
->conf
.config_file_given
) {
333 struct client_cmdline_parser_params params
= {
337 .check_ambiguity
= 0,
341 if (client_cmdline_parser_config_file(ct
->config_file
,
346 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
347 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
348 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
349 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
350 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
351 ct
->conf
.server_port_arg
);
352 ret
= client_connect(ct
);
356 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));