xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /* $NetBSD: t_setdomainname.c,v 1.2 2012/03/25 08:17:54 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_setdomainname.c,v 1.2 2012/03/25 08:17:54 joerg Exp $");
33 
34 #include <sys/param.h>
35 
36 #include <atf-c.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #define	DOMAIN_BACKUP_FILE "domain.bak"
44 
45 static const char domains[][MAXHOSTNAMELEN] = {
46 	"1234567890",
47 	"abcdefghijklmnopqrst",
48 	"!#\xa4%&/(..xasS812=!=!(I(!;X;;X.as.dasa=?;,..<>|**^\xa8",
49 	"--------------------------------------------------------------------"
50 };
51 
52 static void
53 backup_domain(void)
54 {
55 	char domain[MAXHOSTNAMELEN];
56 	int fd;
57 	size_t l;
58 	ssize_t r,n = 0;
59 
60 	memset(domain, 0, sizeof(domain));
61 
62 	ATF_REQUIRE_EQ(0, getdomainname(domain, sizeof(domain)));
63 	l = strnlen(domain, MAXHOSTNAMELEN);
64 	fd = open(DOMAIN_BACKUP_FILE, O_WRONLY | O_CREAT | O_EXCL, 0644);
65 	ATF_REQUIRE(fd >= 0);
66 	while ((r = write(fd, domain + n, l - n)) > 0)
67 		n += r;
68 	ATF_REQUIRE_EQ(0, r);
69 	close(fd);
70 }
71 
72 static void
73 restore_domain(void)
74 {
75 	char domain[MAXHOSTNAMELEN];
76 	int fd;
77 	ssize_t r, n = 0;
78 
79 	memset(domain, 0, sizeof(domain));
80 	if ((fd = open(DOMAIN_BACKUP_FILE, O_RDONLY)) < 0)
81 		err(1, "open");
82 	while ((r = read(fd, domain + n, sizeof(domain) - n)) > 0)
83 		n += r;
84 	if (r < 0)
85 		err(1, "read");
86 	if (setdomainname(domain, n) != 0)
87 		err(1, "setdomainname");
88 	close(fd);
89 }
90 
91 ATF_TC_WITH_CLEANUP(setdomainname_basic);
92 ATF_TC_HEAD(setdomainname_basic, tc)
93 {
94 	atf_tc_set_md_var(tc, "descr", "A basic test of setdomainname(3)");
95 	atf_tc_set_md_var(tc, "require.user", "root");
96 }
97 
98 ATF_TC_BODY(setdomainname_basic, tc)
99 {
100 	char name[MAXHOSTNAMELEN];
101 	size_t i;
102 
103 	backup_domain();
104 	for (i = 0; i < __arraycount(domains); i++) {
105 
106 		(void)memset(name, 0, sizeof(name));
107 
108 #ifdef __FreeBSD__
109 		/*
110 		 * Sanity checks to ensure that the wrong invariant isn't being
111 		 * tested for per PR # 181127
112 		 */
113 		ATF_REQUIRE_EQ(sizeof(domains[i]), MAXHOSTNAMELEN);
114 		ATF_REQUIRE_EQ(sizeof(name), MAXHOSTNAMELEN);
115 
116 		ATF_REQUIRE(setdomainname(domains[i],sizeof(domains[i]) -  1) == 0);
117 		ATF_REQUIRE(getdomainname(name, sizeof(name) - 1) == 0);
118 #else
119 		ATF_REQUIRE(setdomainname(domains[i],sizeof(domains[i])) == 0);
120 		ATF_REQUIRE(getdomainname(name, sizeof(name)) == 0);
121 #endif
122 		ATF_REQUIRE(strcmp(domains[i], name) == 0);
123 	}
124 
125 }
126 
127 ATF_TC_CLEANUP(setdomainname_basic, tc)
128 {
129 	restore_domain();
130 }
131 
132 ATF_TC_WITH_CLEANUP(setdomainname_limit);
133 ATF_TC_HEAD(setdomainname_limit, tc)
134 {
135 	atf_tc_set_md_var(tc, "descr", "Too long domain name errors out?");
136 	atf_tc_set_md_var(tc, "require.user", "root");
137 }
138 
139 ATF_TC_BODY(setdomainname_limit, tc)
140 {
141 	char name[MAXHOSTNAMELEN + 1];
142 
143 	(void)memset(name, 0, sizeof(name));
144 	backup_domain();
145 
146 #ifdef __FreeBSD__
147 	ATF_REQUIRE(setdomainname(name, MAXHOSTNAMELEN - 1 ) == 0);
148 	ATF_REQUIRE(setdomainname(name, MAXHOSTNAMELEN) == -1);
149 #endif
150 	ATF_REQUIRE(setdomainname(name, sizeof(name)) == -1);
151 }
152 
153 ATF_TC_CLEANUP(setdomainname_limit, tc)
154 {
155 	restore_domain();
156 }
157 
158 ATF_TC(setdomainname_perm);
159 ATF_TC_HEAD(setdomainname_perm, tc)
160 {
161 	atf_tc_set_md_var(tc, "descr", "Can normal user set the domain name?");
162 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
163 }
164 
165 ATF_TC_BODY(setdomainname_perm, tc)
166 {
167 	char domain[MAXHOSTNAMELEN];
168 
169 	memset(domain, 0, sizeof(domain));
170 
171 	errno = 0;
172 	ATF_REQUIRE_ERRNO(EPERM, setdomainname(domain, sizeof(domain)) == -1);
173 }
174 
175 ATF_TP_ADD_TCS(tp)
176 {
177 	ATF_TP_ADD_TC(tp, setdomainname_basic);
178 	ATF_TP_ADD_TC(tp, setdomainname_limit);
179 	ATF_TP_ADD_TC(tp, setdomainname_perm);
180 
181 	return atf_no_error();
182 }
183