Skip to content

Commit 3266df9

Browse files
committed
Loading user profile during authentication
1 parent 45809a6 commit 3266df9

File tree

2 files changed

+177
-193
lines changed

2 files changed

+177
-193
lines changed

auth-passwd.c

Lines changed: 51 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
#include "xmalloc.h"
4242
#endif
4343

44-
/*
45-
* We support only client side kerberos on Windows.
46-
*/
44+
/*
45+
* We support only client side kerberos on Windows.
46+
*/
4747

4848
#ifdef WIN32_FIXME
49-
#undef GSSAPI
50-
#undef KRB5
49+
#undef GSSAPI
50+
#undef KRB5
5151
#endif
5252

5353
#include <sys/types.h>
@@ -155,23 +155,23 @@ warn_expiry(Authctxt *authctxt, auth_session_t *as)
155155
#ifdef HAVE_LOGIN_CAP
156156
if (authctxt->valid) {
157157
pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
158-
TWO_WEEKS);
158+
TWO_WEEKS);
159159
acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
160-
TWO_WEEKS);
160+
TWO_WEEKS);
161161
}
162162
#endif
163163
if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
164164
daysleft = pwtimeleft / DAY + 1;
165165
snprintf(buf, sizeof(buf),
166-
"Your password will expire in %lld day%s.\n",
167-
daysleft, daysleft == 1 ? "" : "s");
166+
"Your password will expire in %lld day%s.\n",
167+
daysleft, daysleft == 1 ? "" : "s");
168168
buffer_append(&loginmsg, buf, strlen(buf));
169169
}
170170
if (actimeleft != 0 && actimeleft < acwarntime) {
171171
daysleft = actimeleft / DAY + 1;
172172
snprintf(buf, sizeof(buf),
173-
"Your account will expire in %lld day%s.\n",
174-
daysleft, daysleft == 1 ? "" : "s");
173+
"Your account will expire in %lld day%s.\n",
174+
daysleft, daysleft == 1 ? "" : "s");
175175
buffer_append(&loginmsg, buf, strlen(buf));
176176
}
177177
}
@@ -184,15 +184,16 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
184184
static int expire_checked = 0;
185185

186186
as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
187-
(char *)password);
187+
(char *)password);
188188
if (as == NULL)
189189
return (0);
190190
if (auth_getstate(as) & AUTH_PWEXPIRED) {
191191
auth_close(as);
192192
disable_forwarding();
193193
authctxt->force_pwchange = 1;
194194
return (1);
195-
} else {
195+
}
196+
else {
196197
if (!expire_checked) {
197198
expire_checked = 1;
198199
warn_expiry(authctxt, as);
@@ -202,183 +203,43 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
202203
}
203204

204205
#elif defined(WIN32_FIXME)
206+
extern int auth_sock;
205207
int sys_auth_passwd(Authctxt *authctxt, const char *password)
206208
{
207-
/*
208-
* Authenticate on Windows
209-
*/
210-
211-
struct passwd *pw = authctxt -> pw;
212-
213-
HANDLE hToken = INVALID_HANDLE_VALUE;
214-
215-
BOOL worked = FALSE;
216-
217-
LPWSTR user_UTF16 = NULL;
218-
LPWSTR password_UTF16 = NULL;
219-
LPWSTR domain_UTF16 = NULL;
220-
221-
int buffer_size = 0;
222-
223-
/*
224-
* Identify domain or local login.
225-
*/
226-
227-
char *username = authctxt->user;
228-
229-
char *domainslash = strchr(authctxt->user, '\\');
230-
if (domainslash) {
231-
// domain\username format
232-
char *domainname = authctxt->user;
233-
*domainslash = '\0';
234-
username = ++domainslash; // username is past the domain \ is the username
235-
236-
// Convert domainname from UTF-8 to UTF-16
237-
buffer_size = MultiByteToWideChar(CP_UTF8, 0, domainname, -1, NULL, 0);
238-
239-
if (buffer_size > 0)
240-
{
241-
domain_UTF16 = xmalloc(4 * buffer_size);
242-
}
243-
else
244-
{
245-
return 0;
246-
}
247-
248-
if (0 == MultiByteToWideChar(CP_UTF8, 0, domainname,
249-
-1, domain_UTF16, buffer_size))
250-
{
251-
free(domain_UTF16);
252-
253-
return 0;
254-
}
255-
}
256-
else if (domainslash = strchr(authctxt->user, '@')) {
257-
// username@domain format
258-
username = authctxt->user;
259-
*domainslash = '\0';
260-
char *domainname = ++domainslash; // domainname is past the user@
261-
262-
// Convert domainname from UTF-8 to UTF-16
263-
buffer_size = MultiByteToWideChar(CP_UTF8, 0, domainname, -1, NULL, 0);
264-
265-
if (buffer_size > 0)
266-
{
267-
domain_UTF16 = xmalloc(4 * buffer_size);
268-
}
269-
else
270-
{
271-
return 0;
272-
}
273-
274-
if (0 == MultiByteToWideChar(CP_UTF8, 0, domainname,
275-
-1, domain_UTF16, buffer_size))
276-
{
277-
free(domain_UTF16);
278-
279-
return 0;
280-
}
281-
}
282-
else {
283-
domain_UTF16 = strchr(authctxt->user, '@') ? NULL : L".";
284-
}
285-
286-
authctxt -> methoddata = hToken;
287-
288-
if (domain_UTF16 == NULL)
289-
{
290-
debug3("Using domain logon...");
291-
}
292-
293-
/*
294-
* Convert username from UTF-8 to UTF-16
295-
*/
296-
297-
buffer_size = MultiByteToWideChar(CP_UTF8, 0, username, -1, NULL, 0);
298-
299-
if (buffer_size > 0)
300-
{
301-
user_UTF16 = xmalloc(4 * buffer_size);
302-
}
303-
else
304-
{
305-
return 0;
306-
}
307-
308-
if (0 == MultiByteToWideChar(CP_UTF8, 0, username,
309-
-1, user_UTF16, buffer_size))
310-
{
311-
free(user_UTF16);
312-
313-
return 0;
314-
}
315-
316-
/*
317-
* Convert password from UTF-8 to UTF-16
318-
*/
319-
320-
buffer_size = MultiByteToWideChar(CP_UTF8, 0, password, -1, NULL, 0);
321-
322-
if (buffer_size > 0)
323-
{
324-
password_UTF16 = xmalloc(4 * buffer_size);
325-
}
326-
else
327-
{
328-
return 0;
329-
}
330-
331-
if (0 == MultiByteToWideChar(CP_UTF8, 0, password, -1,
332-
password_UTF16 , buffer_size))
333-
{
334-
free(password_UTF16 );
335-
336-
return 0;
337-
}
338-
339-
worked = LogonUserW(user_UTF16, domain_UTF16, password_UTF16,
340-
LOGON32_LOGON_NETWORK,
341-
LOGON32_PROVIDER_DEFAULT, &hToken);
342-
343-
344-
free(user_UTF16);
345-
free(password_UTF16);
346-
if (domainslash) free(domain_UTF16);
347-
348-
/*
349-
* If login still fails, go out.
350-
*/
351-
352-
if (!worked || hToken == INVALID_HANDLE_VALUE)
353-
{
354-
return 0;
355-
}
356-
357-
/*
358-
* Make sure this can be inherited for when
359-
* we start shells or commands.
360-
*/
361-
362-
worked = SetHandleInformation(hToken, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
363-
364-
if (!worked)
365-
{
366-
CloseHandle(hToken);
367-
368-
hToken = INVALID_HANDLE_VALUE;
369-
370-
authctxt -> methoddata = hToken;
371-
372-
return 0;
373-
}
374-
375-
/*
376-
* Save the handle (or invalid handle) as method-specific data.
377-
*/
378-
379-
authctxt -> methoddata = hToken;
380-
381-
return 1;
209+
/*
210+
* Authenticate on Windows
211+
*/
212+
213+
{
214+
u_char *blob = NULL;
215+
size_t blen = 0;
216+
DWORD token = 0;
217+
struct sshbuf *msg = NULL;
218+
219+
msg = sshbuf_new();
220+
if (!msg)
221+
return 0;
222+
if (sshbuf_put_u8(msg, 100) != 0 ||
223+
sshbuf_put_cstring(msg, "password") != 0 ||
224+
sshbuf_put_cstring(msg, authctxt->user) != 0 ||
225+
sshbuf_put_cstring(msg, password) != 0 ||
226+
ssh_request_reply(auth_sock, msg, msg) != 0 ||
227+
sshbuf_get_u32(msg, &token) != 0) {
228+
debug("auth agent did not authorize client %s", authctxt->pw->pw_name);
229+
return 0;
230+
}
231+
232+
233+
if (blob)
234+
free(blob);
235+
if (msg)
236+
sshbuf_free(msg);
237+
238+
authctxt->methoddata = token;
239+
240+
}
241+
242+
return 1;
382243
}
383244

384245
#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
@@ -397,13 +258,13 @@ sys_auth_passwd(Authctxt *authctxt, const char *password)
397258

398259
/* Encrypt the candidate password using the proper salt. */
399260
encrypted_password = xcrypt(password,
400-
(pw_password[0] && pw_password[1]) ? pw_password : "xx");
261+
(pw_password[0] && pw_password[1]) ? pw_password : "xx");
401262

402263
/*
403264
* Authentication is accepted if the encrypted passwords
404265
* are identical.
405266
*/
406267
return encrypted_password != NULL &&
407-
strcmp(encrypted_password, pw_password) == 0;
268+
strcmp(encrypted_password, pw_password) == 0;
408269
}
409270
#endif

0 commit comments

Comments
 (0)