Path: blob/main/tools/regression/priv/priv_proc_setlogin.c
96295 views
/*-1* Copyright (c) 2006 nCircle Network Security, Inc.2* Copyright (c) 2007 Robert N. M. Watson3* All rights reserved.4*5* This software was developed by Robert N. M. Watson for the TrustedBSD6* Project under contract to nCircle Network Security, Inc.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,21* INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED23* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR24* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF25* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING26* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS27* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930/*31* Test privileges for setlogin(); first query with getlogin() so that the32* result is a no-op, since it affects the entire login session.33*/3435#include <sys/param.h>3637#include <err.h>38#include <errno.h>39#include <unistd.h>4041#include "main.h"4243static int initialized;44static char *loginname;4546int47priv_proc_setlogin_setup(int asroot, int injail, struct test *test)48{4950if (initialized)51return (0);52loginname = getlogin();53if (loginname == NULL) {54warn("priv_proc_setlogin_setup: getlogin");55return (-1);56}57initialized = 1;58return (0);59}6061void62priv_proc_setlogin(int asroot, int injail, struct test *test)63{64int error;6566error = setlogin(loginname);67if (asroot && injail)68expect("priv_proc_setlogin(asroot, injail)", error, 0, 0);69if (asroot && !injail)70expect("priv_proc_setlogin(asroot, !injail)", error, 0, 0);71if (!asroot && injail)72expect("priv_proc_setlogin(!sroot, injail)", error, -1,73EPERM);74if (!asroot && !injail)75expect("priv_proc_setlogin(!asroot, !injail)", error, -1,76EPERM);77}7879void80priv_proc_setlogin_cleanup(int asroot, int injail, struct test *test)81{8283if (initialized)84(void)setlogin(loginname);85}868788