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