Beispiel #1
0
int main(int argc, char **argv)
{
	int val = 1;
	char *st = NULL;
	const char *command = NULL;
	const char *prog = argv[0];
	int ch;

	msg->magic = htonl(EAD_MAGIC);
	msg->sid = 0;

	memset(&local, 0, sizeof(local));
	memset(&remote, 0, sizeof(remote));

	remote.sin_family = AF_INET;
	remote.sin_addr.s_addr = 0xffffffff;
	remote.sin_port = htons(EAD_PORT);

	local.sin_family = AF_INET;
	local.sin_addr.s_addr = INADDR_ANY;
	local.sin_port = 0;

	while ((ch = getopt(argc, argv, "b:s:h")) != -1) {
		switch(ch) {
		case 's':
			inet_aton(optarg, &serverip);
			break;
		case 'b':
			inet_aton(optarg, &remote.sin_addr);
			break;
		case 'h':
			return usage(prog);
		}
	}
	argv += optind;
	argc -= optind;

	switch(argc) {
	case 3:
		command = argv[2];
		/* fall through */
	case 2:
		username = argv[1];
		st = strchr(username, ':');
		if (st) {
			*st = 0;
			st++;
			strncpy(password, st, sizeof(password));
			password[sizeof(password) - 1] = 0;
			/* hide command line password */
			memset(st, 0, strlen(st));
		}
		/* fall through */
	case 1:
		nid = strtoul(argv[0], &st, 16);
		if (st && st[0] != 0)
			return usage(prog);
		/* fall through */
	case 0:
		break;
	default:
		return usage(prog);
	}

	msg->nid = htons(nid);
	s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (s < 0) {
		perror("socket");
		return -1;
	}

	setsockopt(s, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));

	if (bind(s, (struct sockaddr *)&local, sizeof(local)) < 0) {
		perror("bind");
		return -1;
	}
	sockflags = fcntl(s, F_GETFL);

	if (!send_ping()) {
		fprintf(stderr, "No devices found\n");
		return 1;
	}

	if (nid == 0xffff)
		return 0;

	if (!username || !password[0])
		return 0;

	if (!send_username()) {
		fprintf(stderr, "Device did not accept user name\n");
		return 1;
	}
	timeout = EAD_TIMEOUT_LONG;
	if (!get_prime()) {
		fprintf(stderr, "Failed to get user password info\n");
		return 1;
	}
	if (!send_a()) {
		fprintf(stderr, "Failed to send local authentication data\n");
		return 1;
	}
	if (!send_auth()) {
		fprintf(stderr, "Authentication failed\n");
		return 1;
	}
	if (!command) {
		fprintf(stderr, "Authentication succesful\n");
		return 0;
	}
	if (!send_command(command)) {
		fprintf(stderr, "Command failed\n");
		return 1;
	}

	return 0;
}
Beispiel #2
0
/* use the "none" authentication question */
int ssh_userauth1_none(ssh_session session, const char *username){
    return send_username(session, username);
}
Beispiel #3
0
int ssh_userauth1_password(ssh_session session, const char *username,
    const char *password) {
  ssh_string pwd = NULL;
  int rc;
  enter_function();
  rc = send_username(session, username);
  if (rc != SSH_AUTH_DENIED) {
    leave_function();
    return rc;
  }

  /* we trick a bit here. A known flaw in SSH1 protocol is that it's
   * easy to guess password sizes.
   * not that sure ...
   */

  /* XXX fix me here ! */
  /* cisco IOS doesn't like when a password is followed by zeroes and random pad. */
  if(1 || strlen(password) >= 128) {
    /* not risky to disclose the size of such a big password .. */
    pwd = ssh_string_from_char(password);
    if (pwd == NULL) {
      leave_function();
      return SSH_AUTH_ERROR;
    }
  } else {
    /* fill the password string from random things. the strcpy
     * ensure there is at least a nul byte after the password.
     * most implementation won't see the garbage at end.
     * why garbage ? because nul bytes will be compressed by
     * gzip and disclose password len.
     */
    pwd = ssh_string_new(128);
    if (pwd == NULL) {
      leave_function();
      return SSH_AUTH_ERROR;
    }
    ssh_get_random( pwd->string, 128, 0);
    strcpy((char *) pwd->string, password);
  }

  if (buffer_add_u8(session->out_buffer, SSH_CMSG_AUTH_PASSWORD) < 0) {
    ssh_string_burn(pwd);
    ssh_string_free(pwd);
    leave_function();
    return SSH_AUTH_ERROR;
  }
  if (buffer_add_ssh_string(session->out_buffer, pwd) < 0) {
    ssh_string_burn(pwd);
    ssh_string_free(pwd);
    leave_function();
    return SSH_AUTH_ERROR;
  }

  ssh_string_burn(pwd);
  ssh_string_free(pwd);
  session->auth_state=SSH_AUTH_STATE_NONE;
  if (packet_send(session) == SSH_ERROR) {
    leave_function();
    return SSH_AUTH_ERROR;
  }
  rc = wait_auth1_status(session);
  leave_function();
  return rc;
}
Beispiel #4
0
int main(int argc, char *argv[]) {
    int sockfd, portno;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    if (argc != 2) {
        printf("Usage: client <filename>");
        exit(0);
    }
    FILE *address_file = fopen(argv[1], "r");

    // Get address of the form www.example.com:1234
    fgets(buffer, BUFFER_SIZE, address_file);
    char *str = strdup(buffer);
    char *web_address = strsep(&str, ":");
    size_t port_number = atoi(strsep(&str, ":"));

    // Get nickname and strip newlines.
    fgets(buffer, BUFFER_SIZE, address_file);
    char nickname[64];
    strtok(buffer, "\n");
    strcpy(nickname, buffer);

    // Get username and strip newlines.
    fgets(buffer, BUFFER_SIZE, address_file);
    char username[64];
    strtok(buffer, "\n");
    strcpy(username, buffer);

    // Get realname and strip newlines
    fgets(buffer, BUFFER_SIZE, address_file);
    char realname[64];
    strtok(buffer, "\n");
    strcpy(realname, buffer);
    fclose(address_file);

    // Connect to server
    // This is based off of the C socket tutorial at
    // http://www.linuxhowtos.org/C_C++/socket.htm
    portno = port_number;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(web_address);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");

    send_nickname(nickname, sockfd);
    send_username(username, realname, sockfd);
    // listen_to_server(sockfd);
    while(1) {
        read_lines(sockfd);
    }
    close(sockfd);
    return 0;
}