2 * security.c: Routines to aid secure uid operations
4 * Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
5 * Copyright (C) 2001, 2003, 2004, 2007, 2010, 2011 Colin Watson.
7 * This file is part of man-db.
9 * man-db is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * man-db is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with man-db; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 * Mon Aug 8 20:35:30 BST 1994 Wilf. (G.Wilford@ee.surrey.ac.uk)
28 #endif /* HAVE_CONFIG_H */
36 #include <sys/types.h>
39 #define _(String) gettext (String)
41 #include "manconfig.h"
52 * This is the name of the user that the preformatted man pages belong to.
53 * If you are running man as a setuid program, you should make sure
54 * that all of the cat pages and the directories that
55 * they live in are writeable by this user.
63 uid_t ruid; /* initial real user id */
64 uid_t euid; /* initial effective user id */
65 uid_t uid; /* current euid */
66 gid_t rgid; /* initial real group id */
67 gid_t egid; /* initial effective group id */
68 gid_t gid; /* current egid */
70 static struct passwd *man_owner;
72 /* Keep a count of how many times we've dropped privileges, and only regain
73 * them if regain_effective_privs() is called an equal number of times.
75 static int priv_drop_count = 0;
77 static void gripe_set_euid (void)
79 error (FATAL, errno, _("can't set effective uid"));
82 #endif /* MAN_OWNER */
84 void init_security (void)
88 uid = euid = geteuid ();
89 debug ("ruid=%d, euid=%d\n", (int) ruid, (int) euid);
91 gid = egid = getegid ();
92 debug ("rgid=%d, egid=%d\n", (int) rgid, (int) egid);
94 drop_effective_privs ();
95 #endif /* MAN_OWNER */
98 bool _GL_ATTRIBUTE_PURE running_setuid (void)
102 #else /* !MAN_OWNER */
108 /* Return a pointer to the password entry structure for MAN_OWNER. This
109 * structure will be statically stored.
111 struct passwd *get_man_owner (void)
116 man_owner = getpwnam (MAN_OWNER);
118 error (FAIL, 0, _("the setuid man user \"%s\" does not exist"),
123 #endif /* MAN_OWNER */
126 * function to gain user privs by either (a) dropping effective privs
127 * completely (saved ids) or (b) reversing euid w/ uid.
128 * Ignore if superuser.
130 void drop_effective_privs (void)
134 debug ("drop_effective_privs()\n");
135 if (idpriv_temp_drop ())
142 debug ("++priv_drop_count = %d\n", priv_drop_count);
143 #endif /* MAN_OWNER */
147 * function to (re)gain setuid privs by (a) setting euid from suid or (b)
148 * (re)reversing uid w/ euid. Ignore if superuser.
150 void regain_effective_privs (void)
153 if (priv_drop_count) {
155 debug ("--priv_drop_count = %d\n", priv_drop_count);
161 debug ("regain_effective_privs()\n");
162 if (idpriv_temp_restore ())
168 #endif /* MAN_OWNER */
171 /* Pipeline command pre-exec hook to permanently drop privileges. */
172 void drop_privs (void *data _GL_UNUSED)
177 #endif /* MAN_OWNER */