xref: /freebsd/tools/regression/priv/priv_proc_setlogin.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
19fa5f6b4SRobert Watson /*-
29fa5f6b4SRobert Watson  * Copyright (c) 2006 nCircle Network Security, Inc.
3d903306aSRobert Watson  * Copyright (c) 2007 Robert N. M. Watson
49fa5f6b4SRobert Watson  * All rights reserved.
59fa5f6b4SRobert Watson  *
69fa5f6b4SRobert Watson  * This software was developed by Robert N. M. Watson for the TrustedBSD
79fa5f6b4SRobert Watson  * Project under contract to nCircle Network Security, Inc.
89fa5f6b4SRobert Watson  *
99fa5f6b4SRobert Watson  * Redistribution and use in source and binary forms, with or without
109fa5f6b4SRobert Watson  * modification, are permitted provided that the following conditions
119fa5f6b4SRobert Watson  * are met:
129fa5f6b4SRobert Watson  * 1. Redistributions of source code must retain the above copyright
139fa5f6b4SRobert Watson  *    notice, this list of conditions and the following disclaimer.
149fa5f6b4SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
159fa5f6b4SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
169fa5f6b4SRobert Watson  *    documentation and/or other materials provided with the distribution.
179fa5f6b4SRobert Watson  *
189fa5f6b4SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199fa5f6b4SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209fa5f6b4SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219fa5f6b4SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
229fa5f6b4SRobert Watson  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239fa5f6b4SRobert Watson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
249fa5f6b4SRobert Watson  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
259fa5f6b4SRobert Watson  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
269fa5f6b4SRobert Watson  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
279fa5f6b4SRobert Watson  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
289fa5f6b4SRobert Watson  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299fa5f6b4SRobert Watson  */
309fa5f6b4SRobert Watson 
319fa5f6b4SRobert Watson /*
32d903306aSRobert Watson  * Test privileges for setlogin(); first query with getlogin() so that the
33d903306aSRobert Watson  * result is a no-op, since it affects the entire login session.
349fa5f6b4SRobert Watson  */
359fa5f6b4SRobert Watson 
36d903306aSRobert Watson #include <sys/param.h>
37d903306aSRobert Watson 
389fa5f6b4SRobert Watson #include <err.h>
399fa5f6b4SRobert Watson #include <errno.h>
409fa5f6b4SRobert Watson #include <unistd.h>
419fa5f6b4SRobert Watson 
429fa5f6b4SRobert Watson #include "main.h"
439fa5f6b4SRobert Watson 
44d903306aSRobert Watson static int initialized;
45d903306aSRobert Watson static char *loginname;
46d903306aSRobert Watson 
47d903306aSRobert Watson int
priv_proc_setlogin_setup(int asroot,int injail,struct test * test)48d903306aSRobert Watson priv_proc_setlogin_setup(int asroot, int injail, struct test *test)
499fa5f6b4SRobert Watson {
50d903306aSRobert Watson 
51d903306aSRobert Watson 	if (initialized)
52d903306aSRobert Watson 		return (0);
53d903306aSRobert Watson 	loginname = getlogin();
54d903306aSRobert Watson 	if (loginname == NULL) {
55d903306aSRobert Watson 		warn("priv_proc_setlogin_setup: getlogin");
56d903306aSRobert Watson 		return (-1);
57d903306aSRobert Watson 	}
58d903306aSRobert Watson 	initialized = 1;
59d903306aSRobert Watson 	return (0);
60d903306aSRobert Watson }
61d903306aSRobert Watson 
62d903306aSRobert Watson void
priv_proc_setlogin(int asroot,int injail,struct test * test)63d903306aSRobert Watson priv_proc_setlogin(int asroot, int injail, struct test *test)
64d903306aSRobert Watson {
659fa5f6b4SRobert Watson 	int error;
669fa5f6b4SRobert Watson 
679fa5f6b4SRobert Watson 	error = setlogin(loginname);
68d903306aSRobert Watson 	if (asroot && injail)
69d903306aSRobert Watson 		expect("priv_proc_setlogin(asroot, injail)", error, 0, 0);
70d903306aSRobert Watson 	if (asroot && !injail)
71d903306aSRobert Watson 		expect("priv_proc_setlogin(asroot, !injail)", error, 0, 0);
72d903306aSRobert Watson 	if (!asroot && injail)
73d903306aSRobert Watson 		expect("priv_proc_setlogin(!sroot, injail)", error, -1,
74d903306aSRobert Watson 		    EPERM);
75d903306aSRobert Watson 	if (!asroot && !injail)
76d903306aSRobert Watson 		expect("priv_proc_setlogin(!asroot, !injail)", error, -1,
77d903306aSRobert Watson 		    EPERM);
78d903306aSRobert Watson }
799fa5f6b4SRobert Watson 
80d903306aSRobert Watson void
priv_proc_setlogin_cleanup(int asroot,int injail,struct test * test)81d903306aSRobert Watson priv_proc_setlogin_cleanup(int asroot, int injail, struct test *test)
82d903306aSRobert Watson {
839fa5f6b4SRobert Watson 
84d903306aSRobert Watson 	if (initialized)
85d903306aSRobert Watson 		(void)setlogin(loginname);
869fa5f6b4SRobert Watson }
87