1*57718be8SEnji Cooper /* $NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper * by Jukka Ruohonen.
9*57718be8SEnji Cooper *
10*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper * are met:
13*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper *
19*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
30*57718be8SEnji Cooper */
31*57718be8SEnji Cooper #include <sys/cdefs.h>
32*57718be8SEnji Cooper __RCSID("$NetBSD: t_getlogin.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33*57718be8SEnji Cooper
34*57718be8SEnji Cooper #include <sys/param.h>
35*57718be8SEnji Cooper #include <sys/wait.h>
36*57718be8SEnji Cooper
37*57718be8SEnji Cooper #include <atf-c.h>
38*57718be8SEnji Cooper #include <errno.h>
39*57718be8SEnji Cooper #include <stdlib.h>
40*57718be8SEnji Cooper #include <string.h>
41*57718be8SEnji Cooper #include <unistd.h>
42*57718be8SEnji Cooper
43*57718be8SEnji Cooper ATF_TC(getlogin_r_err);
ATF_TC_HEAD(getlogin_r_err,tc)44*57718be8SEnji Cooper ATF_TC_HEAD(getlogin_r_err, tc)
45*57718be8SEnji Cooper {
46*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test errors from getlogin_r(2)");
47*57718be8SEnji Cooper }
48*57718be8SEnji Cooper
ATF_TC_BODY(getlogin_r_err,tc)49*57718be8SEnji Cooper ATF_TC_BODY(getlogin_r_err, tc)
50*57718be8SEnji Cooper {
51*57718be8SEnji Cooper char small[0];
52*57718be8SEnji Cooper
53*57718be8SEnji Cooper ATF_REQUIRE(getlogin_r(small, sizeof(small)) == ERANGE);
54*57718be8SEnji Cooper }
55*57718be8SEnji Cooper
56*57718be8SEnji Cooper ATF_TC(getlogin_same);
ATF_TC_HEAD(getlogin_same,tc)57*57718be8SEnji Cooper ATF_TC_HEAD(getlogin_same, tc)
58*57718be8SEnji Cooper {
59*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "getlogin(2) vs. getlogin_r(2)");
60*57718be8SEnji Cooper }
61*57718be8SEnji Cooper
ATF_TC_BODY(getlogin_same,tc)62*57718be8SEnji Cooper ATF_TC_BODY(getlogin_same, tc)
63*57718be8SEnji Cooper {
64*57718be8SEnji Cooper char buf[MAXLOGNAME];
65*57718be8SEnji Cooper char *str;
66*57718be8SEnji Cooper
67*57718be8SEnji Cooper str = getlogin();
68*57718be8SEnji Cooper
69*57718be8SEnji Cooper if (str == NULL)
70*57718be8SEnji Cooper return;
71*57718be8SEnji Cooper
72*57718be8SEnji Cooper ATF_REQUIRE(getlogin_r(buf, sizeof(buf)) == 0);
73*57718be8SEnji Cooper
74*57718be8SEnji Cooper if (strcmp(str, buf) != 0)
75*57718be8SEnji Cooper atf_tc_fail("getlogin(2) and getlogin_r(2) differ");
76*57718be8SEnji Cooper }
77*57718be8SEnji Cooper
78*57718be8SEnji Cooper ATF_TC(setlogin_basic);
ATF_TC_HEAD(setlogin_basic,tc)79*57718be8SEnji Cooper ATF_TC_HEAD(setlogin_basic, tc)
80*57718be8SEnji Cooper {
81*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test that setlogin(2) works");
82*57718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
83*57718be8SEnji Cooper }
84*57718be8SEnji Cooper
ATF_TC_BODY(setlogin_basic,tc)85*57718be8SEnji Cooper ATF_TC_BODY(setlogin_basic, tc)
86*57718be8SEnji Cooper {
87*57718be8SEnji Cooper char *name;
88*57718be8SEnji Cooper pid_t pid;
89*57718be8SEnji Cooper int sta;
90*57718be8SEnji Cooper
91*57718be8SEnji Cooper pid = fork();
92*57718be8SEnji Cooper ATF_REQUIRE(pid >= 0);
93*57718be8SEnji Cooper
94*57718be8SEnji Cooper if (pid == 0) {
95*57718be8SEnji Cooper
96*57718be8SEnji Cooper (void)setsid();
97*57718be8SEnji Cooper
98*57718be8SEnji Cooper if (setlogin("foobar") != 0)
99*57718be8SEnji Cooper _exit(EXIT_FAILURE);
100*57718be8SEnji Cooper
101*57718be8SEnji Cooper name = getlogin();
102*57718be8SEnji Cooper
103*57718be8SEnji Cooper if (name == NULL)
104*57718be8SEnji Cooper _exit(EXIT_FAILURE);
105*57718be8SEnji Cooper
106*57718be8SEnji Cooper if (strcmp(name, "foobar") != 0)
107*57718be8SEnji Cooper _exit(EXIT_FAILURE);
108*57718be8SEnji Cooper
109*57718be8SEnji Cooper _exit(EXIT_SUCCESS);
110*57718be8SEnji Cooper }
111*57718be8SEnji Cooper
112*57718be8SEnji Cooper (void)wait(&sta);
113*57718be8SEnji Cooper
114*57718be8SEnji Cooper if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
115*57718be8SEnji Cooper atf_tc_fail("setlogin(2) failed to set login name");
116*57718be8SEnji Cooper }
117*57718be8SEnji Cooper
118*57718be8SEnji Cooper ATF_TC(setlogin_err);
ATF_TC_HEAD(setlogin_err,tc)119*57718be8SEnji Cooper ATF_TC_HEAD(setlogin_err, tc)
120*57718be8SEnji Cooper {
121*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test errors from setlogin(2)");
122*57718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
123*57718be8SEnji Cooper }
124*57718be8SEnji Cooper
ATF_TC_BODY(setlogin_err,tc)125*57718be8SEnji Cooper ATF_TC_BODY(setlogin_err, tc)
126*57718be8SEnji Cooper {
127*57718be8SEnji Cooper char buf[MAXLOGNAME + 1];
128*57718be8SEnji Cooper char *name;
129*57718be8SEnji Cooper pid_t pid;
130*57718be8SEnji Cooper int sta;
131*57718be8SEnji Cooper
132*57718be8SEnji Cooper pid = fork();
133*57718be8SEnji Cooper ATF_REQUIRE(pid >= 0);
134*57718be8SEnji Cooper
135*57718be8SEnji Cooper (void)memset(buf, 'x', sizeof(buf));
136*57718be8SEnji Cooper
137*57718be8SEnji Cooper if (pid == 0) {
138*57718be8SEnji Cooper
139*57718be8SEnji Cooper (void)setsid();
140*57718be8SEnji Cooper
141*57718be8SEnji Cooper errno = 0;
142*57718be8SEnji Cooper
143*57718be8SEnji Cooper if (setlogin(buf) != -1)
144*57718be8SEnji Cooper _exit(EINVAL);
145*57718be8SEnji Cooper
146*57718be8SEnji Cooper if (errno != EINVAL)
147*57718be8SEnji Cooper _exit(EINVAL);
148*57718be8SEnji Cooper
149*57718be8SEnji Cooper errno = 0;
150*57718be8SEnji Cooper
151*57718be8SEnji Cooper if (setlogin((void *)-1) != -1)
152*57718be8SEnji Cooper _exit(EFAULT);
153*57718be8SEnji Cooper
154*57718be8SEnji Cooper if (errno != EFAULT)
155*57718be8SEnji Cooper _exit(EFAULT);
156*57718be8SEnji Cooper
157*57718be8SEnji Cooper name = getlogin();
158*57718be8SEnji Cooper
159*57718be8SEnji Cooper if (name == NULL)
160*57718be8SEnji Cooper _exit(EXIT_FAILURE);
161*57718be8SEnji Cooper
162*57718be8SEnji Cooper if (strcmp(name, "foobar") == 0)
163*57718be8SEnji Cooper _exit(EXIT_FAILURE);
164*57718be8SEnji Cooper
165*57718be8SEnji Cooper _exit(EXIT_SUCCESS);
166*57718be8SEnji Cooper }
167*57718be8SEnji Cooper
168*57718be8SEnji Cooper (void)wait(&sta);
169*57718be8SEnji Cooper
170*57718be8SEnji Cooper if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
171*57718be8SEnji Cooper
172*57718be8SEnji Cooper if (WEXITSTATUS(sta) == EFAULT)
173*57718be8SEnji Cooper atf_tc_fail("expected EFAULT, but the call succeeded");
174*57718be8SEnji Cooper
175*57718be8SEnji Cooper if (WEXITSTATUS(sta) == EINVAL)
176*57718be8SEnji Cooper atf_tc_fail("expected EINVAL, but the call succeeded");
177*57718be8SEnji Cooper
178*57718be8SEnji Cooper atf_tc_fail("setlogin(2) failed, but login name was set");
179*57718be8SEnji Cooper }
180*57718be8SEnji Cooper }
181*57718be8SEnji Cooper
182*57718be8SEnji Cooper ATF_TC(setlogin_perm);
ATF_TC_HEAD(setlogin_perm,tc)183*57718be8SEnji Cooper ATF_TC_HEAD(setlogin_perm, tc)
184*57718be8SEnji Cooper {
185*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test setlogin(2) as normal user");
186*57718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "unprivileged");
187*57718be8SEnji Cooper }
188*57718be8SEnji Cooper
ATF_TC_BODY(setlogin_perm,tc)189*57718be8SEnji Cooper ATF_TC_BODY(setlogin_perm, tc)
190*57718be8SEnji Cooper {
191*57718be8SEnji Cooper char *name;
192*57718be8SEnji Cooper pid_t pid;
193*57718be8SEnji Cooper int sta;
194*57718be8SEnji Cooper
195*57718be8SEnji Cooper pid = fork();
196*57718be8SEnji Cooper ATF_REQUIRE(pid >= 0);
197*57718be8SEnji Cooper
198*57718be8SEnji Cooper if (pid == 0) {
199*57718be8SEnji Cooper
200*57718be8SEnji Cooper (void)setsid();
201*57718be8SEnji Cooper
202*57718be8SEnji Cooper errno = 0;
203*57718be8SEnji Cooper
204*57718be8SEnji Cooper if (setlogin("foobar") != -1)
205*57718be8SEnji Cooper _exit(EXIT_FAILURE);
206*57718be8SEnji Cooper
207*57718be8SEnji Cooper if (errno != EPERM)
208*57718be8SEnji Cooper _exit(EXIT_FAILURE);
209*57718be8SEnji Cooper
210*57718be8SEnji Cooper name = getlogin();
211*57718be8SEnji Cooper
212*57718be8SEnji Cooper if (name == NULL)
213*57718be8SEnji Cooper _exit(EXIT_FAILURE);
214*57718be8SEnji Cooper
215*57718be8SEnji Cooper if (strcmp(name, "foobar") == 0)
216*57718be8SEnji Cooper _exit(EXIT_FAILURE);
217*57718be8SEnji Cooper
218*57718be8SEnji Cooper _exit(EXIT_SUCCESS);
219*57718be8SEnji Cooper }
220*57718be8SEnji Cooper
221*57718be8SEnji Cooper (void)wait(&sta);
222*57718be8SEnji Cooper
223*57718be8SEnji Cooper if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
224*57718be8SEnji Cooper atf_tc_fail("login name was set as an unprivileged user");
225*57718be8SEnji Cooper }
226*57718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)227*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
228*57718be8SEnji Cooper {
229*57718be8SEnji Cooper
230*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, getlogin_r_err);
231*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, getlogin_same);
232*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, setlogin_basic);
233*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, setlogin_err);
234*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, setlogin_perm);
235*57718be8SEnji Cooper
236*57718be8SEnji Cooper return atf_no_error();
237*57718be8SEnji Cooper }
238