/* return true if access should be allowed to a service for a socket */ BOOL check_access(int sock, char *allow_list, char *deny_list) { BOOL ret = False; BOOL only_ip = False; char *deny = NULL; char *allow = NULL; DEBUG(10,("check_access: allow = %s, deny = %s\n", allow_list ? allow_list : "NULL", deny_list ? deny_list : "NULL")); if (deny_list) deny = strdup(deny_list); if (allow_list) allow = strdup(allow_list); if ((!deny || *deny==0) && (!allow || *allow==0)) ret = True; if (!ret) { /* bypass gethostbyaddr() calls if the lists only contain IP addrs */ if (only_ipaddrs_in_list(allow) && only_ipaddrs_in_list(deny)) { only_ip = True; DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n")); ret = allow_access(deny,allow, "", get_socket_addr(sock)); } else { DEBUG (3, ("check_access: hostnames in host allow/deny list.\n")); ret = allow_access(deny,allow, get_socket_name(sock), get_socket_addr(sock)); } if (ret) { DEBUG(2,("Allowed connection from %s (%s)\n", only_ip ? "" : get_socket_name(sock), get_socket_addr(sock))); } else { DEBUG(0,("Denied connection from %s (%s)\n", only_ip ? "" : get_socket_name(sock), get_socket_addr(sock))); } } SAFE_FREE(deny); SAFE_FREE(allow); return(ret); }
const char *client_socket_addr(int fd, char *addr, size_t addr_len) { return get_socket_addr(fd, addr, addr_len); }
char *client_socket_addr(void) { return get_socket_addr(client_fd); }
void run(const char *addr, const uint16_t port) { struct sockaddr_in sin; int sockfd; int clientfd; char buf[1024]; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("call socket failed"); } if (get_socket_addr(addr, port, (struct sockaddr*)&sin) < 0) { perror("call get_socket_addr failed"); } if (bind(sockfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) { perror("call bind failed"); } if (listen(sockfd, 5) < 0) { perror("call listen failed"); } if ((clientfd = accept(sockfd, NULL, NULL)) < 0) { perror("call accept failed"); } /* while (1) { if (fgets(buf, 1024, stdin) == NULL) { perror("call fgets error"); break; } else { size_t sended = send(clientfd, buf, strlen(buf) + 1, 0); if (sended < 0) { perror("call send failed"); break; } } size_t recved = recv(clientfd, buf, 1024, 0); if (recved < 0) { perror("call recv failed"); break; } else { if (fputs(buf, stdout) == EOF) { perror("call fputs failed"); break; } } } */ struct event *ev_stdin, *ev_socket, *ev_check; struct event_base *base = event_base_new(); struct timeval sec5 = {10, 0}; ev_stdin = event_new(base, STDIN_FILENO, EV_READ|EV_PERSIST, event_ready_stdin, (int*)&clientfd); ev_socket = event_new(base, clientfd, EV_READ|EV_PERSIST, event_ready_socket, NULL); ev_check = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, event_check_err, (void*)base); event_add(ev_stdin, NULL); event_add(ev_socket, NULL); event_add(ev_check, &sec5); event_base_dispatch(base); close(clientfd); close(sockfd); }