-
-}
-
-/**
- * the post select hook for client commands
- *
- * \param s pointer to the scheduler
- * \param t pointer to the task struct for this command
- *
- * Depending on the current state of the connection and the status of the read
- * and write fd sets of \a s, this function performs the necessary steps to
- * authenticate the connection, to send the command given by \a
- * t->private_data and to receive para_server's output, if any.
- *
- * \sa struct sched, struct task
- */
-void client_post_select(struct sched *s, struct task *t)
-{
- struct private_client_data *pcd = t->private_data;
-
-// PARA_INFO_LOG("status %d\n", pcd->status);
- t->ret = 1;
- if (pcd->fd < 0)
- return;
- if (!pcd->check_r && !pcd->check_w)
- return;
- if (pcd->check_r && !FD_ISSET(pcd->fd, &s->rfds))
- return;
- if (pcd->check_w && !FD_ISSET(pcd->fd, &s->wfds))
- return;
- switch (pcd->status) {
- case CL_CONNECTED: /* receive welcome message */
- t->ret = client_recv_buffer(pcd);
- if (t->ret > 0)
- pcd->status = CL_RECEIVED_WELCOME;
- return;
- case CL_RECEIVED_WELCOME: /* send auth command */
- sprintf(pcd->buf, "auth %s%s", pcd->conf.plain_given?
- "" : "rc4 ", pcd->user);
- PARA_INFO_LOG("--> %s\n", pcd->buf);
- t->ret = send_buffer(pcd->fd, pcd->buf);
- if (t->ret >= 0)
- pcd->status = CL_SENT_AUTH;
- return;
- case CL_SENT_AUTH: /* receive challenge number */
- pcd->loaded = 0;
- t->ret = client_recv_buffer(pcd);
- if (t->ret < 0)
- return;
- if (t->ret != 64) {
- t->ret = -E_INVALID_CHALLENGE;
- PARA_ERROR_LOG("received the following: %s\n", pcd->buf);
- return;
- }
- PARA_INFO_LOG("%s", "<-- [challenge]\n");
- /* decrypt challenge number */
- t->ret = para_decrypt_challenge(pcd->key_file, &pcd->challenge_nr,
- (unsigned char *) pcd->buf, 64);
- if (t->ret > 0)
- pcd->status = CL_RECEIVED_CHALLENGE;
- return;
- case CL_RECEIVED_CHALLENGE: /* send decrypted challenge */
- PARA_INFO_LOG("--> %lu\n", pcd->challenge_nr);
- t->ret = send_va_buffer(pcd->fd, "%s%lu", CHALLENGE_RESPONSE_MSG,
- pcd->challenge_nr);
- if (t->ret > 0)
- pcd->status = CL_SENT_CH_RESPONSE;
- return;
- case CL_SENT_CH_RESPONSE: /* read server response */
- {
- size_t bytes_received;
- unsigned char rc4_buf[2 * RC4_KEY_LEN] = "";
- pcd->loaded = 0;
- t->ret = client_recv_buffer(pcd);
- if (t->ret < 0)
- return;
- bytes_received = t->ret;
- PARA_DEBUG_LOG("++++ server info ++++\n%s\n++++ end of server "
- "info ++++\n", pcd->buf);
- /* check if server has sent "Proceed" message */
- t->ret = -E_CLIENT_AUTH;
- if (!strstr(pcd->buf, PROCEED_MSG))
- return;
- t->ret = 1;
- pcd->status = CL_RECEIVED_PROCEED;
- if (bytes_received < PROCEED_MSG_LEN + 32)
- return;
- PARA_INFO_LOG("%s", "decrypting session key\n");
- t->ret = para_decrypt_buffer(pcd->key_file, rc4_buf,
- (unsigned char *)pcd->buf + PROCEED_MSG_LEN + 1,
- bytes_received - PROCEED_MSG_LEN - 1);
- if (t->ret < 0)
- return;
- RC4_set_key(&pcd->rc4_send_key, RC4_KEY_LEN, rc4_buf);
- RC4_set_key(&pcd->rc4_recv_key, RC4_KEY_LEN, rc4_buf + RC4_KEY_LEN);
- enable_crypt(pcd->fd, rc4_recv, rc4_send, pcd);
- }
- case CL_RECEIVED_PROCEED: /* concat args and send command */
- {
- int i;
- char *command = NULL;
- for (i = 0; i < pcd->conf.inputs_num; i++) {
- char *tmp = command;
- command = make_message("%s\n%s", command?
- command : "", pcd->conf.inputs[i]);
- free(tmp);
- }
- command = para_strcat(command, EOC_MSG "\n");
- PARA_DEBUG_LOG("--> %s\n", command);
- t->ret = send_buffer(pcd->fd, command);
- free(command);
- if (t->ret > 0)
- pcd->status = CL_SENT_COMMAND;
- return;
- }
- case CL_SENT_COMMAND:
- pcd->loaded = 0;
- t->ret = client_recv_buffer(pcd);
- if (t->ret < 0)
- return;
- t->ret = -E_HANDSHAKE_COMPLETE;
- if (strstr(pcd->buf, AWAITING_DATA_MSG))
- pcd->status = CL_SENDING;
- else
- pcd->status = CL_RECEIVING;
- return;
- case CL_SENDING: /* FIXME: might block */
- PARA_INFO_LOG("loaded: %zd\n", *pcd->in_loaded);
- t->ret = send_bin_buffer(pcd->fd, pcd->inbuf, *pcd->in_loaded);
- if (t->ret < 0)
- return;
- *pcd->in_loaded = 0; /* FIXME: short writes */
- return;
- case CL_RECEIVING:
- t->ret = client_recv_buffer(pcd);
- return;
- }