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"
28 * Close the connection to para_server and free all resources.
30 * \param ct Pointer to the client data.
34 void client_close(struct client_task
*ct
)
42 free(ct
->config_file
);
44 client_cmdline_parser_free(&ct
->conf
);
49 * The preselect hook for server commands.
51 * \param s Pointer to the scheduler.
52 * \param t Pointer to the task struct for this command.
54 * The task pointer must contain a pointer to the initialized client data
55 * structure as it is returned by client_open().
57 * This function checks the state of the connection and adds the file descriptor
58 * of the connection to the read or write fd set of \a s accordingly.
60 * \sa register_task() client_open(), struct sched, struct task.
62 static void client_pre_select(struct sched
*s
, struct task
*t
)
64 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
73 case CL_SENT_CH_RESPONSE
:
75 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
79 case CL_RECEIVED_WELCOME
:
80 case CL_RECEIVED_CHALLENGE
:
81 case CL_RECEIVED_PROCEED
:
82 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
87 if (ct
->loaded
< CLIENT_BUFSIZE
- 1) {
88 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
93 if (!ct
->in_loaded
) /* stdin task not yet started */
96 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
97 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
101 t
->error
= *ct
->in_error
;
102 s
->timeout
.tv_sec
= 0;
103 s
->timeout
.tv_usec
= 1;
110 static ssize_t
client_recv_buffer(struct client_task
*ct
)
114 if (ct
->status
< CL_SENT_CH_RESPONSE
)
115 ret
= recv_buffer(ct
->rc4c
.fd
, ct
->buf
+ ct
->loaded
,
116 CLIENT_BUFSIZE
- ct
->loaded
);
118 ret
= rc4_recv_buffer(&ct
->rc4c
, ct
->buf
+ ct
->loaded
,
119 CLIENT_BUFSIZE
- ct
->loaded
);
121 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
);
143 unsigned char crypt_buf
[1024];
148 if (!ct
->check_r
&& !ct
->check_w
)
150 if (ct
->check_r
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->rfds
))
152 if (ct
->check_w
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->wfds
))
154 switch (ct
->status
) {
155 case CL_CONNECTED
: /* receive welcome message */
156 t
->error
= client_recv_buffer(ct
);
159 ct
->status
= CL_RECEIVED_WELCOME
;
161 case CL_RECEIVED_WELCOME
: /* send auth command */
162 sprintf(ct
->buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
163 PARA_INFO_LOG("--> %s\n", ct
->buf
);
164 t
->error
= send_buffer(ct
->rc4c
.fd
, ct
->buf
);
167 ct
->status
= CL_SENT_AUTH
;
169 case CL_SENT_AUTH
: /* receive challenge and rc4 keys */
171 t
->error
= client_recv_buffer(ct
);
174 PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", t
->error
);
175 /* decrypt challenge/rc4 buffer */
176 t
->error
= para_decrypt_buffer(ct
->key_file
, crypt_buf
,
177 (unsigned char *)ct
->buf
, t
->error
);
180 ct
->status
= CL_RECEIVED_CHALLENGE
;
181 RC4_set_key(&ct
->rc4c
.send_key
, RC4_KEY_LEN
,
182 crypt_buf
+ CHALLENGE_SIZE
);
183 RC4_set_key(&ct
->rc4c
.recv_key
, RC4_KEY_LEN
,
184 crypt_buf
+ CHALLENGE_SIZE
+ RC4_KEY_LEN
);
186 case CL_RECEIVED_CHALLENGE
:
188 unsigned char challenge_sha1
[HASH_SIZE
];
189 /* send sha1 of decrypted challenge */
190 sha1_hash((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_sha1
);
191 hash_to_asc(challenge_sha1
, ct
->buf
);
192 PARA_INFO_LOG("--> %s\n", ct
->buf
);
193 t
->error
= 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
;
204 t
->error
= client_recv_buffer(ct
);
207 bytes_received
= t
->error
;
208 /* check if server has sent "Proceed" message */
209 t
->error
= -E_CLIENT_AUTH
;
210 if (bytes_received
< PROCEED_MSG_LEN
)
212 if (!strstr(ct
->buf
, PROCEED_MSG
))
214 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
);
234 ct
->status
= CL_SENT_COMMAND
;
237 case CL_SENT_COMMAND
:
239 t
->error
= client_recv_buffer(ct
);
242 if (strstr(ct
->buf
, AWAITING_DATA_MSG
))
243 ct
->status
= CL_SENDING
;
245 ct
->status
= CL_RECEIVING
;
248 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
249 t
->error
= rc4_send_bin_buffer(&ct
->rc4c
, ct
->inbuf
,
256 t
->error
= client_recv_buffer(ct
);
262 if (t
->error
!= -E_SERVER_EOF
)
263 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
266 /* connect to para_server and register the client task */
267 static int client_connect(struct client_task
*ct
)
272 ret
= makesock(AF_UNSPEC
, IPPROTO_TCP
, 0, ct
->conf
.hostname_arg
,
273 ct
->conf
.server_port_arg
);
277 ct
->status
= CL_CONNECTED
;
278 ret
= mark_fd_nonblocking(ct
->rc4c
.fd
);
281 ct
->task
.pre_select
= client_pre_select
;
282 ct
->task
.post_select
= client_post_select
;
283 sprintf(ct
->task
.status
, "client");
284 register_task(&ct
->task
);
293 * Open connection to para_server.
295 * \param argc Usual argument count.
296 * \param argv Usual argument vector.
297 * \param ct_ptr Points to dynamically allocated and initialized client task
298 * struct upon successful return.
299 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
301 * Check the command line options given by \a argc and argv, set default values
302 * for user name and rsa key file, read further option from the config file.
303 * Finally, establish a connection to para_server.
307 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
310 char *home
= para_homedir();
312 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
314 ct
->buf
= para_malloc(CLIENT_BUFSIZE
);
317 ret
= -E_CLIENT_SYNTAX
;
318 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
320 HANDLE_VERSION_FLAG("client", ct
->conf
);
321 ret
= -E_CLIENT_SYNTAX
;
322 if (!ct
->conf
.inputs_num
)
324 ct
->user
= ct
->conf
.user_given
?
325 para_strdup(ct
->conf
.user_arg
) : para_logname();
327 ct
->key_file
= ct
->conf
.key_file_given
?
328 para_strdup(ct
->conf
.key_file_arg
) :
329 make_message("%s/.paraslash/key.%s", home
, ct
->user
);
331 ct
->config_file
= ct
->conf
.config_file_given
?
332 para_strdup(ct
->conf
.config_file_arg
) :
333 make_message("%s/.paraslash/client.conf", home
);
334 ret
= file_exists(ct
->config_file
);
335 if (!ret
&& ct
->conf
.config_file_given
) {
340 struct client_cmdline_parser_params params
= {
344 .check_ambiguity
= 0,
348 if (client_cmdline_parser_config_file(ct
->config_file
,
353 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
354 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
355 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
356 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
357 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
358 ct
->conf
.server_port_arg
);
359 ret
= client_connect(ct
);
363 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));