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"
26 /** The size of the receiving buffer. */
27 #define CLIENT_BUFSIZE 4000
30 * Close the connection to para_server and free all resources.
32 * \param ct Pointer to the client data.
36 void client_close(struct client_task
*ct
)
42 sc_free(ct
->scc
.recv
);
43 sc_free(ct
->scc
.send
);
45 free(ct
->config_file
);
47 client_cmdline_parser_free(&ct
->conf
);
52 * The preselect hook for server commands.
54 * \param s Pointer to the scheduler.
55 * \param t Pointer to the task struct for this command.
57 * The task pointer must contain a pointer to the initialized client data
58 * structure as it is returned by client_open().
60 * This function checks the state of the connection and adds the file descriptor
61 * of the connection to the read or write fd set of \a s accordingly.
63 * \sa register_task() client_open(), struct sched, struct task.
65 static void client_pre_select(struct sched
*s
, struct task
*t
)
68 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
69 struct btr_node
*btrn
= ct
->btrn
;
76 case CL_SENT_CH_RESPONSE
:
78 para_fd_set(ct
->scc
.fd
, &s
->rfds
, &s
->max_fileno
);
81 case CL_RECEIVED_WELCOME
:
82 case CL_RECEIVED_PROCEED
:
83 para_fd_set(ct
->scc
.fd
, &s
->wfds
, &s
->max_fileno
);
87 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
92 para_fd_set(ct
->scc
.fd
, &s
->rfds
,
97 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
102 para_fd_set(ct
->scc
.fd
, &s
->wfds
,
109 static int client_recv_buffer(struct client_task
*ct
, fd_set
*rfds
,
110 char *buf
, size_t sz
, size_t *n
)
114 if (ct
->status
< CL_SENT_CH_RESPONSE
)
115 return read_nonblock(ct
->scc
.fd
, buf
, sz
, rfds
, n
);
118 ret
= sc_recv_buffer(&ct
->scc
, buf
, sz
);
120 * sc_recv_buffer is used with blocking fds elsewhere, so it
121 * does not use the nonblock-API. Therefore we need to
122 * check for EOF and EAGAIN.
125 return -E_SERVER_EOF
;
126 if (ret
== -ERRNO_TO_PARA_ERROR(EAGAIN
))
135 * The post select hook for client commands.
137 * \param s Pointer to the scheduler.
138 * \param t Pointer to the task struct for this command.
140 * Depending on the current state of the connection and the status of the read
141 * and write fd sets of \a s, this function performs the necessary steps to
142 * authenticate the connection, to send the command given by \a t->private_data
143 * and to receive para_server's output, if any.
145 * \sa struct sched, struct task.
147 static void client_post_select(struct sched
*s
, struct task
*t
)
149 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
150 struct btr_node
*btrn
= ct
->btrn
;
153 char buf
[CLIENT_BUFSIZE
];
158 switch (ct
->status
) {
159 case CL_CONNECTED
: /* receive welcome message */
160 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
161 if (ret
< 0 || n
== 0)
163 ct
->status
= CL_RECEIVED_WELCOME
;
165 case CL_RECEIVED_WELCOME
: /* send auth command */
166 sprintf(buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
167 PARA_INFO_LOG("--> %s\n", buf
);
168 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
170 ret
= send_buffer(ct
->scc
.fd
, buf
);
173 ct
->status
= CL_SENT_AUTH
;
177 * Receive challenge and session keys, decrypt the challenge and
178 * send back the hash of the decrypted challenge.
181 /* decrypted challenge/session key buffer */
182 unsigned char crypt_buf
[1024];
183 /* the SHA1 of the decrypted challenge */
184 unsigned char challenge_hash
[HASH_SIZE
];
186 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
187 if (ret
< 0 || n
== 0)
189 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", n
);
190 ret
= priv_decrypt(ct
->key_file
, crypt_buf
,
191 (unsigned char *)buf
, n
);
194 hash_function((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_hash
);
195 ct
->scc
.send
= sc_new(crypt_buf
+ CHALLENGE_SIZE
, SESSION_KEY_LEN
);
196 ct
->scc
.recv
= sc_new(crypt_buf
+ CHALLENGE_SIZE
+ SESSION_KEY_LEN
,
198 hash_to_asc(challenge_hash
, buf
);
199 PARA_INFO_LOG("--> %s\n", buf
);
200 ret
= send_bin_buffer(ct
->scc
.fd
, (char *)challenge_hash
,
204 ct
->status
= CL_SENT_CH_RESPONSE
;
207 case CL_SENT_CH_RESPONSE
: /* read server response */
209 ret
= client_recv_buffer(ct
, &s
->rfds
, buf
, sizeof(buf
), &n
);
210 if (ret
< 0 || n
== 0)
212 /* check if server has sent "Proceed" message */
213 ret
= -E_CLIENT_AUTH
;
214 if (n
< PROCEED_MSG_LEN
)
216 if (!strstr(buf
, PROCEED_MSG
))
218 ct
->status
= CL_RECEIVED_PROCEED
;
221 case CL_RECEIVED_PROCEED
: /* concat args and send command */
224 char *command
= NULL
;
225 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
227 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
229 command
= make_message("%s\n%s", command
?
230 command
: "", ct
->conf
.inputs
[i
]);
233 command
= para_strcat(command
, EOC_MSG
"\n");
234 PARA_DEBUG_LOG("--> %s\n", command
);
235 ret
= sc_send_buffer(&ct
->scc
, command
);
239 ct
->status
= CL_SENT_COMMAND
;
242 case CL_SENT_COMMAND
:
245 /* can not use "buf" here because we need a malloced buffer */
246 buf2
= para_malloc(CLIENT_BUFSIZE
);
247 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
249 if (strstr(buf2
, AWAITING_DATA_MSG
)) {
251 ct
->status
= CL_SENDING
;
254 ct
->status
= CL_RECEIVING
;
255 btr_add_output(buf2
, n
, btrn
);
264 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
269 if (!FD_ISSET(ct
->scc
.fd
, &s
->wfds
))
271 sz
= btr_next_buffer(btrn
, &buf2
);
272 ret
= sc_send_bin_buffer(&ct
->scc
, buf2
, sz
);
275 btr_consume(btrn
, sz
);
281 ret
= btr_node_status(btrn
, 0, BTR_NT_ROOT
);
287 * The FD_ISSET() is not strictly necessary, but is allows us
288 * to skip the malloc below if there is nothing to read anyway.
290 if (!FD_ISSET(ct
->scc
.fd
, &s
->rfds
))
292 buf2
= para_malloc(CLIENT_BUFSIZE
);
293 ret
= client_recv_buffer(ct
, &s
->rfds
, buf2
, CLIENT_BUFSIZE
, &n
);
295 buf2
= para_realloc(buf2
, n
);
296 btr_add_output(buf2
, n
, btrn
);
305 if (ret
!= -E_SERVER_EOF
&& ret
!= -E_BTR_EOF
)
306 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
307 btr_remove_node(btrn
);
311 /* connect to para_server and register the client task */
312 static int client_connect(struct client_task
*ct
)
317 ret
= para_connect_simple(IPPROTO_TCP
, ct
->conf
.hostname_arg
,
318 ct
->conf
.server_port_arg
);
322 ct
->status
= CL_CONNECTED
;
323 ret
= mark_fd_nonblocking(ct
->scc
.fd
);
326 ct
->task
.pre_select
= client_pre_select
;
327 ct
->task
.post_select
= client_post_select
;
328 sprintf(ct
->task
.status
, "client");
329 register_task(&ct
->task
);
338 * Open connection to para_server.
340 * \param argc Usual argument count.
341 * \param argv Usual argument vector.
342 * \param ct_ptr Points to dynamically allocated and initialized client task
343 * struct upon successful return.
344 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
345 * \param parent Add the new buffer tree node as a child of this node.
346 * \param child Add the new buffer tree node as a parent of this node.
348 * Check the command line options given by \a argc and argv, set default values
349 * for user name and rsa key file, read further option from the config file.
350 * Finally, establish a connection to para_server.
354 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
355 int *loglevel
, struct btr_node
*parent
, struct btr_node
*child
)
357 char *home
= para_homedir();
359 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
361 ct
->btrn
= btr_new_node(&(struct btr_node_description
)
362 EMBRACE(.name
= "client", .parent
= parent
, .child
= child
));
365 ret
= -E_CLIENT_SYNTAX
;
366 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
368 HANDLE_VERSION_FLAG("client", ct
->conf
);
369 ret
= -E_CLIENT_SYNTAX
;
370 if (!ct
->conf
.inputs_num
)
373 ct
->config_file
= ct
->conf
.config_file_given
?
374 para_strdup(ct
->conf
.config_file_arg
) :
375 make_message("%s/.paraslash/client.conf", home
);
376 ret
= file_exists(ct
->config_file
);
377 if (!ret
&& ct
->conf
.config_file_given
) {
382 struct client_cmdline_parser_params params
= {
386 .check_ambiguity
= 0,
390 if (client_cmdline_parser_config_file(ct
->config_file
,
394 ct
->user
= ct
->conf
.user_given
?
395 para_strdup(ct
->conf
.user_arg
) : para_logname();
397 if (ct
->conf
.key_file_given
)
398 ct
->key_file
= para_strdup(ct
->conf
.key_file_arg
);
400 ct
->key_file
= make_message("%s/.paraslash/key.%s",
402 if (!file_exists(ct
->key_file
)) {
404 ct
->key_file
= make_message("%s/.ssh/id_rsa", home
);
409 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
410 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
411 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
412 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
413 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
414 ct
->conf
.server_port_arg
);
415 ret
= client_connect(ct
);
419 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
420 btr_remove_node(ct
->btrn
);
421 btr_free_node(ct
->btrn
);