r23708: - Add define for WINBIND_WARN_PWD_EXPIRE.
[Samba.git] / source / libaddns / dnsutils.c
blob5f0ab042195a8ff565cfc0e7106ac64685a7cdbb
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, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301 USA
27 #include "dns.h"
28 #include <ctype.h>
30 static DNS_ERROR LabelList( TALLOC_CTX *mem_ctx,
31 const char *name,
32 struct dns_domain_label **presult )
34 struct dns_domain_label *result;
35 const char *dot;
37 for (dot = name; *dot != '\0'; dot += 1) {
38 char c = *dot;
40 if (c == '.')
41 break;
43 if (c == '-') continue;
44 if ((c >= 'a') && (c <= 'z')) continue;
45 if ((c >= 'A') && (c <= 'Z')) continue;
46 if ((c >= '0') && (c <= '9')) continue;
48 return ERROR_DNS_INVALID_NAME;
51 if ((dot - name) > 63) {
53 * DNS labels can only be 63 chars long
55 return ERROR_DNS_INVALID_NAME;
58 if (!(result = TALLOC_ZERO_P(mem_ctx, struct dns_domain_label))) {
59 return ERROR_DNS_NO_MEMORY;
62 if (*dot == '\0') {
64 * No dot around, so this is the last component
67 if (!(result->label = talloc_strdup(result, name))) {
68 TALLOC_FREE(result);
69 return ERROR_DNS_NO_MEMORY;
71 result->len = strlen(result->label);
72 *presult = result;
73 return ERROR_DNS_SUCCESS;
76 if (dot[1] == '.') {
78 * Two dots in a row, reject
81 TALLOC_FREE(result);
82 return ERROR_DNS_INVALID_NAME;
85 if (dot[1] != '\0') {
87 * Something follows, get the rest
90 DNS_ERROR err = LabelList(result, dot+1, &result->next);
92 if (!ERR_DNS_IS_OK(err)) {
93 TALLOC_FREE(result);
94 return err;
98 result->len = (dot - name);
100 if (!(result->label = talloc_strndup(result, name, result->len))) {
101 TALLOC_FREE(result);
102 return ERROR_DNS_NO_MEMORY;
105 *presult = result;
106 return ERROR_DNS_SUCCESS;
109 DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
110 const char *pszDomainName,
111 struct dns_domain_name **presult )
113 struct dns_domain_name *result;
114 DNS_ERROR err;
116 if (!(result = talloc(mem_ctx, struct dns_domain_name))) {
117 return ERROR_DNS_NO_MEMORY;
120 err = LabelList( result, pszDomainName, &result->pLabelList );
121 if (!ERR_DNS_IS_OK(err)) {
122 TALLOC_FREE(result);
123 return err;
126 *presult = result;
127 return ERROR_DNS_SUCCESS;
130 /*********************************************************************
131 *********************************************************************/
133 char *dns_generate_keyname( TALLOC_CTX *mem_ctx )
135 char *result = NULL;
136 #if defined(WITH_DNS_UPDATES)
138 uuid_t uuid;
141 * uuid_unparse gives 36 bytes plus '\0'
143 if (!(result = TALLOC_ARRAY(mem_ctx, char, 37))) {
144 return NULL;
147 uuid_generate( uuid );
148 uuid_unparse( uuid, result );
150 #endif
152 return result;