s3: Fix vfs_xattr_tdb.c
[Samba/gebeck_regimport.git] / lib / addns / dnsutils.c
blob5a63c61f149cb1bd3ef27189f53dc1a47d936f6e
1 /*
2 Linux DNS client library implementation
4 Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
5 Copyright (C) 2006 Gerald Carter <jerry@samba.org>
7 ** NOTE! The following LGPL license applies to the libaddns
8 ** library. This does NOT imply that all of Samba is released
9 ** under the LGPL
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "dns.h"
26 #include <ctype.h>
28 #ifdef HAVE_SYS_UUID_H
29 #include <sys/uuid.h>
30 #endif
32 static DNS_ERROR LabelList( TALLOC_CTX *mem_ctx,
33 const char *name,
34 struct dns_domain_label **presult )
36 struct dns_domain_label *result;
37 const char *dot;
39 for (dot = name; *dot != '\0'; dot += 1) {
40 char c = *dot;
42 if (c == '.')
43 break;
45 if (c == '-') continue;
46 if ((c >= 'a') && (c <= 'z')) continue;
47 if ((c >= 'A') && (c <= 'Z')) continue;
48 if ((c >= '0') && (c <= '9')) continue;
50 return ERROR_DNS_INVALID_NAME;
53 if ((dot - name) > 63) {
55 * DNS labels can only be 63 chars long
57 return ERROR_DNS_INVALID_NAME;
60 if (!(result = talloc_zero(mem_ctx, struct dns_domain_label))) {
61 return ERROR_DNS_NO_MEMORY;
64 if (*dot == '\0') {
66 * No dot around, so this is the last component
69 if (!(result->label = talloc_strdup(result, name))) {
70 TALLOC_FREE(result);
71 return ERROR_DNS_NO_MEMORY;
73 result->len = strlen(result->label);
74 *presult = result;
75 return ERROR_DNS_SUCCESS;
78 if (dot[1] == '.') {
80 * Two dots in a row, reject
83 TALLOC_FREE(result);
84 return ERROR_DNS_INVALID_NAME;
87 if (dot[1] != '\0') {
89 * Something follows, get the rest
92 DNS_ERROR err = LabelList(result, dot+1, &result->next);
94 if (!ERR_DNS_IS_OK(err)) {
95 TALLOC_FREE(result);
96 return err;
100 result->len = (dot - name);
102 if (!(result->label = talloc_strndup(result, name, result->len))) {
103 TALLOC_FREE(result);
104 return ERROR_DNS_NO_MEMORY;
107 *presult = result;
108 return ERROR_DNS_SUCCESS;
111 DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
112 const char *pszDomainName,
113 struct dns_domain_name **presult )
115 struct dns_domain_name *result;
116 DNS_ERROR err;
118 if (!(result = talloc(mem_ctx, struct dns_domain_name))) {
119 return ERROR_DNS_NO_MEMORY;
122 err = LabelList( result, pszDomainName, &result->pLabelList );
123 if (!ERR_DNS_IS_OK(err)) {
124 TALLOC_FREE(result);
125 return err;
128 *presult = result;
129 return ERROR_DNS_SUCCESS;
132 /*********************************************************************
133 *********************************************************************/
135 char *dns_generate_keyname( TALLOC_CTX *mem_ctx )
137 char *result = NULL;
138 #if defined(WITH_DNS_UPDATES)
140 uuid_t uuid;
143 * uuid_unparse gives 36 bytes plus '\0'
145 if (!(result = talloc_array(mem_ctx, char, 37))) {
146 return NULL;
149 uuid_generate( uuid );
150 uuid_unparse( uuid, result );
152 #endif
154 return result;