char *get_peer_name(int fd, BOOL force_lookup) { static pstring name_buf; pstring tmp_name; static fstring addr_buf; struct hostent *hp; struct in_addr addr; char *p; /* reverse lookups can be *very* expensive, and in many situations won't work because many networks don't link dhcp with dns. To avoid the delay we avoid the lookup if possible */ if (!lp_hostname_lookups() && (force_lookup == False)) { return get_peer_addr(fd); } p = get_peer_addr(fd); /* it might be the same as the last one - save some DNS work */ if (strcmp(p, addr_buf) == 0) return name_buf; pstrcpy(name_buf,"UNKNOWN"); if (fd == -1) return name_buf; fstrcpy(addr_buf, p); addr = *interpret_addr2(p); /* Look up the remote host name. */ if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) { DEBUG(1,("Gethostbyaddr failed for %s\n",p)); pstrcpy(name_buf, p); } else { pstrcpy(name_buf,(char *)hp->h_name); if (!matchname(name_buf, addr)) { DEBUG(0,("Matchname failed on %s %s\n",name_buf,p)); pstrcpy(name_buf,"UNKNOWN"); } } /* can't pass the same source and dest strings in when you use --enable-developer or the clobber_region() call will get you */ pstrcpy( tmp_name, name_buf ); alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf)); if (strstr(name_buf,"..")) { pstrcpy(name_buf, "UNKNOWN"); } return name_buf; }
int inet_connect(obex_t *handle) { struct sockaddr_in peer; get_peer_addr("localhost", &peer); return OBEX_TransportConnect(handle, (struct sockaddr *) &peer, sizeof(struct sockaddr_in)); }
/*************************************************************************** return the hostname of the client ***************************************************************************/ const char *cgi_remote_addr(void) { if (inetd_server) { char addr[INET6_ADDRSTRLEN]; return get_peer_addr(1,addr,sizeof(addr)); } return getenv("REMOTE_ADDR"); }
int connect_peer(char *peer, char *port) { struct addrinfo *addr = get_peer_addr(peer, port); int sock = get_socket(addr); connect_addr(sock, addr); set_nodelay(sock); freeaddrinfo(addr); return sock; }
/* return true if access should be allowed to a service for a socket */ bool check_access(int sock, const char **allow_list, const char **deny_list) { bool ret = false; bool only_ip = false; if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0)) ret = true; if (!ret) { char addr[INET6_ADDRSTRLEN]; /* Bypass name resolution calls if the lists * only contain IP addrs */ if (only_ipaddrs_in_list(allow_list) && only_ipaddrs_in_list(deny_list)) { only_ip = true; DEBUG (3, ("check_access: no hostnames " "in host allow/deny list.\n")); ret = allow_access(deny_list, allow_list, "", get_peer_addr(sock,addr,sizeof(addr))); } else { DEBUG (3, ("check_access: hostnames in " "host allow/deny list.\n")); ret = allow_access(deny_list, allow_list, get_peer_name(sock,true), get_peer_addr(sock,addr,sizeof(addr))); } if (ret) { DEBUG(2,("Allowed connection from %s (%s)\n", only_ip ? "" : get_peer_name(sock,true), get_peer_addr(sock,addr,sizeof(addr)))); } else { DEBUG(0,("Denied connection from %s (%s)\n", only_ip ? "" : get_peer_name(sock,true), get_peer_addr(sock,addr,sizeof(addr)))); } } return(ret); }
const char *get_tcp_info(int peerfd) { static char info[100] = {0}; SAI localaddr = get_local_name(peerfd); SAI peeraddr = get_peer_addr(peerfd); snprintf(info, sizeof info, "%s:%d->%s:%d", get_addr_ip(&localaddr), get_addr_port(&localaddr), get_addr_ip(&peeraddr), get_addr_port(&peeraddr)); return info; }
const char *client_addr(int fd, char *addr, size_t addrlen) { return get_peer_addr(fd,addr,addrlen); }
static int get_peer_addr(char *name, struct sockaddr_storage *peer) { struct addrinfo hint = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM, .ai_protocol = 0, .ai_flags = AI_ADDRCONFIG }; struct addrinfo *info; int err = getaddrinfo(name, NULL, &hint, &info); if (err) return err; memcpy(peer, info->ai_addr, info->ai_addrlen); freeaddrinfo(info); return 0; } /* * Function main (argc, ) * * Starts all the fun! * */ int main(int argc, char *argv[]) { struct sockaddr_storage peer; obex_object_t *object; int ret; printf("Send and receive files over TCP OBEX\n"); if ( ((argc < 3) || (argc > 3)) && (argc != 1) ) { printf ("Usage: %s [name] [peer]\n", argv[0]); return -1; } handle = OBEX_Init(OBEX_TRANS_INET, obex_event, 0); if (argc == 1) { printf("Waiting for files\n"); ret = TcpOBEX_ServerRegister(handle, NULL, 0); if(ret < 0) { printf("Cannot listen to socket\n"); exit(ret); } while (!finished) { ret = OBEX_HandleInput(handle, 10); if (ret == 0) { printf("Timeout waiting for connection\n"); break; } else if (ret < 0) { printf("Error waiting for connection\n"); break; } } } else { /* We are a client */ ret = get_peer_addr(argv[2], &peer); if (ret) { perror("Bad name"); exit(1); } ret = TcpOBEX_TransportConnect(handle, (struct sockaddr *) &peer, sizeof(peer)); if (ret < 0) { printf("Sorry, unable to connect!\n"); exit(1); } object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT); ret = do_sync_request(handle, object, 0); if( (object = build_object_from_file(handle, argv[1], 0)) ) { ret = do_sync_request(handle, object, 0); } else { perror("PUT failed"); } object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT); ret = do_sync_request(handle, object, 0); printf("PUT successful\n"); } return 0; }
char *client_addr(void) { return get_peer_addr(client_fd); }
void client_setfd(int fd) { client_fd = fd; safe_strcpy(client_ip_string, get_peer_addr(client_fd), sizeof(client_ip_string)-1); }
static BOOL open_sockets_smbd(BOOL is_daemon, BOOL interactive, const char *smb_ports) { int num_interfaces = iface_count(); int num_sockets = 0; int fd_listenset[FD_SETSIZE]; fd_set listen_set; int s; int maxfd = 0; int i; char *ports; if (!is_daemon) { return open_sockets_inetd(); } #ifdef HAVE_ATEXIT { static int atexit_set; if(atexit_set == 0) { atexit_set=1; atexit(killkids); } } #endif #ifndef _XBOX /* Stop zombies */ CatchChild(); #endif FD_ZERO(&listen_set); /* use a reasonable default set of ports - listing on 445 and 139 */ if (!smb_ports) { ports = lp_smb_ports(); if (!ports || !*ports) { ports = smb_xstrdup(SMB_PORTS); } else { ports = smb_xstrdup(ports); } } else { ports = smb_xstrdup(smb_ports); } if (lp_interfaces() && lp_bind_interfaces_only()) { /* We have been given an interfaces line, and been told to only bind to those interfaces. Create a socket per interface and bind to only these. */ /* Now open a listen socket for each of the interfaces. */ for(i = 0; i < num_interfaces; i++) { struct in_addr *ifip = iface_n_ip(i); fstring tok; const char *ptr; if(ifip == NULL) { DEBUG(0,("open_sockets_smbd: interface %d has NULL IP address !\n", i)); continue; } for (ptr=ports; next_token(&ptr, tok, " \t,", sizeof(tok)); ) { unsigned port = atoi(tok); if (port == 0) { continue; } s = fd_listenset[num_sockets] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True); if(s == -1) return False; /* ready to listen */ set_socket_options(s,"SO_KEEPALIVE"); set_socket_options(s,user_socket_options); /* Set server socket to non-blocking for the accept. */ set_blocking(s,False); if (listen(s, SMBD_LISTEN_BACKLOG) == -1) { DEBUG(0,("listen: %s\n",strerror(errno))); close(s); return False; } FD_SET(s,&listen_set); maxfd = MAX( maxfd, s); num_sockets++; if (num_sockets >= FD_SETSIZE) { DEBUG(0,("open_sockets_smbd: Too many sockets to bind to\n")); return False; } } } } else { /* Just bind to 0.0.0.0 - accept connections from anywhere. */ fstring tok; const char *ptr; num_interfaces = 1; for (ptr=ports; next_token(&ptr, tok, " \t,", sizeof(tok)); ) { unsigned port = atoi(tok); if (port == 0) continue; /* open an incoming socket */ s = open_socket_in(SOCK_STREAM, port, 0, interpret_addr(lp_socket_address()),True); if (s == -1) return(False); /* ready to listen */ #ifndef _XBOX set_socket_options(s,"SO_KEEPALIVE"); #endif set_socket_options(s,user_socket_options); /* Set server socket to non-blocking for the accept. */ set_blocking(s,False); if (listen(s, SMBD_LISTEN_BACKLOG) == -1) { DEBUG(0,("open_sockets_smbd: listen: %s\n", strerror(errno))); close(s); return False; } fd_listenset[num_sockets] = s; FD_SET(s,&listen_set); maxfd = MAX( maxfd, s); num_sockets++; if (num_sockets >= FD_SETSIZE) { DEBUG(0,("open_sockets_smbd: Too many sockets to bind to\n")); return False; } } } SAFE_FREE(ports); /* Listen to messages */ message_register(MSG_SMB_SAM_SYNC, msg_sam_sync); message_register(MSG_SMB_SAM_REPL, msg_sam_repl); message_register(MSG_SHUTDOWN, msg_exit_server); message_register(MSG_SMB_FILE_RENAME, msg_file_was_renamed); message_register(MSG_SMB_CONF_UPDATED, smb_conf_updated); #ifdef DEVELOPER message_register(MSG_SMB_INJECT_FAULT, msg_inject_fault); #endif /* now accept incoming connections - forking a new process for each incoming connection */ DEBUG(2,("waiting for a connection\n")); while (1) { fd_set lfds; int num; /* Free up temporary memory from the main smbd. */ lp_TALLOC_FREE(); /* Ensure we respond to PING and DEBUG messages from the main smbd. */ message_dispatch(); memcpy((char *)&lfds, (char *)&listen_set, sizeof(listen_set)); num = sys_select(maxfd+1,&lfds,NULL,NULL,NULL); if (num == -1 && errno == EINTR) { if (got_sig_term) { exit_server_cleanly(NULL); } /* check for sighup processing */ if (reload_after_sighup) { change_to_root_user(); DEBUG(1,("Reloading services after SIGHUP\n")); reload_services(False); reload_after_sighup = 0; } continue; } /* check if we need to reload services */ check_reload(time(NULL)); /* Find the sockets that are read-ready - accept on these. */ for( ; num > 0; num--) { struct sockaddr addr; socklen_t in_addrlen = sizeof(addr); s = -1; for(i = 0; i < num_sockets; i++) { if(FD_ISSET(fd_listenset[i],&lfds)) { s = fd_listenset[i]; /* Clear this so we don't look at it again. */ FD_CLR(fd_listenset[i],&lfds); break; } } smbd_set_server_fd(accept(s,&addr,&in_addrlen)); if (smbd_server_fd() == -1 && errno == EINTR) continue; if (smbd_server_fd() == -1) { DEBUG(0,("open_sockets_smbd: accept: %s\n", strerror(errno))); continue; } /* Ensure child is set to blocking mode */ set_blocking(smbd_server_fd(),True); if (smbd_server_fd() != -1 && interactive) { #ifdef _XBOX xb_IncClientCount(); #endif return True; } if (allowable_number_of_smbd_processes() && smbd_server_fd() != -1 && sys_fork()==0) { /* Child code ... */ /* close the listening socket(s) */ for(i = 0; i < num_sockets; i++) close(fd_listenset[i]); /* close our standard file descriptors */ close_low_fds(False); am_parent = 0; set_socket_options(smbd_server_fd(),"SO_KEEPALIVE"); set_socket_options(smbd_server_fd(),user_socket_options); /* this is needed so that we get decent entries in smbstatus for port 445 connects */ set_remote_machine_name(get_peer_addr(smbd_server_fd()), False); /* Reset the state of the random * number generation system, so * children do not get the same random * numbers as each other */ set_need_random_reseed(); /* tdb needs special fork handling - remove CLEAR_IF_FIRST flags */ if (tdb_reopen_all(1) == -1) { DEBUG(0,("tdb_reopen_all failed.\n")); smb_panic("tdb_reopen_all failed."); } return True; } /* The parent doesn't need this socket */ close(smbd_server_fd()); /* Sun May 6 18:56:14 2001 [email protected]: Clear the closed fd info out of server_fd -- and more importantly, out of client_fd in util_sock.c, to avoid a possible getpeername failure if we reopen the logs and use %I in the filename. */ smbd_set_server_fd(-1); /* Force parent to check log size after * spawning child. Fix from * [email protected]. The * parent smbd will log to logserver.smb. It * writes only two messages for each child * started/finished. But each child writes, * say, 50 messages also in logserver.smb, * begining with the debug_count of the * parent, before the child opens its own log * file logserver.client. In a worst case * scenario the size of logserver.smb would be * checked after about 50*50=2500 messages * (ca. 100kb). * */ force_check_log_size(); } /* end for num */ } /* end while 1 */ /* NOTREACHED return True; */ }
/* * Function main (argc, ) * * Starts all the fun! * */ int main(int argc, char *argv[]) { struct sockaddr_in peer; obex_object_t *object; int ret; printf("Send and receive files over TCP OBEX\n"); if ( ((argc < 3) || (argc > 3)) && (argc != 1) ) { printf ("Usage: %s [name] [peer]\n", argv[0]); return -1; } handle = OBEX_Init(OBEX_TRANS_INET, obex_event, 0); if (argc == 1) { printf("Waiting for files\n"); ret = InOBEX_ServerRegister(handle); if(ret < 0) { printf("Cannot listen to socket\n"); exit(ret); } while (!finished) { ret = OBEX_HandleInput(handle, 10); if (ret == 0) { printf("Timeout waiting for connection\n"); break; } else if (ret < 0) { printf("Error waiting for connection\n"); break; } } } else { /* We are a client */ get_peer_addr(argv[2], &peer); ret = OBEX_TransportConnect(handle, (struct sockaddr *) &peer, sizeof(struct sockaddr_in)); if (ret < 0) { printf("Sorry, unable to connect!\n"); exit(ret); } object = OBEX_ObjectNew(handle, OBEX_CMD_CONNECT); ret = do_sync_request(handle, object, 0); if( (object = build_object_from_file(handle, argv[1], 0)) ) { ret = do_sync_request(handle, object, 0); } else { perror("PUT failed"); } object = OBEX_ObjectNew(handle, OBEX_CMD_DISCONNECT); ret = do_sync_request(handle, object, 0); printf("PUT successful\n"); } return 0; }