2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
16 #include <AutoDeleter.h>
17 #include <errno_private.h>
18 #include <libroot_private.h>
19 #include <RegistrarDefs.h>
20 #include <user_group.h>
22 #include <util/KMessage.h>
25 using BPrivate::UserGroupLocker
;
26 using BPrivate::relocate_pointer
;
29 static KMessage sShadowPwdDBReply
;
30 static spwd
** sShadowPwdEntries
= NULL
;
31 static size_t sShadowPwdEntryCount
= 0;
32 static size_t sIterationIndex
= 0;
34 static struct spwd sShadowPwdBuffer
;
35 static char sShadowPwdStringBuffer
[MAX_SHADOW_PWD_BUFFER_SIZE
];
41 if (sShadowPwdEntries
!= NULL
)
45 KMessage
message(BPrivate::B_REG_GET_SHADOW_PASSWD_DB
);
46 status_t error
= BPrivate::send_authentication_request_to_registrar(message
,
55 if ((error
= sShadowPwdDBReply
.FindInt32("count", &count
)) != B_OK
56 || (error
= sShadowPwdDBReply
.FindData("entries", B_RAW_TYPE
,
57 (const void**)&entries
, &numBytes
)) != B_OK
) {
61 // relocate the entries
62 addr_t baseAddress
= (addr_t
)entries
;
63 for (int32 i
= 0; i
< count
; i
++) {
64 spwd
* entry
= relocate_pointer(baseAddress
, entries
[i
]);
65 relocate_pointer(baseAddress
, entry
->sp_namp
);
66 relocate_pointer(baseAddress
, entry
->sp_pwdp
);
69 sShadowPwdEntries
= entries
;
70 sShadowPwdEntryCount
= count
;
82 struct spwd
* result
= NULL
;
83 int status
= getspent_r(&sShadowPwdBuffer
, sShadowPwdStringBuffer
,
84 sizeof(sShadowPwdStringBuffer
), &result
);
92 getspent_r(struct spwd
* spwd
, char* buffer
, size_t bufferSize
,
93 struct spwd
** _result
)
97 int status
= B_NO_MEMORY
;
101 if ((status
= init_shadow_pwd_db()) == B_OK
) {
102 if (sIterationIndex
>= sShadowPwdEntryCount
)
105 status
= BPrivate::copy_shadow_pwd_to_buffer(
106 sShadowPwdEntries
[sIterationIndex
], spwd
, buffer
, bufferSize
);
108 if (status
== B_OK
) {
130 UserGroupLocker locker
;
132 sShadowPwdDBReply
.Unset();
133 sShadowPwdEntries
= NULL
;
134 sShadowPwdEntryCount
= 0;
140 getspnam(const char *name
)
142 struct spwd
* result
= NULL
;
143 int status
= getspnam_r(name
, &sShadowPwdBuffer
, sShadowPwdStringBuffer
,
144 sizeof(sShadowPwdStringBuffer
), &result
);
152 getspnam_r(const char *name
, struct spwd
*spwd
, char *buffer
,
153 size_t bufferSize
, struct spwd
**_result
)
157 KMessage
message(BPrivate::B_REG_GET_USER
);
158 message
.AddString("name", name
);
159 message
.AddBool("shadow", true);
162 status_t error
= BPrivate::send_authentication_request_to_registrar(message
,
167 const char* password
;
176 if ((error
= reply
.FindString("name", &name
)) != B_OK
177 || (error
= reply
.FindString("shadow password", &password
)) != B_OK
178 || (error
= reply
.FindInt32("last changed", &lastChanged
)) != B_OK
179 || (error
= reply
.FindInt32("min", &min
)) != B_OK
180 || (error
= reply
.FindInt32("max", &max
)) != B_OK
181 || (error
= reply
.FindInt32("warn", &warn
)) != B_OK
182 || (error
= reply
.FindInt32("inactive", &inactive
)) != B_OK
183 || (error
= reply
.FindInt32("expiration", &expiration
)) != B_OK
184 || (error
= reply
.FindInt32("flags", &flags
)) != B_OK
) {
188 error
= BPrivate::copy_shadow_pwd_to_buffer(name
, password
, lastChanged
,
189 min
, max
, warn
, inactive
, expiration
, flags
, spwd
, buffer
, bufferSize
);
198 sgetspent(const char* line
)
200 struct spwd
* result
= NULL
;
201 int status
= sgetspent_r(line
, &sShadowPwdBuffer
, sShadowPwdStringBuffer
,
202 sizeof(sShadowPwdStringBuffer
), &result
);
210 sgetspent_r(const char* _line
, struct spwd
*spwd
, char *buffer
,
211 size_t bufferSize
, struct spwd
** _result
)
218 // we need a mutable copy of the line
219 char* line
= strdup(_line
);
222 MemoryDeleter
_(line
);
234 status_t status
= BPrivate::parse_shadow_pwd_line(line
, name
, password
,
235 lastChanged
, min
, max
, warn
, inactive
, expiration
, flags
);
239 status
= BPrivate::copy_shadow_pwd_to_buffer(name
, password
, lastChanged
,
240 min
, max
, warn
, inactive
, expiration
, flags
, spwd
, buffer
, bufferSize
);
250 fgetspent(FILE* file
)
252 struct spwd
* result
= NULL
;
253 int status
= fgetspent_r(file
, &sShadowPwdBuffer
, sShadowPwdStringBuffer
,
254 sizeof(sShadowPwdStringBuffer
), &result
);
262 fgetspent_r(FILE* file
, struct spwd
* spwd
, char* buffer
, size_t bufferSize
,
263 struct spwd
** _result
)
268 char lineBuffer
[LINE_MAX
+ 1];
270 char* line
= fgets(lineBuffer
, sizeof(lineBuffer
), file
);
277 return sgetspent_r(line
, spwd
, buffer
, bufferSize
, _result
);