acl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Access Control of various sorts
  20. */
  21. #ifndef _ASTERISK_ACL_H
  22. #define _ASTERISK_ACL_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #include "asterisk/network.h"
  27. #include "asterisk/linkedlists.h"
  28. #include "asterisk/netsock2.h"
  29. #include "asterisk/io.h"
  30. enum ast_acl_sense {
  31. AST_SENSE_DENY,
  32. AST_SENSE_ALLOW
  33. };
  34. /* Host based access control */
  35. /*! \brief internal representation of ACL entries
  36. * In principle user applications would have no need for this,
  37. * but there is sometimes a need to extract individual items,
  38. * e.g. to print them, and rather than defining iterators to
  39. * navigate the list, and an externally visible 'struct ast_ha_entry',
  40. * at least in the short term it is more convenient to make the whole
  41. * thing public and let users play with them.
  42. */
  43. struct ast_ha {
  44. /* Host access rule */
  45. struct ast_sockaddr addr;
  46. struct ast_sockaddr netmask;
  47. enum ast_acl_sense sense;
  48. struct ast_ha *next;
  49. };
  50. #define ACL_NAME_LENGTH 80
  51. /*!
  52. * \brief an ast_acl is a linked list node of ast_ha structs which may have names.
  53. *
  54. * \note These shouldn't be used directly by ACL consumers. Consumers should handle
  55. * ACLs via ast_acl_list structs.
  56. */
  57. struct ast_acl {
  58. struct ast_ha *acl; /*!< Rules contained by the ACL */
  59. int is_realtime; /*!< If raised, this named ACL was retrieved from realtime storage */
  60. int is_invalid; /*!< If raised, this is an invalid ACL which will automatically reject everything. */
  61. char name[ACL_NAME_LENGTH]; /*!< If this was retrieved from the named ACL subsystem, this is the name of the ACL. */
  62. AST_LIST_ENTRY(ast_acl) list;
  63. };
  64. /*! \brief Wrapper for an ast_acl linked list. */
  65. AST_LIST_HEAD(ast_acl_list, ast_acl);
  66. /*!
  67. * \brief Free a list of HAs
  68. *
  69. * \details
  70. * Given the head of a list of HAs, it and all appended
  71. * HAs are freed
  72. *
  73. * \param ha The head of the list of HAs to free
  74. * \retval void
  75. */
  76. void ast_free_ha(struct ast_ha *ha);
  77. /*!
  78. * \brief Free a list of ACLs
  79. *
  80. * \details
  81. * Given the head of a list of ast_acl structs, it and all appended
  82. * acl structs will be freed. This includes the ast_ha structs within
  83. * the individual nodes.
  84. * \param acl The list of ACLs to free
  85. * \retval NULL
  86. */
  87. struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl);
  88. /*!
  89. * \brief Copy the contents of one HA to another
  90. *
  91. * \details
  92. * This copies the internals of the 'from' HA to the 'to'
  93. * HA. It is important that the 'to' HA has been allocated
  94. * prior to calling this function
  95. *
  96. * \param from Source HA to copy
  97. * \param to Destination HA to copy to
  98. * \retval void
  99. */
  100. void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to);
  101. /*!
  102. * \brief Add a new rule to a list of HAs
  103. *
  104. * \details
  105. * This adds the new host access rule to the end of the list
  106. * whose head is specified by the path parameter. Rules are
  107. * evaluated in a way such that if multiple rules apply to
  108. * a single IP address/subnet mask, then the rule latest
  109. * in the list will be used.
  110. *
  111. * \param sense Either "permit" or "deny" (Actually any 'p' word will result
  112. * in permission, and any other word will result in denial)
  113. * \param stuff The IP address and subnet mask, separated with a '/'. The subnet
  114. * mask can either be in dotted-decimal format or in CIDR notation (i.e. 0-32).
  115. * \param path The head of the HA list to which we wish to append our new rule. If
  116. * NULL is passed, then the new rule will become the head of the list
  117. * \param[out] error The integer error points to will be set non-zero if an error occurs
  118. * \return The head of the HA list
  119. */
  120. struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error);
  121. /*!
  122. * \brief Convert HAs to a comma separated string value
  123. * \param ha the starting ha head
  124. * \param buf string buffer to convert data to
  125. */
  126. void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf);
  127. /*!
  128. * \brief Convert HAs to a comma separated string value using CIDR notation
  129. * \param ha the starting ha head
  130. * \param buf string buffer to convert data to
  131. */
  132. void ast_ha_join_cidr(const struct ast_ha *ha, struct ast_str **buf);
  133. /*!
  134. * \brief Add a rule to an ACL struct
  135. *
  136. * \details
  137. * This adds a named ACL or an ACL rule to an ast_acl container.
  138. * It works in a similar way to ast_append_ha.
  139. *
  140. * \param sense Can be any among "permit", "deny", or "acl"
  141. * this controls whether the rule being added will simply modify the unnamed ACL at the head of the list
  142. * or if a new named ACL will be added to that ast_acl.
  143. * \param stuff If sense is 'permit'/'deny', this is the ip address and subnet mask separated with a '/' like in ast_append ha.
  144. * If it sense is 'acl', then this will be the name of the ACL being appended to the container.
  145. * \param path Address of the ACL list being appended
  146. * \param[out] error The int that error points to will be set to 1 if an error occurs.
  147. * \param[out] named_acl_flag This will raise a flag under certain conditions to indicate that a named ACL has been added by this
  148. * operation. This may be used to indicate that an event subscription should be made against the named ACL subsystem.
  149. * Note: This flag may be raised by this function, but it will never be lowered by it.
  150. */
  151. void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag);
  152. /*!
  153. * \brief Determines if an ACL is empty or if it contains entries
  154. *
  155. * \param acl_list The ACL list being checked
  156. * \retval 0 - the list is not empty
  157. * \retval 1 - the list is empty
  158. */
  159. int ast_acl_list_is_empty(struct ast_acl_list *acl_list);
  160. /*!
  161. * \brief Apply a set of rules to a given IP address
  162. *
  163. * \details
  164. * The list of host access rules is traversed, beginning with the
  165. * input rule. If the IP address given matches a rule, the "sense"
  166. * of that rule is used as the return value. Note that if an IP
  167. * address matches multiple rules that the last one matched will be
  168. * the one whose sense will be returned.
  169. *
  170. * \param ha The head of the list of host access rules to follow
  171. * \param addr An ast_sockaddr whose address is considered when matching rules
  172. * \retval AST_SENSE_ALLOW The IP address passes our ACL
  173. * \retval AST_SENSE_DENY The IP address fails our ACL
  174. */
  175. enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr);
  176. /*!
  177. * \brief Apply a set of rules to a given IP address
  178. *
  179. * \details
  180. * Similar to the above, only uses an acl container, which is a whole slew
  181. * of ast_ha lists. It runs ast_apply_ha on each of the ast_ha structs
  182. * contained in the acl container. It will deny if any of the ast_ha lists
  183. * fail, and it will pass only if all of the rules pass.
  184. *
  185. * \param acl_list The head of the list of ACLs to evaluate
  186. * \param addr An ast_sockaddr whose address is considered when matching rules
  187. * \param purpose Context for which the ACL is being applied - Establishes purpose of a notice when rejected
  188. *
  189. * \retval AST_SENSE_ALLOW The IP address passes our ACLs
  190. * \retval AST_SENSE_DENY The IP address fails our ACLs
  191. */
  192. enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose);
  193. /*!
  194. * \brief Get the IP address given a hostname
  195. *
  196. * \details
  197. * Similar in nature to ast_gethostbyname, except that instead
  198. * of getting an entire hostent structure, you instead are given
  199. * only the IP address inserted into a ast_sockaddr structure.
  200. *
  201. * \param addr The IP address found. The address family is used
  202. * as an input parameter to filter the returned addresses. If
  203. * it is AST_AF_UNSPEC, both IPv4 and IPv6 addresses can be returned.
  204. * \param hostname The hostname to look up
  205. *
  206. * \retval 0 Success
  207. * \retval -1 Failure
  208. */
  209. int ast_get_ip(struct ast_sockaddr *addr, const char *hostname);
  210. /*!
  211. * \brief Get the IP address given a hostname and optional service
  212. *
  213. * \details
  214. * If the service parameter is non-NULL, then an SRV lookup will be made by
  215. * prepending the service to the hostname parameter, separated by a '.'
  216. * For example, if hostname is "example.com" and service is "_sip._udp" then
  217. * an SRV lookup will be done for "_sip._udp.example.com". If service is NULL,
  218. * then this function acts exactly like a call to ast_get_ip.
  219. *
  220. * \param addr The IP address found. The address family is used
  221. * as an input parameter to filter the returned addresses. If
  222. * it is 0, both IPv4 and IPv6 addresses can be returned.
  223. *
  224. * \param hostname The hostname to look up
  225. * \param service A specific service provided by the host. A NULL service results
  226. * in an A-record lookup instead of an SRV lookup
  227. * \retval 0 Success
  228. * \retval -1 Failure
  229. */
  230. int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service);
  231. /*!
  232. * \brief Get our local IP address when contacting a remote host
  233. *
  234. * \details
  235. * This function will attempt to connect(2) to them over UDP using a source
  236. * port of 5060. If the connect(2) call is successful, then we inspect the
  237. * sockaddr_in output parameter of connect(2) to determine the IP address
  238. * used to connect to them. This IP address is then copied into us.
  239. *
  240. * \param them The IP address to which we wish to attempt to connect
  241. * \param[out] us The source IP address used to connect to them
  242. * \retval -1 Failure
  243. * \retval 0 Success
  244. */
  245. int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us);
  246. /*!
  247. * \brief Find an IP address associated with a specific interface
  248. *
  249. * \details
  250. * Given an interface such as "eth0" we find the primary IP address
  251. * associated with it using the SIOCGIFADDR ioctl. If the ioctl call
  252. * should fail, we populate address with 0s.
  253. *
  254. * \note
  255. * This function is not actually used anywhere
  256. *
  257. * \param iface The interface name whose IP address we wish to find
  258. * \param[out] address The interface's IP address is placed into this param
  259. * \retval -1 Failure. address is filled with 0s
  260. * \retval 0 Success
  261. */
  262. int ast_lookup_iface(char *iface, struct ast_sockaddr *address);
  263. /*!
  264. * \brief Duplicate the contents of a list of host access rules
  265. *
  266. * \details
  267. * A deep copy of all ast_has in the list is made. The returned
  268. * value is allocated on the heap and must be freed independently
  269. * of the input parameter when finished.
  270. *
  271. * \param original The ast_ha to copy
  272. * \retval The head of the list of duplicated ast_has
  273. */
  274. struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original);
  275. /*!
  276. * \brief Duplicates the contests of a list of lists of host access rules.
  277. *
  278. * \details
  279. * A deep copy of an ast_acl list is made (which in turn means a deep copy of
  280. * each of the ast_ha structs contained within). The returned value is allocated
  281. * on the heap and must be freed independently of the input paramater when
  282. * finished.
  283. *
  284. * \param original The ast_acl_list to copy
  285. * \retval The new duplicated ast_acl_list
  286. */
  287. struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original);
  288. /*!
  289. * \brief Find our IP address
  290. *
  291. * \details
  292. * This function goes through many iterations in an attempt to find
  293. * our IP address. If any step along the way should fail, we move to the
  294. * next item in the list. Here are the steps taken:
  295. * - If bindaddr has a non-zero IP address, that is copied into ourip
  296. * - We use a combination of gethostname and ast_gethostbyname to find our
  297. * IP address.
  298. * - We use ast_ouraddrfor with 198.41.0.4 as the destination IP address
  299. * - We try some platform-specific socket operations to find the IP address
  300. *
  301. * \param[out] ourip Our IP address is written here when it is found
  302. * \param bindaddr A hint used for finding our IP. See the steps above for
  303. * more details
  304. * \param family Only addresses of the given family will be returned. Use 0
  305. * or AST_SOCKADDR_UNSPEC to get addresses of all families.
  306. * \retval 0 Success
  307. * \retval -1 Failure
  308. */
  309. int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family);
  310. /*!
  311. * \brief Convert a string to the appropriate COS value
  312. *
  313. * \param value The COS string to convert
  314. * \param[out] cos The integer representation of that COS value
  315. * \retval -1 Failure
  316. * \retval 0 Success
  317. */
  318. int ast_str2cos(const char *value, unsigned int *cos);
  319. /*!
  320. * \brief Convert a string to the appropriate TOS value
  321. *
  322. * \param value The TOS string to convert
  323. * \param[out] tos The integer representation of that TOS value
  324. * \retval -1 Failure
  325. * \retval 0 Success
  326. */
  327. int ast_str2tos(const char *value, unsigned int *tos);
  328. /*!
  329. * \brief Convert a TOS value into its string representation
  330. *
  331. * \param tos The TOS value to look up
  332. * \return The string equivalent of the TOS value
  333. */
  334. const char *ast_tos2str(unsigned int tos);
  335. /*!
  336. * \brief Retrieve a named ACL
  337. *
  338. * \details
  339. * This function attempts to find a named ACL. If found, a copy
  340. * of the requested ACL will be made which must be freed by
  341. * the caller.
  342. *
  343. * \param name Name of the ACL sought
  344. * \param[out] is_realtime will be true if the ACL being returned is from realtime
  345. * \param[out] is_undefined will be true if no ACL profile can be found for the requested name
  346. *
  347. * \retval A copy of the named ACL as an ast_ha
  348. * \retval NULL if no ACL could be found.
  349. */
  350. struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_undefined);
  351. /*!
  352. * \brief Initialize and configure the named ACL system.
  353. *
  354. * \details
  355. * This function will prepare the named ACL system for use.
  356. * For this reason, it needs to be called before other things that use ACLs are initialized.
  357. */
  358. int ast_named_acl_init(void);
  359. /*!
  360. * \brief reload/reconfigure the named ACL system.
  361. *
  362. * \details
  363. * This function is designed to trigger an event upon a successful reload that may update
  364. * ACL consumers.
  365. */
  366. int ast_named_acl_reload(void);
  367. /*!
  368. * \brief a \ref stasis_message_type for changes against a named ACL or the set of all named ACLs
  369. * \since 12
  370. *
  371. * \retval NULL on error
  372. * \retval \ref stasis_message_type for named ACL changes
  373. *
  374. * \note Messages of this type should always be issued on and expected from the
  375. * \ref ast_security_topic \ref stasis_topic
  376. */
  377. struct stasis_message_type *ast_named_acl_change_type(void);
  378. #if defined(__cplusplus) || defined(c_plusplus)
  379. }
  380. #endif
  381. #endif /* _ASTERISK_ACL_H */