Example #1
0
static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
				char **(completion_fn)(const char *text, int start, int end))
{
	char *line = NULL;
	int fd = x_fileno(x_stdin);
	char *ret;

	/* Prompt might be NULL in non-interactive mode. */
	if (prompt) {
		x_fprintf(x_stdout, "%s", prompt);
		x_fflush(x_stdout);
	}

	line = (char *)malloc(BUFSIZ);
	if (!line) {
		return NULL;
	}

	while (!smb_rl_done) {
		struct pollfd pfd;

		ZERO_STRUCT(pfd);
		pfd.fd = fd;
		pfd.events = POLLIN|POLLHUP;

		if (sys_poll_intr(&pfd, 1, 5000) == 1) {
			ret = x_fgets(line, BUFSIZ, x_stdin);
			if (ret == 0) {
				SAFE_FREE(line);
			}
			return ret;
		}
		if (callback) {
			callback();
		}
	}
	SAFE_FREE(line);
	return NULL;
}
static void filter_child(int c, struct sockaddr_storage *dest_ss)
{
	NTSTATUS status;
	int s = -1;
	char packet[128*1024];

	/* we have a connection from a new client, now connect to the server */
	status = open_socket_out(dest_ss, TCP_SMB_PORT, LONG_CONNECT_TIMEOUT, &s);
	if (!NT_STATUS_IS_OK(status)) {
		char addr[INET6_ADDRSTRLEN];
		if (dest_ss) {
			print_sockaddr(addr, sizeof(addr), dest_ss);
		}

		d_printf("Unable to connect to %s (%s)\n",
			 dest_ss?addr:"NULL", nt_errstr(status));
		exit(1);
	}

	while (c != -1 || s != -1) {
		struct pollfd fds[2];
		int num_fds, ret;

		memset(fds, 0, sizeof(struct pollfd) * 2);
		fds[0].fd = -1;
		fds[1].fd = -1;
		num_fds = 0;

		if (s != -1) {
			fds[num_fds].fd = s;
			fds[num_fds].events = POLLIN|POLLHUP;
			num_fds += 1;
		}
		if (c != -1) {
			fds[num_fds].fd = c;
			fds[num_fds].events = POLLIN|POLLHUP;
			num_fds += 1;
		}

		ret = sys_poll_intr(fds, num_fds, -1);
		if (ret <= 0) {
			continue;
		}

		/*
		 * find c in fds and see if it's readable
		 */
		if ((c != -1) &&
		    (((fds[0].fd == c)
		      && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
		     ((fds[1].fd == c)
		      && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
			size_t len;
			if (!NT_STATUS_IS_OK(receive_smb_raw(
							c, packet, sizeof(packet),
							0, 0, &len))) {
				d_printf("client closed connection\n");
				exit(0);
			}
			filter_request(packet, len);
			if (!send_smb(s, packet)) {
				d_printf("server is dead\n");
				exit(1);
			}			
		}

		/*
		 * find s in fds and see if it's readable
		 */
		if ((s != -1) &&
		    (((fds[0].fd == s)
		      && (fds[0].revents & (POLLIN|POLLHUP|POLLERR))) ||
		     ((fds[1].fd == s)
		      && (fds[1].revents & (POLLIN|POLLHUP|POLLERR))))) {
			size_t len;
			if (!NT_STATUS_IS_OK(receive_smb_raw(
							s, packet, sizeof(packet),
							0, 0, &len))) {
				d_printf("server closed connection\n");
				exit(0);
			}
			filter_reply(packet);
			if (!send_smb(c, packet)) {
				d_printf("client is dead\n");
				exit(1);
			}			
		}
	}
	d_printf("Connection closed\n");
	exit(0);
}