2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
16 #include <errno_private.h>
17 #include <libroot_private.h>
18 #include <RegistrarDefs.h>
19 #include <user_group.h>
21 #include <util/KMessage.h>
24 using BPrivate::UserGroupLocker
;
25 using BPrivate::relocate_pointer
;
28 static KMessage sPasswdDBReply
;
29 static passwd
** sPasswdEntries
= NULL
;
30 static size_t sPasswdEntryCount
= 0;
31 static size_t sIterationIndex
= 0;
33 static struct passwd sPasswdBuffer
;
34 static char sPasswdStringBuffer
[MAX_PASSWD_BUFFER_SIZE
];
38 query_passwd_entry(const char* name
, uid_t _uid
, struct passwd
*passwd
,
39 char *buffer
, size_t bufferSize
, struct passwd
**_result
)
43 KMessage
message(BPrivate::B_REG_GET_USER
);
45 message
.AddString("name", name
);
47 message
.AddInt32("uid", _uid
);
50 status_t error
= BPrivate::send_authentication_request_to_registrar(message
,
53 return error
== ENOENT
? B_OK
: error
;
62 if ((error
= reply
.FindInt32("uid", &uid
)) != B_OK
63 || (error
= reply
.FindInt32("gid", &gid
)) != B_OK
64 || (error
= reply
.FindString("name", &name
)) != B_OK
65 || (error
= reply
.FindString("password", &password
)) != B_OK
66 || (error
= reply
.FindString("home", &home
)) != B_OK
67 || (error
= reply
.FindString("shell", &shell
)) != B_OK
68 || (error
= reply
.FindString("real name", &realName
)) != B_OK
) {
72 error
= BPrivate::copy_passwd_to_buffer(name
, password
, uid
, gid
, home
,
73 shell
, realName
, passwd
, buffer
, bufferSize
);
84 if (sPasswdEntries
!= NULL
)
88 KMessage
message(BPrivate::B_REG_GET_PASSWD_DB
);
89 status_t error
= BPrivate::send_authentication_request_to_registrar(message
,
98 if ((error
= sPasswdDBReply
.FindInt32("count", &count
)) != B_OK
99 || (error
= sPasswdDBReply
.FindData("entries", B_RAW_TYPE
,
100 (const void**)&entries
, &numBytes
)) != B_OK
) {
104 // relocate the entries
105 addr_t baseAddress
= (addr_t
)entries
;
106 for (int32 i
= 0; i
< count
; i
++) {
107 passwd
* entry
= relocate_pointer(baseAddress
, entries
[i
]);
108 relocate_pointer(baseAddress
, entry
->pw_name
);
109 relocate_pointer(baseAddress
, entry
->pw_passwd
);
110 relocate_pointer(baseAddress
, entry
->pw_dir
);
111 relocate_pointer(baseAddress
, entry
->pw_shell
);
112 relocate_pointer(baseAddress
, entry
->pw_gecos
);
115 sPasswdEntries
= entries
;
116 sPasswdEntryCount
= count
;
128 struct passwd
* result
= NULL
;
129 int status
= getpwent_r(&sPasswdBuffer
, sPasswdStringBuffer
,
130 sizeof(sPasswdStringBuffer
), &result
);
138 getpwent_r(struct passwd
* passwd
, char* buffer
, size_t bufferSize
,
139 struct passwd
** _result
)
143 int status
= B_NO_MEMORY
;
147 if ((status
= init_passwd_db()) == B_OK
) {
148 if (sIterationIndex
>= sPasswdEntryCount
)
151 status
= BPrivate::copy_passwd_to_buffer(
152 sPasswdEntries
[sIterationIndex
], passwd
, buffer
, bufferSize
);
154 if (status
== B_OK
) {
176 UserGroupLocker locker
;
178 sPasswdDBReply
.Unset();
179 sPasswdEntries
= NULL
;
180 sPasswdEntryCount
= 0;
186 getpwnam(const char *name
)
188 struct passwd
* result
= NULL
;
189 int status
= getpwnam_r(name
, &sPasswdBuffer
, sPasswdStringBuffer
,
190 sizeof(sPasswdStringBuffer
), &result
);
198 getpwnam_r(const char *name
, struct passwd
*passwd
, char *buffer
,
199 size_t bufferSize
, struct passwd
**_result
)
201 return query_passwd_entry(name
, 0, passwd
, buffer
, bufferSize
, _result
);
208 struct passwd
* result
= NULL
;
209 int status
= getpwuid_r(uid
, &sPasswdBuffer
, sPasswdStringBuffer
,
210 sizeof(sPasswdStringBuffer
), &result
);
218 getpwuid_r(uid_t uid
, struct passwd
*passwd
, char *buffer
,
219 size_t bufferSize
, struct passwd
**_result
)
221 return query_passwd_entry(NULL
, uid
, passwd
, buffer
, bufferSize
, _result
);