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. */
16 #include "client.cmdline.h"
22 #include "client.cmdline.h"
26 * Rc4-encrypt data before sending.
28 * \param len The number of bytes to encrypt.
29 * \param indata Pointer to the input data of length \a len to be encrypted.
30 * \param outdata Result-pointer that holds the encrypted data.
31 * \param private_data Contains the rc4 key.
33 static void rc4_send(unsigned long len, const unsigned char *indata,
34 unsigned char *outdata, void *private_data)
36 struct client_task *ct = private_data;
37 RC4(&ct->rc4_send_key, len, indata, outdata);
41 * Rc4-decrypt received data.
43 * Parameters are identical to those of rc4_send.
45 static void rc4_recv(unsigned long len, const unsigned char *indata,
46 unsigned char *outdata, void *private_data)
48 struct client_task *ct = private_data;
49 RC4(&ct->rc4_recv_key, len, indata, outdata);
53 * Close the connection to para_server and free all resources.
55 * \param ct Pointer to the client data.
59 void client_close(struct client_task *ct)
64 disable_crypt(ct->fd);
69 free(ct->config_file);
71 client_cmdline_parser_free(&ct->conf);
76 * The preselect hook for server commands.
78 * \param s Pointer to the scheduler.
79 * \param t Pointer to the task struct for this command.
81 * The task pointer must contain a pointer to the initialized client data
82 * structure as it is returned by client_open().
84 * This function checks the state of the connection and adds the file descriptor
85 * of the connection to the read or write fd set of \a s accordingly.
87 * \sa register_task() client_open(), struct sched, struct task.
89 static void client_pre_select(struct sched *s, struct task *t)
91 struct client_task *ct = container_of(t, struct client_task, task);
100 case CL_SENT_CH_RESPONSE:
101 case CL_SENT_COMMAND:
102 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
106 case CL_RECEIVED_WELCOME:
107 case CL_RECEIVED_CHALLENGE:
108 case CL_RECEIVED_PROCEED:
109 para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
114 if (ct->loaded < CLIENT_BUFSIZE - 1) {
115 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
120 if (!ct->in_loaded) /* stdin task not yet started */
122 if (*ct->in_loaded) {
123 PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
124 para_fd_set(ct->fd, &s->wfds, &s->max_fileno);
128 t->error = *ct->in_error;
129 s->timeout.tv_sec = 0;
130 s->timeout.tv_usec = 1;
137 static ssize_t client_recv_buffer(struct client_task *ct)
139 ssize_t ret = recv_buffer(ct->fd, ct->buf + ct->loaded,
140 CLIENT_BUFSIZE - ct->loaded);
142 return -E_SERVER_EOF;
150 * The post select hook for client commands.
152 * \param s Pointer to the scheduler.
153 * \param t Pointer to the task struct for this command.
155 * Depending on the current state of the connection and the status of the read
156 * and write fd sets of \a s, this function performs the necessary steps to
157 * authenticate the connection, to send the command given by \a t->private_data
158 * and to receive para_server's output, if any.
160 * \sa struct sched, struct task.
162 static void client_post_select(struct sched *s, struct task *t)
164 struct client_task *ct = container_of(t, struct client_task, task);
169 if (!ct->check_r && !ct->check_w)
171 if (ct->check_r && !FD_ISSET(ct->fd, &s->rfds))
173 if (ct->check_w && !FD_ISSET(ct->fd, &s->wfds))
175 switch (ct->status) {
176 case CL_CONNECTED: /* receive welcome message */
177 t->error = client_recv_buffer(ct);
179 ct->status = CL_RECEIVED_WELCOME;
181 case CL_RECEIVED_WELCOME: /* send auth command */
182 sprintf(ct->buf, "auth rc4 %s", ct->user);
183 PARA_INFO_LOG("--> %s\n", ct->buf);
184 t->error = send_buffer(ct->fd, ct->buf);
186 ct->status = CL_SENT_AUTH;
188 case CL_SENT_AUTH: /* receive challenge number */
190 t->error = client_recv_buffer(ct);
193 if (t->error != 64) {
194 t->error = -E_INVALID_CHALLENGE;
195 PARA_ERROR_LOG("received the following: %s\n", ct->buf);
198 PARA_INFO_LOG("<-- [challenge]\n");
199 /* decrypt challenge number */
200 t->error = para_decrypt_challenge(ct->key_file, &ct->challenge_nr,
201 (unsigned char *) ct->buf, 64);
203 ct->status = CL_RECEIVED_CHALLENGE;
205 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
206 PARA_INFO_LOG("--> %lu\n", ct->challenge_nr);
207 t->error = send_va_buffer(ct->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
210 ct->status = CL_SENT_CH_RESPONSE;
212 case CL_SENT_CH_RESPONSE: /* read server response */
214 size_t bytes_received;
215 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
217 t->error = client_recv_buffer(ct);
220 bytes_received = t->error;
221 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
222 "info ++++\n", ct->buf);
223 /* check if server has sent "Proceed" message and the rc4 keys */
224 t->error = -E_CLIENT_AUTH;
225 if (bytes_received < PROCEED_MSG_LEN + 2 * RC4_KEY_LEN)
227 if (!strstr(ct->buf, PROCEED_MSG))
229 PARA_INFO_LOG("decrypting session key\n");
230 t->error = para_decrypt_buffer(ct->key_file, rc4_buf,
231 (unsigned char *)ct->buf + PROCEED_MSG_LEN + 1,
232 bytes_received - PROCEED_MSG_LEN - 1);
235 RC4_set_key(&ct->rc4_send_key, RC4_KEY_LEN, rc4_buf);
236 RC4_set_key(&ct->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
237 enable_crypt(ct->fd, rc4_recv, rc4_send, ct);
238 ct->status = CL_RECEIVED_PROCEED;
241 case CL_RECEIVED_PROCEED: /* concat args and send command */
244 char *command = NULL;
245 for (i = 0; i < ct->conf.inputs_num; i++) {
247 command = make_message("%s\n%s", command?
248 command : "", ct->conf.inputs[i]);
251 command = para_strcat(command, EOC_MSG "\n");
252 PARA_DEBUG_LOG("--> %s\n", command);
253 t->error = send_buffer(ct->fd, command);
256 ct->status = CL_SENT_COMMAND;
259 case CL_SENT_COMMAND:
261 t->error = client_recv_buffer(ct);
264 if (strstr(ct->buf, AWAITING_DATA_MSG))
265 ct->status = CL_SENDING;
267 ct->status = CL_RECEIVING;
269 case CL_SENDING: /* FIXME: might block */
270 PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
271 t->error = send_bin_buffer(ct->fd, ct->inbuf, *ct->in_loaded);
277 t->error = client_recv_buffer(ct);
282 /* connect to para_server and register the client task */
283 static int client_connect(struct client_task *ct)
288 ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
289 ct->conf.server_port_arg);
293 ct->status = CL_CONNECTED;
294 ret = mark_fd_nonblocking(ct->fd);
297 ct->task.pre_select = client_pre_select;
298 ct->task.post_select = client_post_select;
299 sprintf(ct->task.status, "client");
300 register_task(&ct->task);
309 * Open connection to para_server.
311 * \param argc Usual argument count.
312 * \param argv Usual argument vector.
313 * \param ct_ptr Points to dynamically allocated and initialized client task
314 * struct upon successful return.
315 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
317 * Check the command line options given by \a argc and argv, set default values
318 * for user name and rsa key file, read further option from the config file.
319 * Finally, establish a connection to para_server.
323 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
326 char *home = para_homedir();
328 struct client_task *ct = para_calloc(sizeof(struct client_task));
330 ct->buf = para_malloc(CLIENT_BUFSIZE);
333 ret = -E_CLIENT_SYNTAX;
334 if (client_cmdline_parser(argc, argv, &ct->conf))
336 HANDLE_VERSION_FLAG("client", ct->conf);
337 ret = -E_CLIENT_SYNTAX;
338 if (!ct->conf.inputs_num)
340 ct->user = ct->conf.user_given?
341 para_strdup(ct->conf.user_arg) : para_logname();
343 ct->key_file = ct->conf.key_file_given?
344 para_strdup(ct->conf.key_file_arg) :
345 make_message("%s/.paraslash/key.%s", home, ct->user);
347 ct->config_file = ct->conf.config_file_given?
348 para_strdup(ct->conf.config_file_arg) :
349 make_message("%s/.paraslash/client.conf", home);
350 ret = file_exists(ct->config_file);
351 if (!ret && ct->conf.config_file_given) {
356 struct client_cmdline_parser_params params = {
360 .check_ambiguity = 0,
364 if (client_cmdline_parser_config_file(ct->config_file,
369 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
370 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
371 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
372 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
373 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
374 ct->conf.server_port_arg);
375 ret = client_connect(ct);
379 PARA_ERROR_LOG("%s\n", para_strerror(-ret));