summaryrefslogtreecommitdiffstats
path: root/lib/addns/dnsutils.c
blob: 37b862c7f0798121e8f71c10a39146074798b915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
  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, see <http://www.gnu.org/licenses/>.
*/

#include "dns.h"
#include <ctype.h>

static DNS_ERROR LabelList( TALLOC_CTX *mem_ctx,
			    const char *name,
			    struct dns_domain_label **presult )
{
	struct dns_domain_label *result;
	const char *dot;

	for (dot = name; *dot != '\0'; dot += 1) {
		char c = *dot;

		if (c == '.')
			break;

		if (c == '-') continue;
		if ((c >= 'a') && (c <= 'z')) continue;
		if ((c >= 'A') && (c <= 'Z')) continue;
		if ((c >= '0') && (c <= '9')) continue;

		return ERROR_DNS_INVALID_NAME;
	}

	if ((dot - name) > 63) {
		/*
		 * DNS labels can only be 63 chars long
		 */
		return ERROR_DNS_INVALID_NAME;
	}

	if (!(result = TALLOC_ZERO_P(mem_ctx, struct dns_domain_label))) {
		return ERROR_DNS_NO_MEMORY;
	}

	if (*dot == '\0') {
		/*
		 * No dot around, so this is the last component
		 */

		if (!(result->label = talloc_strdup(result, name))) {
			TALLOC_FREE(result);
			return ERROR_DNS_NO_MEMORY;
		}
		result->len = strlen(result->label);
		*presult = result;
		return ERROR_DNS_SUCCESS;
	}

	if (dot[1] == '.') {
		/*
		 * Two dots in a row, reject
		 */

		TALLOC_FREE(result);
		return ERROR_DNS_INVALID_NAME;
	}

	if (dot[1] != '\0') {
		/*
		 * Something follows, get the rest
		 */

		DNS_ERROR err = LabelList(result, dot+1, &result->next);

		if (!ERR_DNS_IS_OK(err)) {
			TALLOC_FREE(result);
			return err;
		}
	}

	result->len = (dot - name);

	if (!(result->label = talloc_strndup(result, name, result->len))) {
		TALLOC_FREE(result);
		return ERROR_DNS_NO_MEMORY;
	}

	*presult = result;
	return ERROR_DNS_SUCCESS;
}

DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
				       const char *pszDomainName,
				       struct dns_domain_name **presult )
{
	struct dns_domain_name *result;
	DNS_ERROR err;

	if (!(result = talloc(mem_ctx, struct dns_domain_name))) {
		return ERROR_DNS_NO_MEMORY;
	}

	err = LabelList( result, pszDomainName, &result->pLabelList );
	if (!ERR_DNS_IS_OK(err)) {
		TALLOC_FREE(result);
		return err;
	}

	*presult = result;
	return ERROR_DNS_SUCCESS;
}

/*********************************************************************
*********************************************************************/

char *dns_generate_keyname( TALLOC_CTX *mem_ctx )
{
	char *result = NULL;
#if defined(WITH_DNS_UPDATES)

	uuid_t uuid;

	/*
	 * uuid_unparse gives 36 bytes plus '\0'
	 */
	if (!(result = TALLOC_ARRAY(mem_ctx, char, 37))) {
		return NULL;
	}

	uuid_generate( uuid );
	uuid_unparse( uuid, result );

#endif

	return result;
}