2 * Copyright (C) 1997-2007 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 */
12 #include "client.cmdline.h"
18 #include "client.cmdline.h"
23 * rc4 encrypt data before sending
25 * \param len the number of bytes to encrypt
26 * \param indata pointer to the input data of length \a len to be encrypted
27 * \param outdata pointer that holds the encrypted data after return
28 * \param private_data pointer to the private client data containing
31 static void rc4_send(unsigned long len, const unsigned char *indata,
32 unsigned char *outdata, void *private_data)
34 struct private_client_data *pcd = private_data;
35 RC4(&pcd->rc4_send_key, len, indata, outdata);
39 * rc4 decrypt received data
41 * \param len the number of bytes to decrypt
42 * \param indata pointer to the input data of length \a len
43 * \param outdata pointer that holds the decrypted data after return
44 * \param private_data pointer to the private client data containing
47 static void rc4_recv(unsigned long len, const unsigned char *indata,
48 unsigned char *outdata, void *private_data)
50 struct private_client_data *pcd = private_data;
51 RC4(&pcd->rc4_recv_key, len, indata, outdata);
55 * close the connection to para_server and free all resources
57 * \param pcd pointer to the client data
61 void client_close(struct private_client_data *pcd)
66 disable_crypt(pcd->fd);
70 free(pcd->config_file);
72 client_cmdline_parser_free(&pcd->conf);
76 static int client_connect(struct private_client_data *pcd)
80 struct sockaddr_in their_addr;
83 ret = get_host_info(pcd->conf.hostname_arg, &he);
92 init_sockaddr(&their_addr, pcd->conf.server_port_arg, he);
93 ret = para_connect(pcd->fd, &their_addr);
96 pcd->status = CL_CONNECTED;
97 ret = mark_fd_nonblock(pcd->fd);
100 pcd->task.pre_select = client_pre_select;
101 pcd->task.post_select = client_post_select;
102 pcd->task.private_data = pcd;
103 sprintf(pcd->task.status, "client");
104 register_task(&pcd->task);
113 * open connection to para_server
115 * \param argc usual argument count
116 * \param argv usual argument vector
117 * \param pcd_ptr points to dynamically allocated and initialized private client data
118 * upon successful return
120 * Check the command line options given by \a argc and argv, set default values
121 * for user name and rsa key file, read further option from the config file.
122 * Finally, establish a connection to para_server.
124 * \return Positive on success, negative on errors.
126 int client_open(int argc, char *argv[], struct private_client_data **pcd_ptr)
128 char *home = para_homedir();
131 struct private_client_data *pcd =
132 para_calloc(sizeof(struct private_client_data));
136 ret = client_cmdline_parser(argc, argv, &pcd->conf);
137 HANDLE_VERSION_FLAG("client", pcd->conf);
138 ret = -E_CLIENT_SYNTAX;
139 if (!pcd->conf.inputs_num)
141 pcd->user = pcd->conf.user_given?
142 para_strdup(pcd->conf.user_arg) : para_logname();
144 pcd->key_file = pcd->conf.key_file_given?
145 para_strdup(pcd->conf.key_file_arg) :
146 make_message("%s/.paraslash/key.%s", home, pcd->user);
148 pcd->config_file = pcd->conf.config_file_given?
149 para_strdup(pcd->conf.config_file_arg) :
150 make_message("%s/.paraslash/client.conf", home);
151 ret = stat(pcd->config_file, &statbuf);
152 if (ret && pcd->conf.config_file_given) {
157 client_cmdline_parser_configfile(pcd->config_file,
158 &pcd->conf, 0, 0, 0);
160 PARA_INFO_LOG("loglevel: %d\n", pcd->conf.loglevel_arg);
161 PARA_INFO_LOG("config_file: %s\n", pcd->config_file);
162 PARA_INFO_LOG("key_file: %s\n", pcd->key_file);
163 PARA_NOTICE_LOG("connecting %s:%d\n", pcd->conf.hostname_arg,
164 pcd->conf.server_port_arg);
165 ret = client_connect(pcd);
169 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
177 * the preselect hook for server commands
179 * \param s pointer to the scheduler
180 * \param t pointer to the task struct for this command
182 * The task pointer must contain a pointer to the initialized client data
183 * structure as it is returned by client_open().
185 * This function checks the state of the connection and adds the file descriptor
186 * of the connection to the read or write fd set of \a s accordingly.
188 * \sa register_task() client_open(), struct sched, struct task
190 void client_pre_select(struct sched *s, struct task *t)
192 struct private_client_data *pcd = t->private_data;
199 switch (pcd->status) {
202 case CL_SENT_CH_RESPONSE:
203 case CL_SENT_COMMAND:
204 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
208 case CL_RECEIVED_WELCOME:
209 case CL_RECEIVED_CHALLENGE:
210 case CL_RECEIVED_PROCEED:
211 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
216 if (pcd->loaded < CLIENT_BUFSIZE - 1) {
217 para_fd_set(pcd->fd, &s->rfds, &s->max_fileno);
222 if (*pcd->in_loaded) {
223 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
224 para_fd_set(pcd->fd, &s->wfds, &s->max_fileno);
228 t->ret = -E_INPUT_EOF;
229 s->timeout.tv_sec = 0;
230 s->timeout.tv_usec = 1;
237 static ssize_t client_recv_buffer(struct private_client_data *pcd)
239 ssize_t ret = recv_buffer(pcd->fd, pcd->buf + pcd->loaded,
240 CLIENT_BUFSIZE - pcd->loaded);
242 return -E_SERVER_EOF;
250 * the post select hook for client commands
252 * \param s pointer to the scheduler
253 * \param t pointer to the task struct for this command
255 * Depending on the current state of the connection and the status of the read
256 * and write fd sets of \a s, this function performs the neccessary steps to
257 * authenticate the connection, to send the commmand given by \a
258 * t->private_data and to receive para_server's output, if any.
260 * \sa struct sched, struct task
262 void client_post_select(struct sched *s, struct task *t)
264 struct private_client_data *pcd = t->private_data;
266 // PARA_INFO_LOG("status %d\n", pcd->status);
270 if (!pcd->check_r && !pcd->check_w)
272 if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
274 if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
276 switch (pcd->status) {
277 case CL_CONNECTED: /* receive welcome message */
278 t->ret = client_recv_buffer(pcd);
280 pcd->status = CL_RECEIVED_WELCOME;
282 case CL_RECEIVED_WELCOME: /* send auth command */
283 sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
284 "" : "rc4 ", pcd->user);
285 PARA_INFO_LOG("--> %s\n", pcd->buf);
286 t->ret = send_buffer(pcd->fd, pcd->buf);
288 pcd->status = CL_SENT_AUTH;
290 case CL_SENT_AUTH: /* receive challenge number */
292 t->ret = client_recv_buffer(pcd);
296 t->ret = -E_INVALID_CHALLENGE;
297 PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
300 PARA_INFO_LOG("%s", "<-- [challenge]\n");
301 /* decrypt challenge number */
302 t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
303 (unsigned char *) pcd->buf, 64);
305 pcd->status = CL_RECEIVED_CHALLENGE;
307 case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
308 PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
309 t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
312 pcd->status = CL_SENT_CH_RESPONSE;
314 case CL_SENT_CH_RESPONSE: /* read server response */
316 size_t bytes_received;
317 unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
319 t->ret = client_recv_buffer(pcd);
322 bytes_received = t->ret;
323 PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
324 "info ++++\n", pcd->buf);
325 /* check if server has sent "Proceed" message */
326 t->ret = -E_CLIENT_AUTH;
327 if (!strstr(pcd->buf, PROCEED_MSG))
330 pcd->status = CL_RECEIVED_PROCEED;
331 if (bytes_received < PROCEED_MSG_LEN + 32)
333 PARA_INFO_LOG("%s", "decrypting session key\n");
334 t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
335 (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
336 bytes_received - PROCEED_MSG_LEN - 1);
339 RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
340 RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
341 enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
343 case CL_RECEIVED_PROCEED: /* concat args and send command */
346 char *command = NULL;
347 for (i = 0; i < pcd->conf.inputs_num; i++) {
349 command = make_message("%s\n%s", command?
350 command : "", pcd->conf.inputs[i]);
353 command = para_strcat(command, EOC_MSG "\n");
354 PARA_DEBUG_LOG("--> %s\n", command);
355 t->ret = send_buffer(pcd->fd, command);
358 pcd->status = CL_SENT_COMMAND;
361 case CL_SENT_COMMAND:
363 t->ret = client_recv_buffer(pcd);
366 t->ret = -E_HANDSHAKE_COMPLETE;
367 if (strstr(pcd->buf, AWAITING_DATA_MSG))
368 pcd->status = CL_SENDING;
370 pcd->status = CL_RECEIVING;
372 case CL_SENDING: /* FIXME: might block */
373 PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
374 t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
377 *pcd->in_loaded = 0; /* FIXME: short writes */
380 t->ret = client_recv_buffer(pcd);