Пример #1
0
int print_working_directory(int command_socket)
{
  char message[] = "PWD\n";
  int length = strlen(message);
  char buf[DATA_SIZE];
  int bytes = 0;

  if (send(command_socket, message, strlen(message), 0) == -1)
    {
      perror("Could not send PWD command to the server!\n");
      return -1;
    }

  if ((bytes = recv(command_socket, buf, DATA_SIZE -1, 0)) == -1)
    {
      perror("recv");
      return -1;
    }

  if (parse_response_code(getFTPresponse_code(buf)) != PWD_ACCEPT)
    {
      printf("Server did not accept PWD command!\n");
      return -1;
    }

  printf("%s", buf);

  return 0;
  
}
Пример #2
0
int pasv_request(int command_socket, char *pasv_response, int buffer_size)
{
  char message[] = "PASV\n";
  char buf[DATA_SIZE];
  int bytes = 0;
  
  if (send(command_socket, message, strlen(message), 0) == -1)
    {
      perror("send");
      return -1;
    }

  if ((bytes = recv(command_socket, buf, DATA_SIZE - 1, 0)) == -1)
    {
      perror("recv");
      return -1;
    }

  buf[bytes] = '\0';
  printf("%s\n", buf);

  // The server return the code 227 if it accept the PASV request
  if (parse_response_code(getFTPresponse_code(buf)) != PASV_SUCCESS)
    {
      printf("PASV request not accepted!\n");
      return -1;
    }

  strncpy(pasv_response, buf, buffer_size);

  return 1;
}
Пример #3
0
int change_directory(int command_socket, char *path)
{
  char message[100] = "CWD ";
  strcat(message, path);
  int length = strlen(message);
  message[length] = '\n';
  message[length + 1] = '\0';

  char buf[DATA_SIZE];
  int bytes = 0;

  if (send(command_socket, message, strlen(message), 0) == -1)
    {
      perror("Could not send CWD command to server!\n");
      return -1;
    }

  if ((bytes = recv(command_socket, buf, DATA_SIZE -1, 0) == -1))
    {
      perror("recv");
      return -1;
    }

  if (parse_response_code(getFTPresponse_code(buf)) != REQUEST_ACCEPTED)
    {
      printf("Server rejected CWD command!\n");
      return -1;
    }

  return 0;
}
Пример #4
0
static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
{
	struct imap *imap = ctx->imap;
	struct imap_cmd *cmdp, **pcmdp;
	char *cmd, *arg, *arg1;
	int n, resp, resp2, tag;

	for (;;) {
		if (buffer_gets(&imap->buf, &cmd))
			return RESP_BAD;

		arg = next_arg(&cmd);
		if (*arg == '*') {
			arg = next_arg(&cmd);
			if (!arg) {
				fprintf(stderr, "IMAP error: unable to parse untagged response\n");
				return RESP_BAD;
			}

			if (!strcmp("NAMESPACE", arg)) {
				/* rfc2342 NAMESPACE response. */
				skip_list(&cmd); /* Personal mailboxes */
				skip_list(&cmd); /* Others' mailboxes */
				skip_list(&cmd); /* Shared mailboxes */
			} else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
				   !strcmp("NO", arg) || !strcmp("BYE", arg)) {
				if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
					return resp;
			} else if (!strcmp("CAPABILITY", arg)) {
				parse_capability(imap, cmd);
			} else if ((arg1 = next_arg(&cmd))) {
				; /*
				   * Unhandled response-data with at least two words.
				   * Ignore it.
				   *
				   * NEEDSWORK: Previously this case handled '<num> EXISTS'
				   * and '<num> RECENT' but as a probably-unintended side
				   * effect it ignores other unrecognized two-word
				   * responses.  imap-send doesn't ever try to read
				   * messages or mailboxes these days, so consider
				   * eliminating this case.
				   */
			} else {
				fprintf(stderr, "IMAP error: unable to parse untagged response\n");
				return RESP_BAD;
			}
		} else if (!imap->in_progress) {
			fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
			return RESP_BAD;
		} else if (*arg == '+') {
			/* This can happen only with the last command underway, as
			   it enforces a round-trip. */
			cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
			       offsetof(struct imap_cmd, next));
			if (cmdp->cb.data) {
				n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
				free(cmdp->cb.data);
				cmdp->cb.data = NULL;
				if (n != (int)cmdp->cb.dlen)
					return RESP_BAD;
			} else if (cmdp->cb.cont) {
				if (cmdp->cb.cont(ctx, cmdp, cmd))
					return RESP_BAD;
			} else {
				fprintf(stderr, "IMAP error: unexpected command continuation request\n");
				return RESP_BAD;
			}
			if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
				return RESP_BAD;
			if (!cmdp->cb.cont)
				imap->literal_pending = 0;
			if (!tcmd)
				return DRV_OK;
		} else {