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/>.
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
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
)
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) {
57 if (!is_ipaddress(*list
)) {
59 * if we failed, make sure that it was not because the token
60 * was a network/netmask pair. Only network/netmask pairs
63 if ((strchr(*list
, '/')) == NULL
) {
65 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list
));
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
)
81 struct socket_address
*addr
;
84 if ((!deny_list
|| *deny_list
==0) &&
85 (!allow_list
|| *allow_list
==0)) {
89 mem_ctx
= talloc_init("socket_check_access");
94 addr
= socket_get_peer_addr(sock
, mem_ctx
);
96 DEBUG(0,("socket_check_access: Denied connection from unknown host: could not get peer address from kernel\n"));
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
);
111 DEBUG(0,("socket_check_access: Denied connection from unknown host\n"));
112 talloc_free(mem_ctx
);
116 ret
= allow_access(deny_list
, allow_list
, name
, addr
->addr
);
119 DEBUG(2,("socket_check_access: Allowed connection to '%s' from %s (%s)\n",
120 service_name
, name
, addr
->addr
));
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
);