5f0ab042195a8ff565cfc0e7106ac64685a7cdbb
[ira/wip.git] / source3 / libaddns / dnsutils.c
1 /*
2   Linux DNS client library implementation
3
4   Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
5   Copyright (C) 2006 Gerald Carter <jerry@samba.org>
6
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
10
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.
15
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.
20
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
25 */
26
27 #include "dns.h"
28 #include <ctype.h>
29
30 static DNS_ERROR LabelList( TALLOC_CTX *mem_ctx,
31                             const char *name,
32                             struct dns_domain_label **presult )
33 {
34         struct dns_domain_label *result;
35         const char *dot;
36
37         for (dot = name; *dot != '\0'; dot += 1) {
38                 char c = *dot;
39
40                 if (c == '.')
41                         break;
42
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;
47
48                 return ERROR_DNS_INVALID_NAME;
49         }
50
51         if ((dot - name) > 63) {
52                 /*
53                  * DNS labels can only be 63 chars long
54                  */
55                 return ERROR_DNS_INVALID_NAME;
56         }
57
58         if (!(result = TALLOC_ZERO_P(mem_ctx, struct dns_domain_label))) {
59                 return ERROR_DNS_NO_MEMORY;
60         }
61
62         if (*dot == '\0') {
63                 /*
64                  * No dot around, so this is the last component
65                  */
66
67                 if (!(result->label = talloc_strdup(result, name))) {
68                         TALLOC_FREE(result);
69                         return ERROR_DNS_NO_MEMORY;
70                 }
71                 result->len = strlen(result->label);
72                 *presult = result;
73                 return ERROR_DNS_SUCCESS;
74         }
75
76         if (dot[1] == '.') {
77                 /*
78                  * Two dots in a row, reject
79                  */
80
81                 TALLOC_FREE(result);
82                 return ERROR_DNS_INVALID_NAME;
83         }
84
85         if (dot[1] != '\0') {
86                 /*
87                  * Something follows, get the rest
88                  */
89
90                 DNS_ERROR err = LabelList(result, dot+1, &result->next);
91
92                 if (!ERR_DNS_IS_OK(err)) {
93                         TALLOC_FREE(result);
94                         return err;
95                 }
96         }
97
98         result->len = (dot - name);
99
100         if (!(result->label = talloc_strndup(result, name, result->len))) {
101                 TALLOC_FREE(result);
102                 return ERROR_DNS_NO_MEMORY;
103         }
104
105         *presult = result;
106         return ERROR_DNS_SUCCESS;
107 }
108
109 DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
110                                        const char *pszDomainName,
111                                        struct dns_domain_name **presult )
112 {
113         struct dns_domain_name *result;
114         DNS_ERROR err;
115
116         if (!(result = talloc(mem_ctx, struct dns_domain_name))) {
117                 return ERROR_DNS_NO_MEMORY;
118         }
119
120         err = LabelList( result, pszDomainName, &result->pLabelList );
121         if (!ERR_DNS_IS_OK(err)) {
122                 TALLOC_FREE(result);
123                 return err;
124         }
125
126         *presult = result;
127         return ERROR_DNS_SUCCESS;
128 }
129
130 /*********************************************************************
131 *********************************************************************/
132
133 char *dns_generate_keyname( TALLOC_CTX *mem_ctx )
134 {
135         char *result = NULL;
136 #if defined(WITH_DNS_UPDATES)
137
138         uuid_t uuid;
139
140         /*
141          * uuid_unparse gives 36 bytes plus '\0'
142          */
143         if (!(result = TALLOC_ARRAY(mem_ctx, char, 37))) {
144                 return NULL;
145         }
146
147         uuid_generate( uuid );
148         uuid_unparse( uuid, result );
149
150 #endif
151
152         return result;
153 }