2 * Copyright (C) 1997-2011 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>
16 #include "client.cmdline.h"
21 #include "client.cmdline.h"
23 #include "buffer_tree.h"
25 /** The size of the receiving buffer. */
26 #define CLIENT_BUFSIZE 4000
29 * Close the connection to para_server and free all resources.
31 * \param ct Pointer to the client data.
35 void client_close(struct client_task *ct)
41 sc_free(ct->scc.recv);
42 sc_free(ct->scc.send);
44 free(ct->config_file);
46 client_cmdline_parser_free(&ct->conf);
51 * The preselect hook for server commands.
53 * \param s Pointer to the scheduler.
54 * \param t Pointer to the task struct for this command.
56 * The task pointer must contain a pointer to the initialized client data
57 * structure as it is returned by client_open().
59 * This function checks the state of the connection and adds the file descriptor
60 * of the connection to the read or write fd set of \a s accordingly.
62 * \sa register_task() client_open(), struct sched, struct task.
64 static void client_pre_select(struct sched *s, struct task *t)
67 struct client_task *ct = container_of(t, struct client_task, task);
68 struct btr_node *btrn = ct->btrn;
75 case CL_SENT_CH_RESPONSE:
77 para_fd_set(ct->scc.fd, &s->rfds, &s->max_fileno);
80 case CL_RECEIVED_WELCOME:
81 case CL_RECEIVED_PROCEED:
82 para_fd_set(ct->scc.fd, &s->wfds, &s->max_fileno);
86 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
91 para_fd_set(ct->scc.fd, &s->rfds,
96 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
101 para_fd_set(ct->scc.fd, &s->wfds,
108 static int client_recv_buffer(struct client_task *ct, fd_set *rfds,
109 char *buf, size_t sz, size_t *n)
113 if (ct->status < CL_SENT_CH_RESPONSE)
114 return read_nonblock(ct->scc.fd, buf, sz, rfds, n);
117 ret = sc_recv_buffer(&ct->scc, buf, sz);
119 * sc_recv_buffer is used with blocking fds elsewhere, so it
120 * does not use the nonblock-API. Therefore we need to
121 * check for EOF and EAGAIN.
124 return -E_SERVER_EOF;
125 if (ret == -ERRNO_TO_PARA_ERROR(EAGAIN))
134 * The post select hook for client commands.
136 * \param s Pointer to the scheduler.
137 * \param t Pointer to the task struct for this command.
139 * Depending on the current state of the connection and the status of the read
140 * and write fd sets of \a s, this function performs the necessary steps to
141 * authenticate the connection, to send the command given by \a t->private_data
142 * and to receive para_server's output, if any.
144 * \sa struct sched, struct task.
146 static void client_post_select(struct sched *s, struct task *t)
148 struct client_task *ct = container_of(t, struct client_task, task);
149 struct btr_node *btrn = ct->btrn;
152 char buf[CLIENT_BUFSIZE];
157 switch (ct->status) {
158 case CL_CONNECTED: /* receive welcome message */
159 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
160 if (ret < 0 || n == 0)
162 ct->status = CL_RECEIVED_WELCOME;
164 case CL_RECEIVED_WELCOME: /* send auth command */
165 sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
166 PARA_INFO_LOG("--> %s\n", buf);
167 if (!FD_ISSET(ct->scc.fd, &s->wfds))
169 ret = send_buffer(ct->scc.fd, buf);
172 ct->status = CL_SENT_AUTH;
176 * Receive challenge and session keys, decrypt the challenge and
177 * send back the hash of the decrypted challenge.
180 /* decrypted challenge/session key buffer */
181 unsigned char crypt_buf[1024];
182 /* the SHA1 of the decrypted challenge */
183 unsigned char challenge_hash[HASH_SIZE];
185 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
186 if (ret < 0 || n == 0)
188 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n);
189 ret = priv_decrypt(ct->key_file, crypt_buf,
190 (unsigned char *)buf, n);
193 hash_function((char *)crypt_buf, CHALLENGE_SIZE, challenge_hash);
194 ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN);
195 ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN,
197 hash_to_asc(challenge_hash, buf);
198 PARA_INFO_LOG("--> %s\n", buf);
199 ret = send_bin_buffer(ct->scc.fd, (char *)challenge_hash,
203 ct->status = CL_SENT_CH_RESPONSE;
206 case CL_SENT_CH_RESPONSE: /* read server response */
208 ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n);
209 if (ret < 0 || n == 0)
211 /* check if server has sent "Proceed" message */
212 ret = -E_CLIENT_AUTH;
213 if (n < PROCEED_MSG_LEN)
215 if (!strstr(buf, PROCEED_MSG))
217 ct->status = CL_RECEIVED_PROCEED;
220 case CL_RECEIVED_PROCEED: /* concat args and send command */
223 char *command = NULL;
224 if (!FD_ISSET(ct->scc.fd, &s->wfds))
226 for (i = 0; i < ct->conf.inputs_num; i++) {
228 command = make_message("%s\n%s", command?
229 command : "", ct->conf.inputs[i]);
232 command = para_strcat(command, EOC_MSG "\n");
233 PARA_DEBUG_LOG("--> %s\n", command);
234 ret = sc_send_buffer(&ct->scc, command);
238 ct->status = CL_SENT_COMMAND;
241 case CL_SENT_COMMAND:
244 /* can not use "buf" here because we need a malloced buffer */
245 buf2 = para_malloc(CLIENT_BUFSIZE);
246 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
248 if (strstr(buf2, AWAITING_DATA_MSG)) {
250 ct->status = CL_SENDING;
253 ct->status = CL_RECEIVING;
254 btr_add_output(buf2, n, btrn);
263 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
268 if (!FD_ISSET(ct->scc.fd, &s->wfds))
270 sz = btr_next_buffer(btrn, &buf2);
271 ret = sc_send_bin_buffer(&ct->scc, buf2, sz);
274 btr_consume(btrn, sz);
280 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
286 * The FD_ISSET() is not strictly necessary, but is allows us
287 * to skip the malloc below if there is nothing to read anyway.
289 if (!FD_ISSET(ct->scc.fd, &s->rfds))
291 buf2 = para_malloc(CLIENT_BUFSIZE);
292 ret = client_recv_buffer(ct, &s->rfds, buf2, CLIENT_BUFSIZE, &n);
294 buf2 = para_realloc(buf2, n);
295 btr_add_output(buf2, n, btrn);
304 if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
305 PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
306 btr_remove_node(btrn);
310 /* connect to para_server and register the client task */
311 static int client_connect(struct client_task *ct)
316 ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
317 ct->conf.server_port_arg);
321 ct->status = CL_CONNECTED;
322 ret = mark_fd_nonblocking(ct->scc.fd);
325 ct->task.pre_select = client_pre_select;
326 ct->task.post_select = client_post_select;
327 sprintf(ct->task.status, "client");
328 register_task(&ct->task);
337 * Open connection to para_server.
339 * \param argc Usual argument count.
340 * \param argv Usual argument vector.
341 * \param ct_ptr Points to dynamically allocated and initialized client task
342 * struct upon successful return.
343 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
344 * \param parent Add the new buffer tree node as a child of this node.
345 * \param child Add the new buffer tree node as a parent of this node.
347 * Check the command line options given by \a argc and argv, set default values
348 * for user name and rsa key file, read further option from the config file.
349 * Finally, establish a connection to para_server.
353 int client_open(int argc, char *argv[], struct client_task **ct_ptr,
354 int *loglevel, struct btr_node *parent, struct btr_node *child)
356 char *home = para_homedir();
358 struct client_task *ct = para_calloc(sizeof(struct client_task));
360 ct->btrn = btr_new_node(&(struct btr_node_description)
361 EMBRACE(.name = "client", .parent = parent, .child = child));
364 ret = -E_CLIENT_SYNTAX;
365 if (client_cmdline_parser(argc, argv, &ct->conf))
367 HANDLE_VERSION_FLAG("client", ct->conf);
368 ret = -E_CLIENT_SYNTAX;
369 if (!ct->conf.inputs_num)
372 ct->config_file = ct->conf.config_file_given?
373 para_strdup(ct->conf.config_file_arg) :
374 make_message("%s/.paraslash/client.conf", home);
375 ret = file_exists(ct->config_file);
376 if (!ret && ct->conf.config_file_given) {
381 struct client_cmdline_parser_params params = {
385 .check_ambiguity = 0,
389 if (client_cmdline_parser_config_file(ct->config_file,
393 ct->user = ct->conf.user_given?
394 para_strdup(ct->conf.user_arg) : para_logname();
396 ct->key_file = ct->conf.key_file_given?
397 para_strdup(ct->conf.key_file_arg) :
398 make_message("%s/.paraslash/key.%s", home, ct->user);
401 *loglevel = get_loglevel_by_name(ct->conf.loglevel_arg);
402 PARA_INFO_LOG("loglevel: %s\n", ct->conf.loglevel_arg);
403 PARA_INFO_LOG("config_file: %s\n", ct->config_file);
404 PARA_INFO_LOG("key_file: %s\n", ct->key_file);
405 PARA_NOTICE_LOG("connecting %s:%d\n", ct->conf.hostname_arg,
406 ct->conf.server_port_arg);
407 ret = client_connect(ct);
411 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
412 btr_remove_node(ct->btrn);
413 btr_free_node(ct->btrn);