CVE-2023-0614 ldb: Use binary search to check whether attribute is secret
[Samba.git] / source4 / lib / socket / access.c
blobc019fd64d26ba1c1c5b1fbde5a576e8f7e86b8eb
1 /*
2 Unix SMB/CIFS implementation.
4 check access rules for socket connections
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /*
24 This module is an adaption of code from the tcpd-1.4 package written
25 by Wietse Venema, Eindhoven University of Technology, The Netherlands.
27 The code is used here with permission.
29 The code has been considerably changed from the original. Bug reports
30 should be sent to samba-technical@lists.samba.org
33 #include "includes.h"
34 #include "system/network.h"
35 #include "lib/socket/socket.h"
36 #include "lib/util/util_net.h"
37 #include "lib/util/access.h"
39 /* return true if the char* contains ip addrs only. Used to avoid
40 gethostbyaddr() calls */
42 static bool only_ipaddrs_in_list(const char** list)
44 bool only_ip = true;
46 if (!list)
47 return true;
49 for (; *list ; list++) {
50 /* factor out the special strings */
51 if (strcmp(*list, "ALL")==0 ||
52 strcmp(*list, "FAIL")==0 ||
53 strcmp(*list, "EXCEPT")==0) {
54 continue;
57 if (!is_ipaddress(*list)) {
58 /*
59 * if we failed, make sure that it was not because the token
60 * was a network/netmask pair. Only network/netmask pairs
61 * have a '/' in them
63 if ((strchr(*list, '/')) == NULL) {
64 only_ip = false;
65 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list));
66 break;
71 return only_ip;
74 /* return true if access should be allowed to a service for a socket */
75 bool socket_check_access(struct socket_context *sock,
76 const char *service_name,
77 const char **allow_list, const char **deny_list)
79 bool ret;
80 const char *name="";
81 struct socket_address *addr;
82 TALLOC_CTX *mem_ctx;
84 if ((!deny_list || *deny_list==0) &&
85 (!allow_list || *allow_list==0)) {
86 return true;
89 mem_ctx = talloc_init("socket_check_access");
90 if (!mem_ctx) {
91 return false;
94 addr = socket_get_peer_addr(sock, mem_ctx);
95 if (!addr) {
96 DEBUG(0,("socket_check_access: Denied connection from unknown host: could not get peer address from kernel\n"));
97 talloc_free(mem_ctx);
98 return false;
101 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
102 if (!only_ipaddrs_in_list(allow_list) ||
103 !only_ipaddrs_in_list(deny_list)) {
104 name = socket_get_peer_name(sock, mem_ctx);
105 if (!name) {
106 name = addr->addr;
110 if (!addr) {
111 DEBUG(0,("socket_check_access: Denied connection from unknown host\n"));
112 talloc_free(mem_ctx);
113 return false;
116 ret = allow_access(deny_list, allow_list, name, addr->addr);
118 if (ret) {
119 DEBUG(2,("socket_check_access: Allowed connection to '%s' from %s (%s)\n",
120 service_name, name, addr->addr));
121 } else {
122 DEBUG(0,("socket_check_access: Denied connection to '%s' from %s (%s)\n",
123 service_name, name, addr->addr));
126 talloc_free(mem_ctx);
128 return ret;