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. */
10 #include <sys/types.h>
12 #include <openssl/rc4.h>
18 #include "client.cmdline.h"
24 #include "client.cmdline.h"
27 #include "buffer_tree.h"
29 /** The size of the receiving buffer. */
30 #define CLIENT_BUFSIZE 4000
33 * Close the connection to para_server and free all resources.
35 * \param ct Pointer to the client data.
39 void client_close(struct client_task *ct)
46 free(ct->config_file);
48 client_cmdline_parser_free(&ct->conf);
53 * The preselect hook for server commands.
55 * \param s Pointer to the scheduler.
56 * \param t Pointer to the task struct for this command.
58 * The task pointer must contain a pointer to the initialized client data
59 * structure as it is returned by client_open().
61 * This function checks the state of the connection and adds the file descriptor
62 * of the connection to the read or write fd set of \a s accordingly.
64 * \sa register_task() client_open(), struct sched, struct task.
66 static void client_pre_select(struct sched *s, struct task *t)
69 struct client_task *ct = container_of(t, struct client_task, task);
70 struct btr_node *btrn = ct->btrn;
77 case CL_SENT_CH_RESPONSE:
79 para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
82 case CL_RECEIVED_WELCOME:
83 case CL_RECEIVED_PROCEED:
84 para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
88 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
93 para_fd_set(ct->rc4c.fd, &s->rfds,
98 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
103 para_fd_set(ct->rc4c.fd, &s->wfds,
110 static ssize_t client_recv_buffer(struct client_task *ct, char *buf, size_t len)
114 if (ct->status < CL_SENT_CH_RESPONSE)
115 ret = recv_buffer(ct->rc4c.fd, buf, len);
117 ret = rc4_recv_buffer(&ct->rc4c, buf, len);
119 return -E_SERVER_EOF;
124 * The post select hook for client commands.
126 * \param s Pointer to the scheduler.
127 * \param t Pointer to the task struct for this command.
129 * Depending on the current state of the connection and the status of the read
130 * and write fd sets of \a s, this function performs the necessary steps to
131 * authenticate the connection, to send the command given by \a t->private_data
132 * and to receive para_server's output, if any.
134 * \sa struct sched, struct task.
136 static void client_post_select(struct sched *s, struct task *t)
138 struct client_task *ct = container_of(t, struct client_task, task);
139 struct btr_node *btrn = ct->btrn;
141 char buf[CLIENT_BUFSIZE];
146 switch (ct->status) {
147 case CL_CONNECTED: /* receive welcome message */
148 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
150 ret = client_recv_buffer(ct, buf, sizeof(buf));
153 ct->status = CL_RECEIVED_WELCOME;
155 case CL_RECEIVED_WELCOME: /* send auth command */
156 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
157 PARA_INFO_LOG("--> %s\n", buf);
158 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
160 ret = send_buffer(ct->rc4c.fd, buf);
163 ct->status = CL_SENT_AUTH;
167 * Receive challenge and rc4 keys, decrypt the challenge and
168 * send back the hash of the decrypted challenge.
171 /* decrypted challenge/rc4 buffer */
172 unsigned char crypt_buf[1024];
173 /* the SHA1 of the decrypted challenge */
174 unsigned char challenge_sha1[HASH_SIZE];
176 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
178 ret = client_recv_buffer(ct, buf, sizeof(buf));
181 PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", ret);
182 ret = para_decrypt_buffer(ct->key_file, crypt_buf,
183 (unsigned char *)buf, ret);
186 sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
187 RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
188 crypt_buf + CHALLENGE_SIZE);
189 RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
190 crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
191 hash_to_asc(challenge_sha1, buf);
192 PARA_INFO_LOG("--> %s\n", buf);
193 ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
197 ct->status = CL_SENT_CH_RESPONSE;
200 case CL_SENT_CH_RESPONSE: /* read server response */
202 size_t bytes_received;
203 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
205 ret = client_recv_buffer(ct, buf, sizeof(buf));
208 bytes_received = ret;
209 /* check if server has sent "Proceed" message */
210 ret = -E_CLIENT_AUTH;
211 if (bytes_received < PROCEED_MSG_LEN)
213 if (!strstr(buf, PROCEED_MSG))
215 ct->status = CL_RECEIVED_PROCEED;
218 case CL_RECEIVED_PROCEED: /* concat args and send command */
221 char *command = NULL;
222 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
224 for (i = 0; i < ct->conf.inputs_num; i++) {
226 command = make_message("%s\n%s", command?
227 command : "", ct->conf.inputs[i]);
230 command = para_strcat(command, EOC_MSG "\n");
231 PARA_DEBUG_LOG("--> %s\n", command);
232 ret = rc4_send_buffer(&ct->rc4c, command);
236 ct->status = CL_SENT_COMMAND;
239 case CL_SENT_COMMAND:
242 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
244 /* can not use "buf" here because we need a malloced buffer */
245 buf2 = para_malloc(CLIENT_BUFSIZE);
246 ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
251 if (strstr(buf2, AWAITING_DATA_MSG)) {
253 ct->status = CL_SENDING;
256 ct->status = CL_RECEIVING;
257 btr_add_output(buf2, ret, btrn);
264 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
269 if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
271 sz = btr_next_buffer(btrn, &buf2);
272 ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
275 btr_consume(btrn, sz);
281 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
286 if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
288 buf2 = para_malloc(CLIENT_BUFSIZE);
289 ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
294 buf2 = para_realloc(buf2, ret);
295 btr_add_output(buf2, ret, btrn);
302 if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
303 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
304 btr_remove_node(btrn);
308 /* connect to para_server and register the client task */
309 static int client_connect(struct client_task *ct)
314 ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
315 ct->conf.server_port_arg);
319 ct->status = CL_CONNECTED;
320 ret = mark_fd_nonblocking(ct->rc4c.fd);
323 ct->task.pre_select = client_pre_select;
324 ct->task.post_select = client_post_select;
325 sprintf(ct->task.status, "client");
326 register_task(&ct->task);
335 * Open connection to para_server.
337 * \param argc Usual argument count.
338 * \param argv Usual argument vector.
339 * \param ct_ptr Points to dynamically allocated and initialized client task
340 * struct upon successful return.
341 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
343 * Check the command line options given by \a argc and argv, set default values
344 * for user name and rsa key file, read further option from the config file.
345 * Finally, establish a connection to para_server.
349 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
350 int *loglevel, struct btr_node *parent, struct btr_node *child)
352 char *home = para_homedir();
354 struct client_task *ct = para_calloc(sizeof(struct client_task));
356 ct->btrn = btr_new_node(&(struct btr_node_description)
357 EMBRACE(.name = "client", .parent = parent, .child = child));
360 ret = -E_CLIENT_SYNTAX;
361 if (client_cmdline_parser(argc, argv, &ct->conf))
363 HANDLE_VERSION_FLAG("client", ct->conf);
364 ret = -E_CLIENT_SYNTAX;
365 if (!ct->conf.inputs_num)
367 ct->user = ct->conf.user_given?
368 para_strdup(ct->conf.user_arg) : para_logname();
370 ct->key_file = ct->conf.key_file_given?
371 para_strdup(ct->conf.key_file_arg) :
372 make_message("%s/.paraslash/key.%s", home, ct->user);
374 ct->config_file = ct->conf.config_file_given?
375 para_strdup(ct->conf.config_file_arg) :
376 make_message("%s/.paraslash/client.conf", home);
377 ret = file_exists(ct->config_file);
378 if (!ret && ct->conf.config_file_given) {
383 struct client_cmdline_parser_params params = {
387 .check_ambiguity = 0,
391 if (client_cmdline_parser_config_file(ct->config_file,
396 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
397 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
398 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
399 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
400 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
401 ct->conf.server_port_arg);
402 ret = client_connect(ct);
406 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
407 btr_free_node(ct->btrn);