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:- elif(predicate_property(setgroups(_), defined)). 59initgroups(_, GID) :- % at least drop supplementary groups 60 setgroups([GID]). 61:- else. 62initgroups(_,_). 63:- endif. 64 65:- if(predicate_property(setgroups(_), defined)). 66:- export(setgroups/1). 67:- endif.
x if this is not accessible)133user_data(name, user_info(Nam, _, _, _, _, _, _), Nam). 134user_data(password, user_info(_, PWD, _, _, _, _, _), PWD). 135user_data(uid, user_info(_, _, UID, _, _, _, _), UID). 136user_data(gid, user_info(_, _, _, GID, _, _, _), GID). 137user_data(comment, user_info(_, _, _, _, GEC, _, _), GEC). 138user_data(home, user_info(_, _, _, _, _, HOM, _), HOM). 139user_data(shell, user_info(_, _, _, _, _, _, SHE), SHE).
x if this is not accessible)160group_data(name, group_info(Nam, _, _, _), Nam). 161group_data(password, group_info(_, PWD, _, _), PWD). 162group_data(gid, group_info(_, _, GID, _), GID). 163group_data(members, group_info(_, _, _, MBR), MBR). 164 165 /******************************* 166 * SETTING * 167 *******************************/
207set_user_and_group(User) :- 208 user_info(User, Data), 209 user_data(name, Data, Name), 210 user_data(uid, Data, UID), 211 user_data(gid, Data, GID), 212 initgroups(Name, GID), 213 setgid(GID), 214 setuid(UID). 215 216set_user_and_group(User, Group) :- 217 user_info(User, Data), 218 group_info(Group, GData), 219 user_data(name, Data, Name), 220 user_data(uid, Data, UID), 221 group_data(gid, GData, GID), 222 initgroups(Name, GID), 223 setgid(GID), 224 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.