PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/lib/access.c

https://gitlab.com/math4youbyusgroupillinois/samba
C | 343 lines | 227 code | 48 blank | 68 comment | 96 complexity | d428134ae1b07ba2d6cb39f9d5208990 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-3.0
  1. /*
  2. This module is an adaption of code from the tcpd-1.4 package written
  3. by Wietse Venema, Eindhoven University of Technology, The Netherlands.
  4. The code is used here with permission.
  5. The code has been considerably changed from the original. Bug reports
  6. should be sent to samba@samba.org
  7. Updated for IPv6 by Jeremy Allison (C) 2007.
  8. */
  9. #include "includes.h"
  10. #include "../lib/util/memcache.h"
  11. #include "lib/socket/interfaces.h"
  12. #define NAME_INDEX 0
  13. #define ADDR_INDEX 1
  14. /* masked_match - match address against netnumber/netmask */
  15. static bool masked_match(const char *tok, const char *slash, const char *s)
  16. {
  17. struct sockaddr_storage ss_mask;
  18. struct sockaddr_storage ss_tok;
  19. struct sockaddr_storage ss_host;
  20. char *tok_copy = NULL;
  21. if (!interpret_string_addr(&ss_host, s, 0)) {
  22. return false;
  23. }
  24. if (*tok == '[') {
  25. /* IPv6 address - remove braces. */
  26. tok_copy = SMB_STRDUP(tok+1);
  27. if (!tok_copy) {
  28. return false;
  29. }
  30. /* Remove the terminating ']' */
  31. tok_copy[PTR_DIFF(slash,tok)-1] = '\0';
  32. } else {
  33. tok_copy = SMB_STRDUP(tok);
  34. if (!tok_copy) {
  35. return false;
  36. }
  37. /* Remove the terminating '/' */
  38. tok_copy[PTR_DIFF(slash,tok)] = '\0';
  39. }
  40. if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
  41. SAFE_FREE(tok_copy);
  42. return false;
  43. }
  44. SAFE_FREE(tok_copy);
  45. if (strlen(slash + 1) > 2) {
  46. if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
  47. return false;
  48. }
  49. } else {
  50. char *endp = NULL;
  51. unsigned long val = strtoul(slash+1, &endp, 0);
  52. if (slash+1 == endp || (endp && *endp != '\0')) {
  53. return false;
  54. }
  55. if (!make_netmask(&ss_mask, &ss_tok, val)) {
  56. return false;
  57. }
  58. }
  59. return same_net((struct sockaddr *)(void *)&ss_host,
  60. (struct sockaddr *)(void *)&ss_tok,
  61. (struct sockaddr *)(void *)&ss_mask);
  62. }
  63. /* string_match - match string s against token tok */
  64. static bool string_match(const char *tok,const char *s)
  65. {
  66. size_t tok_len;
  67. size_t str_len;
  68. const char *cut;
  69. /* Return true if a token has the magic value "ALL". Return
  70. * true if the token is "FAIL". If the token starts with a "."
  71. * (domain name), return true if it matches the last fields of
  72. * the string. If the token has the magic value "LOCAL",
  73. * return true if the string does not contain a "."
  74. * character. If the token ends on a "." (network number),
  75. * return true if it matches the first fields of the
  76. * string. If the token begins with a "@" (netgroup name),
  77. * return true if the string is a (host) member of the
  78. * netgroup. Return true if the token fully matches the
  79. * string. If the token is a netnumber/netmask pair, return
  80. * true if the address is a member of the specified subnet.
  81. */
  82. if (tok[0] == '.') { /* domain: match last fields */
  83. if ((str_len = strlen(s)) > (tok_len = strlen(tok))
  84. && strequal(tok, s + str_len - tok_len)) {
  85. return true;
  86. }
  87. } else if (tok[0] == '@') { /* netgroup: look it up */
  88. #ifdef HAVE_NETGROUP
  89. DATA_BLOB tmp;
  90. char *mydomain = NULL;
  91. char *hostname = NULL;
  92. bool netgroup_ok = false;
  93. if (memcache_lookup(
  94. NULL, SINGLETON_CACHE,
  95. data_blob_string_const_null("yp_default_domain"),
  96. &tmp)) {
  97. SMB_ASSERT(tmp.length > 0);
  98. mydomain = (tmp.data[0] == '\0')
  99. ? NULL : (char *)tmp.data;
  100. }
  101. else {
  102. yp_get_default_domain(&mydomain);
  103. memcache_add(
  104. NULL, SINGLETON_CACHE,
  105. data_blob_string_const_null("yp_default_domain"),
  106. data_blob_string_const_null(mydomain?mydomain:""));
  107. }
  108. if (!mydomain) {
  109. DEBUG(0,("Unable to get default yp domain. "
  110. "Try without it.\n"));
  111. }
  112. if (!(hostname = SMB_STRDUP(s))) {
  113. DEBUG(1,("out of memory for strdup!\n"));
  114. return false;
  115. }
  116. netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
  117. DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
  118. hostname,
  119. mydomain?mydomain:"(ANY)",
  120. tok+1,
  121. BOOLSTR(netgroup_ok)));
  122. SAFE_FREE(hostname);
  123. if (netgroup_ok)
  124. return true;
  125. #else
  126. DEBUG(0,("access: netgroup support is not configured\n"));
  127. return false;
  128. #endif
  129. } else if (strequal(tok, "ALL")) { /* all: match any */
  130. return true;
  131. } else if (strequal(tok, "FAIL")) { /* fail: match any */
  132. return true;
  133. } else if (strequal(tok, "LOCAL")) { /* local: no dots */
  134. if (strchr_m(s, '.') == 0 && !strequal(s, "unknown")) {
  135. return true;
  136. }
  137. } else if (strequal(tok, s)) { /* match host name or address */
  138. return true;
  139. } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
  140. if (strncmp(tok, s, tok_len) == 0) {
  141. return true;
  142. }
  143. } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
  144. if ((isdigit(s[0]) && strchr_m(tok, '.') != NULL) ||
  145. (tok[0] == '[' && cut > tok && cut[-1] == ']') ||
  146. ((isxdigit(s[0]) || s[0] == ':') &&
  147. strchr_m(tok, ':') != NULL)) {
  148. /* IPv4/netmask or
  149. * [IPv6:addr]/netmask or IPv6:addr/netmask */
  150. return masked_match(tok, cut, s);
  151. }
  152. } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
  153. return unix_wild_match(tok, s);
  154. }
  155. return false;
  156. }
  157. /* client_match - match host name and address against token */
  158. bool client_match(const char *tok, const void *item)
  159. {
  160. const char **client = discard_const_p(const char *, item);
  161. const char *tok_addr = tok;
  162. const char *cli_addr = client[ADDR_INDEX];
  163. /*
  164. * tok and client[ADDR_INDEX] can be an IPv4 mapped to IPv6,
  165. * we try and match the IPv4 part of address only.
  166. * Bug #5311 and #7383.
  167. */
  168. if (strnequal(tok_addr, "::ffff:",7)) {
  169. tok_addr += 7;
  170. }
  171. if (strnequal(cli_addr,"::ffff:",7)) {
  172. cli_addr += 7;
  173. }
  174. /*
  175. * Try to match the address first. If that fails, try to match the host
  176. * name if available.
  177. */
  178. if (string_match(tok_addr, cli_addr)) {
  179. return true;
  180. }
  181. if (client[NAME_INDEX][0] != 0) {
  182. if (string_match(tok, client[NAME_INDEX])) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. /* list_match - match an item against a list of tokens with exceptions */
  189. bool list_match(const char **list,const void *item,
  190. bool (*match_fn)(const char *, const void *))
  191. {
  192. bool match = false;
  193. if (!list) {
  194. return false;
  195. }
  196. /*
  197. * Process tokens one at a time. We have exhausted all possible matches
  198. * when we reach an "EXCEPT" token or the end of the list. If we do find
  199. * a match, look for an "EXCEPT" list and recurse to determine whether
  200. * the match is affected by any exceptions.
  201. */
  202. for (; *list ; list++) {
  203. if (strequal(*list, "EXCEPT")) {
  204. /* EXCEPT: give up */
  205. break;
  206. }
  207. if ((match = (*match_fn) (*list, item))) {
  208. /* true or FAIL */
  209. break;
  210. }
  211. }
  212. /* Process exceptions to true or FAIL matches. */
  213. if (match != false) {
  214. while (*list && !strequal(*list, "EXCEPT")) {
  215. list++;
  216. }
  217. for (; *list; list++) {
  218. if ((*match_fn) (*list, item)) {
  219. /* Exception Found */
  220. return false;
  221. }
  222. }
  223. }
  224. return match;
  225. }
  226. /* return true if access should be allowed */
  227. static bool allow_access_internal(const char **deny_list,
  228. const char **allow_list,
  229. const char *cname,
  230. const char *caddr)
  231. {
  232. const char *client[2];
  233. client[NAME_INDEX] = cname;
  234. client[ADDR_INDEX] = caddr;
  235. /* if it is loopback then always allow unless specifically denied */
  236. if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
  237. /*
  238. * If 127.0.0.1 matches both allow and deny then allow.
  239. * Patch from Steve Langasek vorlon@netexpress.net.
  240. */
  241. if (deny_list &&
  242. list_match(deny_list,client,client_match) &&
  243. (!allow_list ||
  244. !list_match(allow_list,client, client_match))) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. /* if theres no deny list and no allow list then allow access */
  250. if ((!deny_list || *deny_list == 0) &&
  251. (!allow_list || *allow_list == 0)) {
  252. return true;
  253. }
  254. /* if there is an allow list but no deny list then allow only hosts
  255. on the allow list */
  256. if (!deny_list || *deny_list == 0) {
  257. return(list_match(allow_list,client,client_match));
  258. }
  259. /* if theres a deny list but no allow list then allow
  260. all hosts not on the deny list */
  261. if (!allow_list || *allow_list == 0) {
  262. return(!list_match(deny_list,client,client_match));
  263. }
  264. /* if there are both types of list then allow all hosts on the
  265. allow list */
  266. if (list_match(allow_list,(const char *)client,client_match)) {
  267. return true;
  268. }
  269. /* if there are both types of list and it's not on the allow then
  270. allow it if its not on the deny */
  271. if (list_match(deny_list,(const char *)client,client_match)) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. /* return true if access should be allowed */
  277. bool allow_access(const char **deny_list,
  278. const char **allow_list,
  279. const char *cname,
  280. const char *caddr)
  281. {
  282. bool ret;
  283. char *nc_cname = smb_xstrdup(cname);
  284. char *nc_caddr = smb_xstrdup(caddr);
  285. ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
  286. DEBUG(ret ? 3 : 0,
  287. ("%s connection from %s (%s)\n",
  288. ret ? "Allowed" : "Denied", nc_cname, nc_caddr));
  289. SAFE_FREE(nc_cname);
  290. SAFE_FREE(nc_caddr);
  291. return ret;
  292. }