1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2011-2013, VU University Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(uid, 36 [ getuid/1, % -UID 37 getgid/1, % -GID 38 geteuid/1, % -UID 39 getegid/1, % -GID 40 getgroups/1, % -GIDs 41 user_info/2, % +User, -UserInfo 42 group_info/2, % +Group, -GroupInfo 43 user_data/3, % +Field, +UserInfo, -Value 44 group_data/3, % +Field, +GroupInfo, -Value 45 setuid/1, % +UID 46 setgid/1, % +GID 47 seteuid/1, % +UID 48 setegid/1, % +GID 49 50 set_user_and_group/1, % +User 51 set_user_and_group/2 % +User, +Group 52 ]). 53 54:- use_foreign_library(foreign(uid)). 55 56:- if(predicate_property(initgroups(_,_), defined)). 57:- export(initgroups/2). 58:- else. 59initgroups(_,_). 60:- endif. 61 62:- if(predicate_property(setgroups(_), defined)). 63:- export(setgroups/1). 64:- endif.
x
if this is not accessible)130user_data(name, user_info(Nam, _, _, _, _, _, _), Nam). 131user_data(password, user_info(_, PWD, _, _, _, _, _), PWD). 132user_data(uid, user_info(_, _, UID, _, _, _, _), UID). 133user_data(gid, user_info(_, _, _, GID, _, _, _), GID). 134user_data(comment, user_info(_, _, _, _, GEC, _, _), GEC). 135user_data(home, user_info(_, _, _, _, _, HOM, _), HOM). 136user_data(shell, user_info(_, _, _, _, _, _, SHE), SHE).
x
if this is not accessible)157group_data(name, group_info(Nam, _, _, _), Nam). 158group_data(password, group_info(_, PWD, _, _), PWD). 159group_data(gid, group_info(_, _, GID, _), GID). 160group_data(members, group_info(_, _, _, MBR), MBR). 161 162 /******************************* 163 * SETTING * 164 *******************************/
204set_user_and_group(User) :- 205 user_info(User, Data), 206 user_data(uid, Data, UID), 207 user_data(gid, Data, GID), 208 initgroups(User, GID), 209 setgid(GID), 210 setuid(UID). 211 212set_user_and_group(User, Group) :- 213 user_info(User, Data), 214 group_info(Group, GData), 215 user_data(uid, Data, UID), 216 user_data(gid, Data, UGID), 217 group_data(gid, GData, GID), 218 initgroups(User, UGID), 219 setgid(GID), 220 setuid(UID)
User and group management on Unix systems
This module provides and interface to user and group information on Posix systems. In addition, it allows for changing user and group ids. When changing user and group settings for the calling process, bear in mind that:
setgroups()
andinitgroups()
are not part of the POSIX standard and therefore the derived predicates may not be present.