00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, 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/io.h" 00033 00034 #define AST_SENSE_DENY 0 00035 #define AST_SENSE_ALLOW 1 00036 00037 /* Host based access control */ 00038 00039 /*! \brief internal representation of acl entries 00040 * In principle user applications would have no need for this, 00041 * but there is sometimes a need to extract individual items, 00042 * e.g. to print them, and rather than defining iterators to 00043 * navigate the list, and an externally visible 'struct ast_ha_entry', 00044 * at least in the short term it is more convenient to make the whole 00045 * thing public and let users play with them. 00046 */ 00047 struct ast_ha { 00048 /* Host access rule */ 00049 struct in_addr netaddr; 00050 struct in_addr netmask; 00051 int sense; 00052 struct ast_ha *next; 00053 }; 00054 00055 /*! 00056 * \brief Free a list of HAs 00057 * 00058 * \details 00059 * Given the head of a list of HAs, it and all appended 00060 * HAs are freed 00061 * 00062 * \param ha The head of the list of HAs to free 00063 * \retval void 00064 */ 00065 void ast_free_ha(struct ast_ha *ha); 00066 00067 /*! 00068 * \brief Copy the contents of one HA to another 00069 * 00070 * \details 00071 * This copies the internals of the 'from' HA to the 'to' 00072 * HA. It is important that the 'to' HA has been allocated 00073 * prior to calling this function 00074 * 00075 * \param from Source HA to copy 00076 * \param to Destination HA to copy to 00077 * \retval void 00078 */ 00079 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to); 00080 00081 /*! 00082 * \brief Add a new rule to a list of HAs 00083 * 00084 * \details 00085 * This adds the new host access rule to the end of the list 00086 * whose head is specified by the path parameter. Rules are 00087 * evaluated in a way such that if multiple rules apply to 00088 * a single IP address/subnet mask, then the rule latest 00089 * in the list will be used. 00090 * 00091 * \param sense Either "permit" or "deny" (Actually any 'p' word will result 00092 * in permission, and any other word will result in denial) 00093 * \param stuff The IP address and subnet mask, separated with a '/'. The subnet 00094 * mask can either be in dotted-decimal format or in CIDR notation (i.e. 0-32). 00095 * \param path The head of the HA list to which we wish to append our new rule. If 00096 * NULL is passed, then the new rule will become the head of the list 00097 * \param[out] error The integer error points to will be set non-zero if an error occurs 00098 * \return The head of the HA list 00099 */ 00100 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error); 00101 00102 /*! 00103 * \brief Apply a set of rules to a given IP address 00104 * 00105 * \details 00106 * The list of host access rules is traversed, beginning with the 00107 * input rule. If the IP address given matches a rule, the "sense" 00108 * of that rule is used as the return value. Note that if an IP 00109 * address matches multiple rules that the last one matched will be 00110 * the one whose sense will be returned. 00111 * 00112 * \param ha The head of the list of host access rules to follow 00113 * \param sin A sockaddr_in whose address is considered when matching rules 00114 * \retval AST_SENSE_ALLOW The IP address passes our ACL 00115 * \retval AST_SENSE_DENY The IP address fails our ACL 00116 */ 00117 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin); 00118 00119 /*! 00120 * \brief Get the IP address given a hostname 00121 * 00122 * \details 00123 * Similar in nature to ast_gethostbyname, except that instead 00124 * of getting an entire hostent structure, you instead are given 00125 * only the IP address inserted into a sockaddr_in structure. 00126 * 00127 * \param[out] sin The IP address is written into sin->sin_addr 00128 * \param value The hostname to look up 00129 * \retval 0 Success 00130 * \retval -1 Failure 00131 */ 00132 int ast_get_ip(struct sockaddr_in *sin, const char *value); 00133 00134 /*! 00135 * \brief Get the IP address given a hostname and optional service 00136 * 00137 * \details 00138 * If the service parameter is non-NULL, then an SRV lookup will be made by 00139 * prepending the service to the value parameter, separated by a '.' 00140 * For example, if value is "example.com" and service is "_sip._udp" then 00141 * an SRV lookup will be done for "_sip._udp.example.com". If service is NULL, 00142 * then this function acts exactly like a call to ast_get_ip. 00143 * 00144 * \param[out] sin The IP address is written into sin->sin_addr 00145 * \param value The hostname to look up 00146 * \param service A specific service provided by the host. A NULL service results 00147 * in an A-record lookup instead of an SRV lookup 00148 * \retval 0 Success 00149 * \retval -1 Failure 00150 */ 00151 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service); 00152 00153 /*! 00154 * \brief Get our local IP address when contacting a remote host 00155 * 00156 * \details 00157 * This function will attempt to connect(2) to them over UDP using a source 00158 * port of 5060. If the connect(2) call is successful, then we inspect the 00159 * sockaddr_in output parameter of connect(2) to determine the IP address 00160 * used to connect to them. This IP address is then copied into us. 00161 * 00162 * \param them The IP address to which we wish to attempt to connect 00163 * \param[out] us The source IP address used to connect to them 00164 * \retval -1 Failure 00165 * \retval 0 Success 00166 */ 00167 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us); 00168 00169 /*! 00170 * \brief Find an IP address associated with a specific interface 00171 * 00172 * \details 00173 * Given an interface such as "eth0" we find the primary IP address 00174 * associated with it using the SIOCGIFADDR ioctl. If the ioctl call 00175 * should fail, we populate address with 0s. 00176 * 00177 * \note 00178 * This function is not actually used anywhere 00179 * 00180 * \param iface The interface name whose IP address we wish to find 00181 * \param[out] address The interface's IP address is placed into this param 00182 * \retval -1 Failure. address is filled with 0s 00183 * \retval 0 Success 00184 */ 00185 int ast_lookup_iface(char *iface, struct in_addr *address); 00186 00187 /*! 00188 * \brief Duplicate the contents of a list of host access rules 00189 * 00190 * \details 00191 * A deep copy of all ast_has in the list is made. The returned 00192 * value is allocated on the heap and must be freed independently 00193 * of the input parameter when finished. 00194 * 00195 * \note 00196 * This function is not actually used anywhere. 00197 * 00198 * \param original The ast_ha to copy 00199 * \retval The head of the list of duplicated ast_has 00200 */ 00201 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original); 00202 00203 /*! 00204 * \brief Find our IP address 00205 * 00206 * \details 00207 * This function goes through many iterations in an attempt to find 00208 * our IP address. If any step along the way should fail, we move to the 00209 * next item in the list. Here are the steps taken: 00210 * - If bindaddr has a non-zero IP address, that is copied into ourip 00211 * - We use a combination of gethostname and ast_gethostbyname to find our 00212 * IP address. 00213 * - We use ast_ouraddrfor with 198.41.0.4 as the destination IP address 00214 * - We try some platform-specific socket operations to find the IP address 00215 * 00216 * \param[out] ourip Our IP address is written here when it is found 00217 * \param bindaddr A hint used for finding our IP. See the steps above for 00218 * more details 00219 * \retval 0 Success 00220 * \retval -1 Failure 00221 */ 00222 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr); 00223 00224 /*! 00225 * \brief Convert a string to the appropriate COS value 00226 * 00227 * \param value The COS string to convert 00228 * \param[out] cos The integer representation of that COS value 00229 * \retval -1 Failure 00230 * \retval 0 Success 00231 */ 00232 int ast_str2cos(const char *value, unsigned int *cos); 00233 00234 /*! 00235 * \brief Convert a string to the appropriate TOS value 00236 * 00237 * \param value The TOS string to convert 00238 * \param[out] tos The integer representation of that TOS value 00239 * \retval -1 Failure 00240 * \retval 0 Success 00241 */ 00242 int ast_str2tos(const char *value, unsigned int *tos); 00243 00244 /*! 00245 * \brief Convert a TOS value into its string representation 00246 * 00247 * \param tos The TOS value to look up 00248 * \return The string equivalent of the TOS value 00249 */ 00250 const char *ast_tos2str(unsigned int tos); 00251 00252 #if defined(__cplusplus) || defined(c_plusplus) 00253 } 00254 #endif 00255 00256 #endif /* _ASTERISK_ACL_H */