Sat Apr 26 2014 22:01:25

Asterisk developer's documentation


acl.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2012, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Access Control of various sorts
00021  */
00022 
00023 #ifndef _ASTERISK_ACL_H
00024 #define _ASTERISK_ACL_H
00025 
00026 
00027 #if defined(__cplusplus) || defined(c_plusplus)
00028 extern "C" {
00029 #endif
00030 
00031 #include "asterisk/network.h"
00032 #include "asterisk/linkedlists.h"
00033 #include "asterisk/netsock2.h"
00034 #include "asterisk/io.h"
00035 
00036 enum ast_acl_sense {
00037    AST_SENSE_DENY,
00038    AST_SENSE_ALLOW
00039 };
00040 
00041 /* Host based access control */
00042 
00043 /*! \brief internal representation of ACL entries
00044  * In principle user applications would have no need for this,
00045  * but there is sometimes a need to extract individual items,
00046  * e.g. to print them, and rather than defining iterators to
00047  * navigate the list, and an externally visible 'struct ast_ha_entry',
00048  * at least in the short term it is more convenient to make the whole
00049  * thing public and let users play with them.
00050  */
00051 struct ast_ha {
00052    /* Host access rule */
00053    struct ast_sockaddr addr;
00054    struct ast_sockaddr netmask;
00055    enum ast_acl_sense sense;
00056    struct ast_ha *next;
00057 };
00058 
00059 #define ACL_NAME_LENGTH 80
00060 
00061 /*!
00062  * \brief an ast_acl is a linked list node of ast_ha structs which may have names.
00063  *
00064  * \note These shouldn't be used directly by ACL consumers. Consumers should handle
00065  *       ACLs via ast_acl_list structs.
00066  */
00067 struct ast_acl {
00068    struct ast_ha *acl;             /*!< Rules contained by the ACL */
00069    int is_realtime;                /*!< If raised, this named ACL was retrieved from realtime storage */
00070    int is_invalid;                 /*!< If raised, this is an invalid ACL which will automatically reject everything. */
00071    char name[ACL_NAME_LENGTH];     /*!< If this was retrieved from the named ACL subsystem, this is the name of the ACL. */
00072    AST_LIST_ENTRY(ast_acl) list;
00073 };
00074 
00075 /*! \brief Wrapper for an ast_acl linked list. */
00076 AST_LIST_HEAD(ast_acl_list, ast_acl);
00077 
00078 /*!
00079  * \brief Free a list of HAs
00080  *
00081  * \details
00082  * Given the head of a list of HAs, it and all appended
00083  * HAs are freed
00084  *
00085  * \param ha The head of the list of HAs to free
00086  * \retval void
00087  */
00088 void ast_free_ha(struct ast_ha *ha);
00089 
00090 /*!
00091  * \brief Free a list of ACLs
00092  *
00093  * \details
00094  * Given the head of a list of ast_acl structs, it and all appended
00095  * acl structs will be freed. This includes the ast_ha structs within
00096  * the individual nodes.
00097  * \param acl The list of ACLs to free
00098  * \retval NULL
00099  */
00100 struct ast_acl_list *ast_free_acl_list(struct ast_acl_list *acl);
00101 
00102 /*!
00103  * \brief Copy the contents of one HA to another
00104  *
00105  * \details
00106  * This copies the internals of the 'from' HA to the 'to'
00107  * HA. It is important that the 'to' HA has been allocated
00108  * prior to calling this function
00109  *
00110  * \param from Source HA to copy
00111  * \param to Destination HA to copy to
00112  * \retval void
00113  */
00114 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to);
00115 
00116 /*!
00117  * \brief Add a new rule to a list of HAs
00118  *
00119  * \details
00120  * This adds the new host access rule to the end of the list
00121  * whose head is specified by the path parameter. Rules are
00122  * evaluated in a way such that if multiple rules apply to
00123  * a single IP address/subnet mask, then the rule latest
00124  * in the list will be used.
00125  *
00126  * \param sense Either "permit" or "deny" (Actually any 'p' word will result
00127  * in permission, and any other word will result in denial)
00128  * \param stuff The IP address and subnet mask, separated with a '/'. The subnet
00129  * mask can either be in dotted-decimal format or in CIDR notation (i.e. 0-32).
00130  * \param path The head of the HA list to which we wish to append our new rule. If
00131  * NULL is passed, then the new rule will become the head of the list
00132  * \param[out] error The integer error points to will be set non-zero if an error occurs
00133  * \return The head of the HA list
00134  */
00135 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error);
00136 
00137 /*!
00138  * \brief Add a rule to an ACL struct
00139  *
00140  * \details
00141  * This adds a named ACL or an ACL rule to an ast_acl container.
00142  * It works in a similar way to ast_append_ha.
00143  *
00144  * \param sense Can be any among "permit", "deny", or "acl"
00145  *        this controls whether the rule being added will simply modify the unnamed ACL at the head of the list
00146  *        or if a new named ACL will be added to that ast_acl.
00147  * \param stuff If sense is 'permit'/'deny', this is the ip address and subnet mask separated with a '/' like in ast_append ha.
00148  *        If it sense is 'acl', then this will be the name of the ACL being appended to the container.
00149  * \param path Address of the ACL list being appended
00150  * \param[out] error The int that error points to will be set to 1 if an error occurs.
00151  * \param[out] named_acl_flag This will raise a flag under certain conditions to indicate that a named ACL has been added by this
00152  *        operation. This may be used to indicate that an event subscription should be made against the named ACL subsystem.
00153  *        Note: This flag may be raised by this function, but it will never be lowered by it.
00154  */
00155 void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag);
00156 
00157 /*!
00158  * \brief Determines if an ACL is empty or if it contains entries
00159  *
00160  * \param acl_list The ACL list being checked
00161  * \retval 0 - the list is not empty
00162  * \retval 1 - the list is empty
00163  */
00164 int ast_acl_list_is_empty(struct ast_acl_list *acl_list);
00165 
00166 /*!
00167  * \brief Apply a set of rules to a given IP address
00168  *
00169  * \details
00170  * The list of host access rules is traversed, beginning with the
00171  * input rule. If the IP address given matches a rule, the "sense"
00172  * of that rule is used as the return value. Note that if an IP
00173  * address matches multiple rules that the last one matched will be
00174  * the one whose sense will be returned.
00175  *
00176  * \param ha The head of the list of host access rules to follow
00177  * \param addr An ast_sockaddr whose address is considered when matching rules
00178  * \retval AST_SENSE_ALLOW The IP address passes our ACL
00179  * \retval AST_SENSE_DENY The IP address fails our ACL
00180  */
00181 enum ast_acl_sense ast_apply_ha(const struct ast_ha *ha, const struct ast_sockaddr *addr);
00182 
00183 /*!
00184  * \brief Apply a set of rules to a given IP address
00185  *
00186  * \details
00187  * Similar to the above, only uses an acl container, which is a whole slew
00188  * of ast_ha lists. It runs ast_apply_ha on each of the ast_ha structs
00189  * contained in the acl container. It will deny if any of the ast_ha lists
00190  * fail, and it will pass only if all of the rules pass.
00191  *
00192  * \param acl The head of the list of ACLs to evaluate
00193  * \param addr An ast_sockaddr whose address is considered when matching rules
00194  * \param purpose Context for which the ACL is being applied - Establishes purpose of a notice when rejected
00195  *
00196  * \retval AST_SENSE_ALLOW The IP address passes our ACLs
00197  * \retval AST_SENSE_DENY The IP address fails our ACLs
00198  */
00199 enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose);
00200 
00201 /*!
00202  * \brief Get the IP address given a hostname
00203  *
00204  * \details
00205  * Similar in nature to ast_gethostbyname, except that instead
00206  * of getting an entire hostent structure, you instead are given
00207  * only the IP address inserted into a ast_sockaddr structure.
00208  *
00209  * \param addr The IP address found.  The address family is used
00210  * as an input parameter to filter the returned addresses.  If
00211  * it is 0, both IPv4 and IPv6 addresses can be returned.
00212  * \param hostname The hostname to look up
00213  *
00214  * \retval 0 Success
00215  * \retval -1 Failure
00216  */
00217 int ast_get_ip(struct ast_sockaddr *addr, const char *hostname);
00218 
00219 /*!
00220  * \brief Get the IP address given a hostname and optional service
00221  *
00222  * \details
00223  * If the service parameter is non-NULL, then an SRV lookup will be made by
00224  * prepending the service to the hostname parameter, separated by a '.'
00225  * For example, if hostname is "example.com" and service is "_sip._udp" then
00226  * an SRV lookup will be done for "_sip._udp.example.com". If service is NULL,
00227  * then this function acts exactly like a call to ast_get_ip.
00228  *
00229  * \param addr The IP address found.  The address family is used
00230  * as an input parameter to filter the returned addresses.  If
00231  * it is 0, both IPv4 and IPv6 addresses can be returned.
00232  *
00233  * \param hostname The hostname to look up
00234  * \param service A specific service provided by the host. A NULL service results
00235  * in an A-record lookup instead of an SRV lookup
00236  * \retval 0 Success
00237  * \retval -1 Failure
00238  */
00239 int ast_get_ip_or_srv(struct ast_sockaddr *addr, const char *hostname, const char *service);
00240 
00241 /*!
00242  * \brief Get our local IP address when contacting a remote host
00243  *
00244  * \details
00245  * This function will attempt to connect(2) to them over UDP using a source
00246  * port of 5060. If the connect(2) call is successful, then we inspect the
00247  * sockaddr_in output parameter of connect(2) to determine the IP address
00248  * used to connect to them. This IP address is then copied into us.
00249  *
00250  * \param them The IP address to which we wish to attempt to connect
00251  * \param[out] us The source IP address used to connect to them
00252  * \retval -1 Failure
00253  * \retval 0 Success
00254  */
00255 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us);
00256 
00257 /*!
00258  * \brief Find an IP address associated with a specific interface
00259  *
00260  * \details
00261  * Given an interface such as "eth0" we find the primary IP address
00262  * associated with it using the SIOCGIFADDR ioctl. If the ioctl call
00263  * should fail, we populate address with 0s.
00264  *
00265  * \note
00266  * This function is not actually used anywhere
00267  *
00268  * \param iface The interface name whose IP address we wish to find
00269  * \param[out] address The interface's IP address is placed into this param
00270  * \retval -1 Failure. address is filled with 0s
00271  * \retval 0 Success
00272  */
00273 int ast_lookup_iface(char *iface, struct ast_sockaddr *address);
00274 
00275 /*!
00276  * \brief Duplicate the contents of a list of host access rules
00277  *
00278  * \details
00279  * A deep copy of all ast_has in the list is made. The returned
00280  * value is allocated on the heap and must be freed independently
00281  * of the input parameter when finished.
00282  *
00283  * \param original The ast_ha to copy
00284  * \retval The head of the list of duplicated ast_has
00285  */
00286 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original);
00287 
00288 /*!
00289  * \brief Duplicates the contests of a list of lists of host access rules.
00290  *
00291  * \details
00292  * A deep copy of an ast_acl list is made (which in turn means a deep copy of
00293  * each of the ast_ha structs contained within). The returned value is allocated
00294  * on the heap and must be freed independently of the input paramater when
00295  * finished.
00296  *
00297  * \param original The ast_acl_list to copy
00298  * \retval The new duplicated ast_acl_list
00299  */
00300 struct ast_acl_list *ast_duplicate_acl_list(struct ast_acl_list *original);
00301 
00302 /*!
00303  * \brief Find our IP address
00304  *
00305  * \details
00306  * This function goes through many iterations in an attempt to find
00307  * our IP address. If any step along the way should fail, we move to the
00308  * next item in the list. Here are the steps taken:
00309  * - If bindaddr has a non-zero IP address, that is copied into ourip
00310  * - We use a combination of gethostname and ast_gethostbyname to find our
00311  *   IP address.
00312  * - We use ast_ouraddrfor with 198.41.0.4 as the destination IP address
00313  * - We try some platform-specific socket operations to find the IP address
00314  *
00315  * \param[out] ourip Our IP address is written here when it is found
00316  * \param bindaddr A hint used for finding our IP. See the steps above for
00317  * more details
00318  * \param family Only addresses of the given family will be returned. Use 0
00319  * or AST_SOCKADDR_UNSPEC to get addresses of all families.
00320  * \retval 0 Success
00321  * \retval -1 Failure
00322  */
00323 int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family);
00324 
00325 /*!
00326  * \brief Convert a string to the appropriate COS value
00327  *
00328  * \param value The COS string to convert
00329  * \param[out] cos The integer representation of that COS value
00330  * \retval -1 Failure
00331  * \retval 0 Success
00332  */
00333 int ast_str2cos(const char *value, unsigned int *cos);
00334 
00335 /*!
00336  * \brief Convert a string to the appropriate TOS value
00337  *
00338  * \param value The TOS string to convert
00339  * \param[out] tos The integer representation of that TOS value
00340  * \retval -1 Failure
00341  * \retval 0 Success
00342  */
00343 int ast_str2tos(const char *value, unsigned int *tos);
00344 
00345 /*!
00346  * \brief Convert a TOS value into its string representation
00347  *
00348  * \param tos The TOS value to look up
00349  * \return The string equivalent of the TOS value
00350  */
00351 const char *ast_tos2str(unsigned int tos);
00352 
00353 /*!
00354  * \brief Retrieve a named ACL
00355  *
00356  * \details
00357  * This function attempts to find a named ACL. If found, a copy
00358  * of the requested ACL will be made which must be freed by
00359  * the caller.
00360  *
00361  * \param name Name of the ACL sought
00362  * \param[out] is_realtime will be true if the ACL being returned is from realtime
00363  * \param[out] is_undefined will be true if no ACL profile can be found for the requested name
00364  *
00365  * \retval A copy of the named ACL as an ast_ha
00366  * \retval NULL if no ACL could be found.
00367  */
00368 struct ast_ha *ast_named_acl_find(const char *name, int *is_realtime, int *is_undefined);
00369 
00370 /*!
00371  * \brief Initialize and configure the named ACL system.
00372  *
00373  * \details
00374  * This function will prepare the named ACL system for use.
00375  * For this reason, it needs to be called before other things that use ACLs are initialized.
00376  */
00377 int ast_named_acl_init(void);
00378 
00379 /*!
00380  * \brief reload/reconfigure the named ACL system.
00381  *
00382  * \details
00383  * This function is designed to trigger an event upon a successful reload that may update
00384  * ACL consumers.
00385  */
00386 int ast_named_acl_reload(void);
00387 
00388 #if defined(__cplusplus) || defined(c_plusplus)
00389 }
00390 #endif
00391 
00392 #endif /* _ASTERISK_ACL_H */