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
);
144 unsigned char crypt_buf
[1024];
149 if (!ct
->check_r
&& !ct
->check_w
)
151 if (ct
->check_r
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->rfds
))
153 if (ct
->check_w
&& !FD_ISSET(ct
->rc4c
.fd
, &s
->wfds
))
155 switch (ct
->status
) {
156 case CL_CONNECTED
: /* receive welcome message */
157 t
->error
= client_recv_buffer(ct
);
160 ct
->status
= CL_RECEIVED_WELCOME
;
162 case CL_RECEIVED_WELCOME
: /* send auth command */
163 sprintf(ct
->buf
, AUTH_REQUEST_MSG
"%s", ct
->user
);
164 PARA_INFO_LOG("--> %s\n", ct
->buf
);
165 t
->error
= send_buffer(ct
->rc4c
.fd
, ct
->buf
);
168 ct
->status
= CL_SENT_AUTH
;
170 case CL_SENT_AUTH
: /* receive challenge and rc4 keys */
172 t
->error
= client_recv_buffer(ct
);
175 PARA_INFO_LOG("<-- [challenge] (%d bytes)\n", t
->error
);
176 /* decrypt challenge/rc4 buffer */
177 t
->error
= para_decrypt_buffer(ct
->key_file
, crypt_buf
,
178 (unsigned char *)ct
->buf
, t
->error
);
181 ct
->status
= CL_RECEIVED_CHALLENGE
;
182 RC4_set_key(&ct
->rc4c
.send_key
, RC4_KEY_LEN
,
183 crypt_buf
+ CHALLENGE_SIZE
);
184 RC4_set_key(&ct
->rc4c
.recv_key
, RC4_KEY_LEN
,
185 crypt_buf
+ CHALLENGE_SIZE
+ RC4_KEY_LEN
);
187 case CL_RECEIVED_CHALLENGE
:
189 unsigned char challenge_sha1
[HASH_SIZE
];
190 /* send sha1 of decrypted challenge */
191 sha1_hash((char *)crypt_buf
, CHALLENGE_SIZE
, challenge_sha1
);
192 hash_to_asc(challenge_sha1
, ct
->buf
);
193 PARA_INFO_LOG("--> %s\n", ct
->buf
);
194 t
->error
= send_bin_buffer(ct
->rc4c
.fd
, (char *)challenge_sha1
,
198 ct
->status
= CL_SENT_CH_RESPONSE
;
201 case CL_SENT_CH_RESPONSE
: /* read server response */
203 size_t bytes_received
;
205 t
->error
= client_recv_buffer(ct
);
208 bytes_received
= t
->error
;
209 /* check if server has sent "Proceed" message */
210 t
->error
= -E_CLIENT_AUTH
;
211 if (bytes_received
< PROCEED_MSG_LEN
)
213 if (!strstr(ct
->buf
, PROCEED_MSG
))
215 ct
->status
= CL_RECEIVED_PROCEED
;
219 case CL_RECEIVED_PROCEED
: /* concat args and send command */
222 char *command
= NULL
;
223 for (i
= 0; i
< ct
->conf
.inputs_num
; i
++) {
225 command
= make_message("%s\n%s", command
?
226 command
: "", ct
->conf
.inputs
[i
]);
229 command
= para_strcat(command
, EOC_MSG
"\n");
230 PARA_DEBUG_LOG("--> %s\n", command
);
231 t
->error
= rc4_send_buffer(&ct
->rc4c
, command
);
235 ct
->status
= CL_SENT_COMMAND
;
238 case CL_SENT_COMMAND
:
240 t
->error
= client_recv_buffer(ct
);
243 if (strstr(ct
->buf
, AWAITING_DATA_MSG
))
244 ct
->status
= CL_SENDING
;
246 ct
->status
= CL_RECEIVING
;
249 PARA_INFO_LOG("loaded: %zd\n", *ct
->in_loaded
);
250 t
->error
= rc4_send_bin_buffer(&ct
->rc4c
, ct
->inbuf
,
257 t
->error
= client_recv_buffer(ct
);
263 if (t
->error
!= -E_SERVER_EOF
)
264 PARA_ERROR_LOG("%s\n", para_strerror(-t
->error
));
267 /* connect to para_server and register the client task */
268 static int client_connect(struct client_task
*ct
)
273 ret
= makesock(AF_UNSPEC
, IPPROTO_TCP
, 0, ct
->conf
.hostname_arg
,
274 ct
->conf
.server_port_arg
);
278 ct
->status
= CL_CONNECTED
;
279 ret
= mark_fd_nonblocking(ct
->rc4c
.fd
);
282 ct
->task
.pre_select
= client_pre_select
;
283 ct
->task
.post_select
= client_post_select
;
284 sprintf(ct
->task
.status
, "client");
285 register_task(&ct
->task
);
294 * Open connection to para_server.
296 * \param argc Usual argument count.
297 * \param argv Usual argument vector.
298 * \param ct_ptr Points to dynamically allocated and initialized client task
299 * struct upon successful return.
300 * \param loglevel If not \p NULL, the number of the loglevel is stored here.
302 * Check the command line options given by \a argc and argv, set default values
303 * for user name and rsa key file, read further option from the config file.
304 * Finally, establish a connection to para_server.
308 int client_open(int argc
, char *argv
[], struct client_task
**ct_ptr
,
311 char *home
= para_homedir();
313 struct client_task
*ct
= para_calloc(sizeof(struct client_task
));
315 ct
->buf
= para_malloc(CLIENT_BUFSIZE
);
318 ret
= -E_CLIENT_SYNTAX
;
319 if (client_cmdline_parser(argc
, argv
, &ct
->conf
))
321 HANDLE_VERSION_FLAG("client", ct
->conf
);
322 ret
= -E_CLIENT_SYNTAX
;
323 if (!ct
->conf
.inputs_num
)
325 ct
->user
= ct
->conf
.user_given
?
326 para_strdup(ct
->conf
.user_arg
) : para_logname();
328 ct
->key_file
= ct
->conf
.key_file_given
?
329 para_strdup(ct
->conf
.key_file_arg
) :
330 make_message("%s/.paraslash/key.%s", home
, ct
->user
);
332 ct
->config_file
= ct
->conf
.config_file_given
?
333 para_strdup(ct
->conf
.config_file_arg
) :
334 make_message("%s/.paraslash/client.conf", home
);
335 ret
= file_exists(ct
->config_file
);
336 if (!ret
&& ct
->conf
.config_file_given
) {
341 struct client_cmdline_parser_params params
= {
345 .check_ambiguity
= 0,
349 if (client_cmdline_parser_config_file(ct
->config_file
,
354 *loglevel
= get_loglevel_by_name(ct
->conf
.loglevel_arg
);
355 PARA_INFO_LOG("loglevel: %s\n", ct
->conf
.loglevel_arg
);
356 PARA_INFO_LOG("config_file: %s\n", ct
->config_file
);
357 PARA_INFO_LOG("key_file: %s\n", ct
->key_file
);
358 PARA_NOTICE_LOG("connecting %s:%d\n", ct
->conf
.hostname_arg
,
359 ct
->conf
.server_port_arg
);
360 ret
= client_connect(ct
);
364 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));