priv_proc_setlogin.c revision 162271
1/*- 2 * Copyright (c) 2006 nCircle Network Security, Inc. 3 * All rights reserved. 4 * 5 * This software was developed by Robert N. M. Watson for the TrustedBSD 6 * Project under contract to nCircle Network Security, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * 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 LIMITED 23 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD: head/tools/regression/priv/priv_proc_setlogin.c 162271 2006-09-13 09:05:39Z rwatson $ 30 */ 31 32/* 33 * Test that privilege is required to call setlogin(). Do so by first 34 * querying with getlogin(), then setting the result back using setlogin(), 35 * at first with privilege, then without. 36 */ 37 38#include <err.h> 39#include <errno.h> 40#include <unistd.h> 41 42#include "main.h" 43 44void 45priv_proc_setlogin(void) 46{ 47 char *loginname; 48 int error; 49 50 assert_root(); 51 52 loginname = getlogin(); 53 if (loginname == NULL) 54 err(-1, "getlogin"); 55 56 /* 57 * First, with privilege. 58 */ 59 error = setlogin(loginname); 60 if (error) 61 err(-1, "setlogin(%s) as root", loginname); 62 63 /* 64 * Then again, without privilege. 65 */ 66 set_euid(UID_OTHER); 67 68 error = setlogin(loginname); 69 if (error == 0) 70 errx(-1, "setlogin(%s) succeeded as !root", loginname); 71 if (errno != EPERM) 72 err(-1, "setlogin(%s) wrong errno %d", loginname, errno); 73} 74