1 kumpf 1.2 /*
2 //%2006////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
5 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
6 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
7 // IBM Corp.; EMC Corporation, The Open Group.
8 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
9 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
10 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
11 // EMC Corporation; VERITAS Software Corporation; The Open Group.
12 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
13 // EMC Corporation; Symantec Corporation; The Open Group.
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to
17 // deal in the Software without restriction, including without limitation the
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 // sell copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
23 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
24 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
26 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32 */
33 #include <sys/types.h>
34 #include <pwd.h>
35 #include "User.h"
36 #include "Log.h"
37 #include "Strlcpy.h"
38
39 #define PWD_BUFF_SIZE 4096
40
41 /*
42 **==============================================================================
43 kumpf 1.2 **
44 ** GetUserInfo()
45 **
46 ** Lookup the given user's uid and gid.
47 **
48 **==============================================================================
49 */
50
51 int GetUserInfo(const char* user, int* uid, int* gid)
52 {
53 struct passwd pwd;
54 char buffer[PWD_BUFF_SIZE];
55 struct passwd* ptr = 0;
56
57 if (getpwnam_r(user, &pwd, buffer, PWD_BUFF_SIZE, &ptr) != 0 || !ptr)
58 {
59 Log(LL_TRACE, "getpwnam_r(%s, ...) failed", user);
60 return -1;
61 }
62
63 *uid = ptr->pw_uid;
64 kumpf 1.2 *gid = ptr->pw_gid;
65
66 return 0;
67 }
68
69 /*
70 **==============================================================================
71 **
72 ** GetUserName()
73 **
74 ** Lookup the user name for the specified user ID.
75 **
76 **==============================================================================
77 */
78
79 int GetUserName(int uid, char username[EXECUTOR_BUFFER_SIZE])
80 {
81 struct passwd pwd;
82 char buffer[PWD_BUFF_SIZE];
83 struct passwd* ptr = 0;
84
85 kumpf 1.2 if (getpwuid_r(uid, &pwd, buffer, PWD_BUFF_SIZE, &ptr) != 0 || !ptr)
86 {
87 Log(LL_TRACE, "getpwuid_r(%d, ...) failed", uid);
88 username[0] = '\0';
89 return -1;
90 }
91
92 Strlcpy(username, ptr->pw_name, EXECUTOR_BUFFER_SIZE);
93 return 0;
94 }
|