2006-08-24 19:43:32 +04:00
/*
Linux DNS client library implementation
Copyright ( C ) 2006 Krishna Ganugapati < krishnag @ centeris . com >
Copyright ( C ) 2006 Gerald Carter < jerry @ samba . org >
* * NOTE ! The following LGPL license applies to the libaddns
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2.1 of the License , or ( at your option ) any later version .
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA
02110 - 1301 USA
*/
# include "dns.h"
# include <ctype.h>
2006-11-18 00:46:26 +03:00
static DNS_ERROR LabelList ( TALLOC_CTX * mem_ctx ,
const char * name ,
struct dns_domain_label * * presult )
2006-08-24 19:43:32 +04:00
{
2006-11-18 00:46:26 +03:00
struct dns_domain_label * result ;
const char * dot ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
for ( dot = name ; * dot ! = ' \0 ' ; dot + = 1 ) {
char c = * dot ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( c = = ' . ' )
break ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( c = = ' - ' ) continue ;
if ( ( c > = ' a ' ) & & ( c < = ' z ' ) ) continue ;
if ( ( c > = ' A ' ) & & ( c < = ' Z ' ) ) continue ;
if ( ( c > = ' 0 ' ) & & ( c < = ' 9 ' ) ) continue ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
return ERROR_DNS_INVALID_NAME ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
if ( ( dot - name ) > 63 ) {
/*
* DNS labels can only be 63 chars long
*/
return ERROR_DNS_INVALID_NAME ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
if ( ! ( result = TALLOC_ZERO_P ( mem_ctx , struct dns_domain_label ) ) ) {
return ERROR_DNS_NO_MEMORY ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
if ( * dot = = ' \0 ' ) {
/*
* No dot around , so this is the last component
*/
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( ! ( result - > label = talloc_strdup ( result , name ) ) ) {
TALLOC_FREE ( result ) ;
return ERROR_DNS_NO_MEMORY ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
result - > len = strlen ( result - > label ) ;
* presult = result ;
return ERROR_DNS_SUCCESS ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
if ( dot [ 1 ] = = ' . ' ) {
/*
* Two dots in a row , reject
*/
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
TALLOC_FREE ( result ) ;
return ERROR_DNS_INVALID_NAME ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
if ( dot [ 1 ] ! = ' \0 ' ) {
/*
* Something follows , get the rest
*/
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
DNS_ERROR err = LabelList ( result , dot + 1 , & result - > next ) ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( ! ERR_DNS_IS_OK ( err ) ) {
TALLOC_FREE ( result ) ;
return err ;
}
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
result - > len = ( dot - name ) ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( ! ( result - > label = talloc_strndup ( result , name , result - > len ) ) ) {
TALLOC_FREE ( result ) ;
return ERROR_DNS_NO_MEMORY ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
* presult = result ;
return ERROR_DNS_SUCCESS ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
DNS_ERROR dns_domain_name_from_string ( TALLOC_CTX * mem_ctx ,
const char * pszDomainName ,
struct dns_domain_name * * presult )
2006-08-24 19:43:32 +04:00
{
2006-11-18 00:46:26 +03:00
struct dns_domain_name * result ;
DNS_ERROR err ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
if ( ! ( result = talloc ( mem_ctx , struct dns_domain_name ) ) ) {
return ERROR_DNS_NO_MEMORY ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
err = LabelList ( result , pszDomainName , & result - > pLabelList ) ;
if ( ! ERR_DNS_IS_OK ( err ) ) {
TALLOC_FREE ( result ) ;
return err ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
* presult = result ;
return ERROR_DNS_SUCCESS ;
2006-08-24 19:43:32 +04:00
}
/*********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-11-18 00:46:26 +03:00
char * dns_generate_keyname ( TALLOC_CTX * mem_ctx )
2006-08-24 19:43:32 +04:00
{
2006-11-18 00:46:26 +03:00
char * result = NULL ;
# if defined(WITH_DNS_UPDATES)
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
uuid_t uuid ;
2006-08-24 19:43:32 +04:00
2006-11-18 00:46:26 +03:00
/*
* uuid_unparse gives 36 bytes plus ' \0 '
*/
if ( ! ( result = TALLOC_ARRAY ( mem_ctx , char , 37 ) ) ) {
return NULL ;
2006-08-24 19:43:32 +04:00
}
2006-11-18 00:46:26 +03:00
uuid_generate ( uuid ) ;
uuid_unparse ( uuid , result ) ;
2006-08-24 19:43:32 +04:00
# endif
2006-11-18 00:46:26 +03:00
return result ;
2006-08-24 19:43:32 +04:00
}