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"
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
)
43 free(ct
->config_file
);
45 client_cmdline_parser_free(&ct
->conf
);
50 * The preselect hook for server commands.
52 * \param s Pointer to the scheduler.
53 * \param t Pointer to the task struct for this command.
55 * The task pointer must contain a pointer to the initialized client data
56 * structure as it is returned by client_open().
58 * This function checks the state of the connection and adds the file descriptor
59 * of the connection to the read or write fd set of \a s accordingly.
61 * \sa register_task() client_open(), struct sched, struct task.
63 static void client_pre_select(struct sched
*s
, struct task
*t
)
65 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
74 case CL_SENT_CH_RESPONSE
:
76 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
80 case CL_RECEIVED_WELCOME
:
81 case CL_RECEIVED_CHALLENGE
:
82 case CL_RECEIVED_PROCEED
:
83 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
88 if (ct
->loaded
< CLIENT_BUFSIZE
- 1) {
89 para_fd_set(ct
->rc4c
.fd
, &s
->rfds
, &s
->max_fileno
);
94 if (!ct
->in_loaded
) /* stdin task not yet started */
97 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
98 para_fd_set(ct
->rc4c
.fd
, &s
->wfds
, &s
->max_fileno
);
102 t
->error
= *ct
->in_error
;
103 s
->timeout
.tv_sec
= 0;
104 s
->timeout
.tv_usec
= 1;
111 static ssize_t
client_recv_buffer(struct client_task
*ct
)
115 if (ct
->status
< CL_SENT_CH_RESPONSE
)
116 ret
= recv_buffer(ct
->rc4c
.fd
, ct
->buf
+ ct
->loaded
,
117 CLIENT_BUFSIZE
- ct
->loaded
);
119 ret
= rc4_recv_buffer(&ct
->rc4c
, ct
->buf
+ ct
->loaded
,
120 CLIENT_BUFSIZE
- ct
->loaded
);
122 return -E_SERVER_EOF
;
129 * The post select hook for client commands.
131 * \param s Pointer to the scheduler.
132 * \param t Pointer to the task struct for this command.
134 * Depending on the current state of the connection and the status of the read
135 * and write fd sets of \a s, this function performs the necessary steps to
136 * authenticate the connection, to send the command given by \a t->private_data
137 * and to receive para_server's output, if any.
139 * \sa struct sched, struct task.
141 static void client_post_select(struct sched
*s
, struct task
*t
)
143 struct client_task
*ct
= container_of(t
, struct client_task
, task
);
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 ct
->loaded
= t
->error
;
175 PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", ct
->loaded
);
176 ct
->status
= CL_RECEIVED_CHALLENGE
;
178 case CL_RECEIVED_CHALLENGE
:
180 /* decrypted challenge/rc4 buffer */
181 unsigned char crypt_buf
[1024];
182 /* the SHA1 of the decrypted challenge */
183 unsigned char challenge_sha1
[HASH_SIZE
];
185 t
->error
= para_decrypt_buffer(ct
->key_file
, crypt_buf
,
186 (unsigned char *)ct
->buf
, ct
->loaded
);
189 sha1_hash((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_sha1
);
190 RC4_set_key(&ct
->rc4c
.send_key
, RC4_KEY_LEN
,
191 crypt_buf
+ CHALLENGE_SIZE
);
192 RC4_set_key(&ct
->rc4c
.recv_key
, RC4_KEY_LEN
,
193 crypt_buf
+ CHALLENGE_SIZE
+ RC4_KEY_LEN
);
194 hash_to_asc(challenge_sha1
, ct
->buf
);
195 PARA_INFO_LOG("--> %s\n", ct
->buf
);
196 t
->error
= send_bin_buffer(ct
->rc4c
.fd
, (char *)challenge_sha1
,
200 ct
->status
= CL_SENT_CH_RESPONSE
;
203 case CL_SENT_CH_RESPONSE
: /* read server response */
205 size_t bytes_received
;
207 t
->error
= client_recv_buffer(ct
);
210 bytes_received
= t
->error
;
211 /* check if server has sent "Proceed" message */
212 t
->error
= -E_CLIENT_AUTH
;
213 if (bytes_received
< PROCEED_MSG_LEN
)
215 if (!strstr(ct
->buf
, PROCEED_MSG
))
217 ct
->status
= CL_RECEIVED_PROCEED
;
221 case CL_RECEIVED_PROCEED
: /* concat args and send command */
224 char *command
= NULL
;
225 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
227 command
= make_message("%s\n%s", command
?
228 command
: "", ct
->conf
.inputs
[i
]);
231 command
= para_strcat(command
, EOC_MSG
"\n");
232 PARA_DEBUG_LOG("--> %s\n", command
);
233 t
->error
= rc4_send_buffer(&ct
->rc4c
, command
);
237 ct
->status
= CL_SENT_COMMAND
;
240 case CL_SENT_COMMAND
:
242 t
->error
= client_recv_buffer(ct
);
245 if (strstr(ct
->buf
, AWAITING_DATA_MSG
))
246 ct
->status
= CL_SENDING
;
248 ct
->status
= CL_RECEIVING
;
251 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
252 t
->error
= rc4_send_bin_buffer(&ct
->rc4c
, ct
->inbuf
,
259 t
->error
= client_recv_buffer(ct
);
265 if (t
->error
!= -E_SERVER_EOF
)
266 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
269 /* connect to para_server and register the client task */
270 static int client_connect(struct client_task
*ct
)
275 ret
= makesock(AF_UNSPEC
, IPPROTO_TCP
, 0, ct
->conf
.hostname_arg
,
276 ct
->conf
.server_port_arg
);
280 ct
->status
= CL_CONNECTED
;
281 ret
= mark_fd_nonblocking(ct
->rc4c
.fd
);
284 ct
->task
.pre_select
= client_pre_select
;
285 ct
->task
.post_select
= client_post_select
;
286 sprintf(ct
->task
.status
, "client");
287 register_task(&ct
->task
);
296 * Open connection to para_server.
298 * \param argc Usual argument count.
299 * \param argv Usual argument vector.
300 * \param ct_ptr Points to dynamically allocated and initialized client task
301 * struct upon successful return.
302 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
304 * Check the command line options given by \a argc and argv, set default values
305 * for user name and rsa key file, read further option from the config file.
306 * Finally, establish a connection to para_server.
310 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
313 char *home
= para_homedir();
315 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
317 ct
->buf
= para_malloc(CLIENT_BUFSIZE
);
320 ret
= -E_CLIENT_SYNTAX
;
321 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
323 HANDLE_VERSION_FLAG("client", ct
->conf
);
324 ret
= -E_CLIENT_SYNTAX
;
325 if (!ct
->conf
.inputs_num
)
327 ct
->user
= ct
->conf
.user_given
?
328 para_strdup(ct
->conf
.user_arg
) : para_logname();
330 ct
->key_file
= ct
->conf
.key_file_given
?
331 para_strdup(ct
->conf
.key_file_arg
) :
332 make_message("%s/.paraslash/key.%s", home
, ct
->user
);
334 ct
->config_file
= ct
->conf
.config_file_given
?
335 para_strdup(ct
->conf
.config_file_arg
) :
336 make_message("%s/.paraslash/client.conf", home
);
337 ret
= file_exists(ct
->config_file
);
338 if (!ret
&& ct
->conf
.config_file_given
) {
343 struct client_cmdline_parser_params params
= {
347 .check_ambiguity
= 0,
351 if (client_cmdline_parser_config_file(ct
->config_file
,
356 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
357 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
358 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
359 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
360 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
361 ct
->conf
.server_port_arg
);
362 ret
= client_connect(ct
);
366 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));