2 Unix SMB/CIFS implementation.
6 Copyright (C) Gerald (Jerry) Carter 2007
7 Copyright (C) Guenther Deschner 2008
8 Copyright (C) Volker Lendecke 2009
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Required Headers */
27 #include "libwbclient.h"
28 #include "../winbind_client.h"
30 /* Authenticate a username/password pair */
31 wbcErr
wbcAuthenticateUser(const char *username
,
34 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
35 struct wbcAuthUserParams params
;
39 params
.account_name
= username
;
40 params
.level
= WBC_AUTH_USER_LEVEL_PLAIN
;
41 params
.password
.plaintext
= password
;
43 wbc_status
= wbcAuthenticateUserEx(¶ms
, NULL
, NULL
);
44 BAIL_ON_WBC_ERROR(wbc_status
);
50 static bool sid_attr_compose(struct wbcSidWithAttr
*s
,
51 const struct wbcDomainSid
*d
,
52 uint32_t rid
, uint32_t attr
)
54 if (d
->num_auths
>= WBC_MAXSUBAUTHS
) {
58 s
->sid
.sub_auths
[s
->sid
.num_auths
++] = rid
;
63 static void wbcAuthUserInfoDestructor(void *ptr
)
65 struct wbcAuthUserInfo
*i
= (struct wbcAuthUserInfo
*)ptr
;
66 free(i
->account_name
);
67 free(i
->user_principal
);
70 free(i
->dns_domain_name
);
71 free(i
->logon_server
);
72 free(i
->logon_script
);
73 free(i
->profile_path
);
74 free(i
->home_directory
);
79 static wbcErr
wbc_create_auth_info(const struct winbindd_response
*resp
,
80 struct wbcAuthUserInfo
**_i
)
82 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
83 struct wbcAuthUserInfo
*i
;
84 struct wbcDomainSid domain_sid
;
89 i
= (struct wbcAuthUserInfo
*)wbcAllocateMemory(
90 1, sizeof(struct wbcAuthUserInfo
),
91 wbcAuthUserInfoDestructor
);
92 BAIL_ON_PTR_ERROR(i
, wbc_status
);
94 i
->user_flags
= resp
->data
.auth
.info3
.user_flgs
;
96 i
->account_name
= strdup(resp
->data
.auth
.info3
.user_name
);
97 BAIL_ON_PTR_ERROR(i
->account_name
, wbc_status
);
98 i
->user_principal
= NULL
;
99 i
->full_name
= strdup(resp
->data
.auth
.info3
.full_name
);
100 BAIL_ON_PTR_ERROR(i
->full_name
, wbc_status
);
101 i
->domain_name
= strdup(resp
->data
.auth
.info3
.logon_dom
);
102 BAIL_ON_PTR_ERROR(i
->domain_name
, wbc_status
);
103 i
->dns_domain_name
= NULL
;
105 i
->acct_flags
= resp
->data
.auth
.info3
.acct_flags
;
106 memcpy(i
->user_session_key
,
107 resp
->data
.auth
.user_session_key
,
108 sizeof(i
->user_session_key
));
109 memcpy(i
->lm_session_key
,
110 resp
->data
.auth
.first_8_lm_hash
,
111 sizeof(i
->lm_session_key
));
113 i
->logon_count
= resp
->data
.auth
.info3
.logon_count
;
114 i
->bad_password_count
= resp
->data
.auth
.info3
.bad_pw_count
;
116 i
->logon_time
= resp
->data
.auth
.info3
.logon_time
;
117 i
->logoff_time
= resp
->data
.auth
.info3
.logoff_time
;
118 i
->kickoff_time
= resp
->data
.auth
.info3
.kickoff_time
;
119 i
->pass_last_set_time
= resp
->data
.auth
.info3
.pass_last_set_time
;
120 i
->pass_can_change_time
= resp
->data
.auth
.info3
.pass_can_change_time
;
121 i
->pass_must_change_time
= resp
->data
.auth
.info3
.pass_must_change_time
;
123 i
->logon_server
= strdup(resp
->data
.auth
.info3
.logon_srv
);
124 BAIL_ON_PTR_ERROR(i
->logon_server
, wbc_status
);
125 i
->logon_script
= strdup(resp
->data
.auth
.info3
.logon_script
);
126 BAIL_ON_PTR_ERROR(i
->logon_script
, wbc_status
);
127 i
->profile_path
= strdup(resp
->data
.auth
.info3
.profile_path
);
128 BAIL_ON_PTR_ERROR(i
->profile_path
, wbc_status
);
129 i
->home_directory
= strdup(resp
->data
.auth
.info3
.home_dir
);
130 BAIL_ON_PTR_ERROR(i
->home_directory
, wbc_status
);
131 i
->home_drive
= strdup(resp
->data
.auth
.info3
.dir_drive
);
132 BAIL_ON_PTR_ERROR(i
->home_drive
, wbc_status
);
135 i
->num_sids
+= resp
->data
.auth
.info3
.num_groups
;
136 i
->num_sids
+= resp
->data
.auth
.info3
.num_other_sids
;
138 i
->sids
= (struct wbcSidWithAttr
*)calloc(
139 sizeof(struct wbcSidWithAttr
), i
->num_sids
);
140 BAIL_ON_PTR_ERROR(i
->sids
, wbc_status
);
142 wbc_status
= wbcStringToSid(resp
->data
.auth
.info3
.dom_sid
,
144 BAIL_ON_WBC_ERROR(wbc_status
);
147 if (!sid_attr_compose(&i
->sids
[sn
], &domain_sid
,
148 resp
->data
.auth
.info3
.user_rid
, 0)) {
149 wbc_status
= WBC_ERR_INVALID_SID
;
153 if (!sid_attr_compose(&i
->sids
[sn
], &domain_sid
,
154 resp
->data
.auth
.info3
.group_rid
, 0)) {
155 wbc_status
= WBC_ERR_INVALID_SID
;
160 p
= (char *)resp
->extra_data
.data
;
162 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
163 BAIL_ON_WBC_ERROR(wbc_status
);
166 for (j
=0; j
< resp
->data
.auth
.info3
.num_groups
; j
++) {
171 char *e
= strchr(p
, '\n');
173 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
174 BAIL_ON_WBC_ERROR(wbc_status
);
179 ret
= sscanf(s
, "0x%08X:0x%08X", &rid
, &attrs
);
181 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
182 BAIL_ON_WBC_ERROR(wbc_status
);
185 if (!sid_attr_compose(&i
->sids
[sn
], &domain_sid
,
187 wbc_status
= WBC_ERR_INVALID_SID
;
193 for (j
=0; j
< resp
->data
.auth
.info3
.num_other_sids
; j
++) {
198 char *e
= strchr(p
, '\n');
200 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
201 BAIL_ON_WBC_ERROR(wbc_status
);
208 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
209 BAIL_ON_WBC_ERROR(wbc_status
);
214 ret
= sscanf(a
, "0x%08X",
217 wbc_status
= WBC_ERR_INVALID_RESPONSE
;
218 BAIL_ON_WBC_ERROR(wbc_status
);
221 wbc_status
= wbcStringToSid(s
, &i
->sids
[sn
].sid
);
222 BAIL_ON_WBC_ERROR(wbc_status
);
224 i
->sids
[sn
].attributes
= attrs
;
237 static void wbcAuthErrorInfoDestructor(void *ptr
)
239 struct wbcAuthErrorInfo
*e
= (struct wbcAuthErrorInfo
*)ptr
;
241 free(e
->display_string
);
244 static wbcErr
wbc_create_error_info(const struct winbindd_response
*resp
,
245 struct wbcAuthErrorInfo
**_e
)
247 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
248 struct wbcAuthErrorInfo
*e
;
250 e
= (struct wbcAuthErrorInfo
*)wbcAllocateMemory(
251 1, sizeof(struct wbcAuthErrorInfo
),
252 wbcAuthErrorInfoDestructor
);
253 BAIL_ON_PTR_ERROR(e
, wbc_status
);
255 e
->nt_status
= resp
->data
.auth
.nt_status
;
256 e
->pam_error
= resp
->data
.auth
.pam_error
;
257 e
->nt_string
= strdup(resp
->data
.auth
.nt_status_string
);
258 BAIL_ON_PTR_ERROR(e
->nt_string
, wbc_status
);
260 e
->display_string
= strdup(resp
->data
.auth
.error_string
);
261 BAIL_ON_PTR_ERROR(e
->display_string
, wbc_status
);
271 static wbcErr
wbc_create_password_policy_info(const struct winbindd_response
*resp
,
272 struct wbcUserPasswordPolicyInfo
**_i
)
274 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
275 struct wbcUserPasswordPolicyInfo
*i
;
277 i
= (struct wbcUserPasswordPolicyInfo
*)wbcAllocateMemory(
278 1, sizeof(struct wbcUserPasswordPolicyInfo
), NULL
);
279 BAIL_ON_PTR_ERROR(i
, wbc_status
);
281 i
->min_passwordage
= resp
->data
.auth
.policy
.min_passwordage
;
282 i
->min_length_password
= resp
->data
.auth
.policy
.min_length_password
;
283 i
->password_history
= resp
->data
.auth
.policy
.password_history
;
284 i
->password_properties
= resp
->data
.auth
.policy
.password_properties
;
285 i
->expire
= resp
->data
.auth
.policy
.expire
;
295 static void wbcLogonUserInfoDestructor(void *ptr
)
297 struct wbcLogonUserInfo
*i
= (struct wbcLogonUserInfo
*)ptr
;
298 wbcFreeMemory(i
->info
);
299 wbcFreeMemory(i
->blobs
);
302 static wbcErr
wbc_create_logon_info(struct winbindd_response
*resp
,
303 struct wbcLogonUserInfo
**_i
)
305 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
306 struct wbcLogonUserInfo
*i
;
308 i
= (struct wbcLogonUserInfo
*)wbcAllocateMemory(
309 1, sizeof(struct wbcLogonUserInfo
),
310 wbcLogonUserInfoDestructor
);
311 BAIL_ON_PTR_ERROR(i
, wbc_status
);
313 wbc_status
= wbc_create_auth_info(resp
, &i
->info
);
314 BAIL_ON_WBC_ERROR(wbc_status
);
316 if (resp
->data
.auth
.krb5ccname
[0] != '\0') {
317 wbc_status
= wbcAddNamedBlob(&i
->num_blobs
,
321 (uint8_t *)resp
->data
.auth
.krb5ccname
,
322 strlen(resp
->data
.auth
.krb5ccname
)+1);
323 BAIL_ON_WBC_ERROR(wbc_status
);
326 if (resp
->data
.auth
.unix_username
[0] != '\0') {
327 wbc_status
= wbcAddNamedBlob(&i
->num_blobs
,
331 (uint8_t *)resp
->data
.auth
.unix_username
,
332 strlen(resp
->data
.auth
.unix_username
)+1);
333 BAIL_ON_WBC_ERROR(wbc_status
);
344 /* Authenticate with more detailed information */
345 wbcErr
wbcAuthenticateUserEx(const struct wbcAuthUserParams
*params
,
346 struct wbcAuthUserInfo
**info
,
347 struct wbcAuthErrorInfo
**error
)
349 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
351 struct winbindd_request request
;
352 struct winbindd_response response
;
354 ZERO_STRUCT(request
);
355 ZERO_STRUCT(response
);
362 wbc_status
= WBC_ERR_INVALID_PARAM
;
363 BAIL_ON_WBC_ERROR(wbc_status
);
366 if (params
->level
!= WBC_AUTH_USER_LEVEL_PAC
&& !params
->account_name
) {
367 wbc_status
= WBC_ERR_INVALID_PARAM
;
368 BAIL_ON_WBC_ERROR(wbc_status
);
371 /* Initialize request */
373 switch (params
->level
) {
374 case WBC_AUTH_USER_LEVEL_PLAIN
:
375 cmd
= WINBINDD_PAM_AUTH
;
376 request
.flags
= WBFLAG_PAM_INFO3_TEXT
|
377 WBFLAG_PAM_USER_SESSION_KEY
|
380 if (!params
->password
.plaintext
) {
381 wbc_status
= WBC_ERR_INVALID_PARAM
;
382 BAIL_ON_WBC_ERROR(wbc_status
);
385 if (params
->domain_name
&& params
->domain_name
[0]) {
386 /* We need to get the winbind separator :-( */
387 struct winbindd_response sep_response
;
389 ZERO_STRUCT(sep_response
);
391 wbc_status
= wbcRequestResponse(WINBINDD_INFO
,
392 NULL
, &sep_response
);
393 BAIL_ON_WBC_ERROR(wbc_status
);
395 snprintf(request
.data
.auth
.user
,
396 sizeof(request
.data
.auth
.user
)-1,
399 sep_response
.data
.info
.winbind_separator
,
400 params
->account_name
);
402 strncpy(request
.data
.auth
.user
,
403 params
->account_name
,
404 sizeof(request
.data
.auth
.user
)-1);
407 strncpy(request
.data
.auth
.pass
,
408 params
->password
.plaintext
,
409 sizeof(request
.data
.auth
.pass
)-1);
412 case WBC_AUTH_USER_LEVEL_HASH
:
413 wbc_status
= WBC_ERR_NOT_IMPLEMENTED
;
414 BAIL_ON_WBC_ERROR(wbc_status
);
417 case WBC_AUTH_USER_LEVEL_RESPONSE
:
418 cmd
= WINBINDD_PAM_AUTH_CRAP
;
419 request
.flags
= WBFLAG_PAM_INFO3_TEXT
|
420 WBFLAG_PAM_USER_SESSION_KEY
|
423 if (params
->password
.response
.lm_length
&&
424 !params
->password
.response
.lm_data
) {
425 wbc_status
= WBC_ERR_INVALID_PARAM
;
426 BAIL_ON_WBC_ERROR(wbc_status
);
428 if (params
->password
.response
.lm_length
== 0 &&
429 params
->password
.response
.lm_data
) {
430 wbc_status
= WBC_ERR_INVALID_PARAM
;
431 BAIL_ON_WBC_ERROR(wbc_status
);
434 if (params
->password
.response
.nt_length
&&
435 !params
->password
.response
.nt_data
) {
436 wbc_status
= WBC_ERR_INVALID_PARAM
;
437 BAIL_ON_WBC_ERROR(wbc_status
);
439 if (params
->password
.response
.nt_length
== 0&&
440 params
->password
.response
.nt_data
) {
441 wbc_status
= WBC_ERR_INVALID_PARAM
;
442 BAIL_ON_WBC_ERROR(wbc_status
);
445 strncpy(request
.data
.auth_crap
.user
,
446 params
->account_name
,
447 sizeof(request
.data
.auth_crap
.user
)-1);
448 if (params
->domain_name
) {
449 strncpy(request
.data
.auth_crap
.domain
,
451 sizeof(request
.data
.auth_crap
.domain
)-1);
453 if (params
->workstation_name
) {
454 strncpy(request
.data
.auth_crap
.workstation
,
455 params
->workstation_name
,
456 sizeof(request
.data
.auth_crap
.workstation
)-1);
459 request
.data
.auth_crap
.logon_parameters
=
460 params
->parameter_control
;
462 memcpy(request
.data
.auth_crap
.chal
,
463 params
->password
.response
.challenge
,
464 sizeof(request
.data
.auth_crap
.chal
));
466 request
.data
.auth_crap
.lm_resp_len
=
467 MIN(params
->password
.response
.lm_length
,
468 sizeof(request
.data
.auth_crap
.lm_resp
));
469 if (params
->password
.response
.lm_data
) {
470 memcpy(request
.data
.auth_crap
.lm_resp
,
471 params
->password
.response
.lm_data
,
472 request
.data
.auth_crap
.lm_resp_len
);
474 request
.data
.auth_crap
.nt_resp_len
= params
->password
.response
.nt_length
;
475 if (params
->password
.response
.nt_length
> sizeof(request
.data
.auth_crap
.nt_resp
)) {
476 request
.flags
|= WBFLAG_BIG_NTLMV2_BLOB
;
477 request
.extra_len
= params
->password
.response
.nt_length
;
478 request
.extra_data
.data
= (char *)malloc(
480 if (request
.extra_data
.data
== NULL
) {
481 wbc_status
= WBC_ERR_NO_MEMORY
;
482 BAIL_ON_WBC_ERROR(wbc_status
);
484 memcpy(request
.extra_data
.data
,
485 params
->password
.response
.nt_data
,
486 request
.data
.auth_crap
.nt_resp_len
);
487 } else if (params
->password
.response
.nt_data
) {
488 memcpy(request
.data
.auth_crap
.nt_resp
,
489 params
->password
.response
.nt_data
,
490 request
.data
.auth_crap
.nt_resp_len
);
494 case WBC_AUTH_USER_LEVEL_PAC
:
495 cmd
= WINBINDD_PAM_AUTH_CRAP
;
496 request
.flags
= WBFLAG_PAM_AUTH_PAC
| WBFLAG_PAM_INFO3_TEXT
;
497 request
.extra_data
.data
= malloc(params
->password
.pac
.length
);
498 if (request
.extra_data
.data
== NULL
) {
499 wbc_status
= WBC_ERR_NO_MEMORY
;
500 BAIL_ON_WBC_ERROR(wbc_status
);
502 memcpy(request
.extra_data
.data
, params
->password
.pac
.data
,
503 params
->password
.pac
.length
);
504 request
.extra_len
= params
->password
.pac
.length
;
512 wbc_status
= WBC_ERR_INVALID_PARAM
;
513 BAIL_ON_WBC_ERROR(wbc_status
);
517 request
.flags
|= params
->flags
;
520 if (cmd
== WINBINDD_PAM_AUTH_CRAP
) {
521 wbc_status
= wbcRequestResponsePriv(cmd
, &request
, &response
);
523 wbc_status
= wbcRequestResponse(cmd
, &request
, &response
);
525 if (response
.data
.auth
.nt_status
!= 0) {
527 wbc_status
= wbc_create_error_info(&response
,
529 BAIL_ON_WBC_ERROR(wbc_status
);
532 wbc_status
= WBC_ERR_AUTH_ERROR
;
533 BAIL_ON_WBC_ERROR(wbc_status
);
535 BAIL_ON_WBC_ERROR(wbc_status
);
538 wbc_status
= wbc_create_auth_info(&response
, info
);
539 BAIL_ON_WBC_ERROR(wbc_status
);
543 winbindd_free_response(&response
);
545 free(request
.extra_data
.data
);
550 /* Trigger a verification of the trust credentials of a specific domain */
551 wbcErr
wbcCheckTrustCredentials(const char *domain
,
552 struct wbcAuthErrorInfo
**error
)
554 struct winbindd_request request
;
555 struct winbindd_response response
;
556 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
558 ZERO_STRUCT(request
);
559 ZERO_STRUCT(response
);
562 strncpy(request
.domain_name
, domain
,
563 sizeof(request
.domain_name
)-1);
568 wbc_status
= wbcRequestResponsePriv(WINBINDD_CHECK_MACHACC
,
569 &request
, &response
);
570 if (response
.data
.auth
.nt_status
!= 0) {
572 wbc_status
= wbc_create_error_info(&response
,
574 BAIL_ON_WBC_ERROR(wbc_status
);
577 wbc_status
= WBC_ERR_AUTH_ERROR
;
578 BAIL_ON_WBC_ERROR(wbc_status
);
580 BAIL_ON_WBC_ERROR(wbc_status
);
586 /* Trigger a change of the trust credentials for a specific domain */
587 wbcErr
wbcChangeTrustCredentials(const char *domain
,
588 struct wbcAuthErrorInfo
**error
)
590 struct winbindd_request request
;
591 struct winbindd_response response
;
592 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
594 ZERO_STRUCT(request
);
595 ZERO_STRUCT(response
);
598 strncpy(request
.domain_name
, domain
,
599 sizeof(request
.domain_name
)-1);
604 wbc_status
= wbcRequestResponsePriv(WINBINDD_CHANGE_MACHACC
,
605 &request
, &response
);
606 if (response
.data
.auth
.nt_status
!= 0) {
608 wbc_status
= wbc_create_error_info(&response
,
610 BAIL_ON_WBC_ERROR(wbc_status
);
613 wbc_status
= WBC_ERR_AUTH_ERROR
;
614 BAIL_ON_WBC_ERROR(wbc_status
);
616 BAIL_ON_WBC_ERROR(wbc_status
);
623 * Trigger a no-op NETLOGON call. Lightweight version of
624 * wbcCheckTrustCredentials
626 wbcErr
wbcPingDc(const char *domain
, struct wbcAuthErrorInfo
**error
)
628 return wbcPingDc2(domain
, error
, NULL
);
632 * Trigger a no-op NETLOGON call. Lightweight version of
633 * wbcCheckTrustCredentials, optionally return attempted DC
635 wbcErr
wbcPingDc2(const char *domain
, struct wbcAuthErrorInfo
**error
,
638 struct winbindd_request request
;
639 struct winbindd_response response
;
640 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
642 ZERO_STRUCT(request
);
643 ZERO_STRUCT(response
);
646 strncpy(request
.domain_name
, domain
,
647 sizeof(request
.domain_name
)-1);
652 wbc_status
= wbcRequestResponse(WINBINDD_PING_DC
,
656 if (dcname
&& response
.extra_data
.data
) {
659 len
= response
.length
- sizeof(struct winbindd_response
);
660 *dcname
= wbcAllocateMemory(1, len
, NULL
);
661 BAIL_ON_PTR_ERROR(*dcname
, wbc_status
);
663 strlcpy(*dcname
, response
.extra_data
.data
, len
);
666 if (response
.data
.auth
.nt_status
!= 0) {
668 wbc_status
= wbc_create_error_info(&response
,
670 BAIL_ON_WBC_ERROR(wbc_status
);
673 wbc_status
= WBC_ERR_AUTH_ERROR
;
674 BAIL_ON_WBC_ERROR(wbc_status
);
676 BAIL_ON_WBC_ERROR(wbc_status
);
682 /* Trigger an extended logoff notification to Winbind for a specific user */
683 wbcErr
wbcLogoffUserEx(const struct wbcLogoffUserParams
*params
,
684 struct wbcAuthErrorInfo
**error
)
686 struct winbindd_request request
;
687 struct winbindd_response response
;
688 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
693 if (!params
|| !params
->username
) {
694 wbc_status
= WBC_ERR_INVALID_PARAM
;
695 BAIL_ON_WBC_ERROR(wbc_status
);
698 if ((params
->num_blobs
> 0) && (params
->blobs
== NULL
)) {
699 wbc_status
= WBC_ERR_INVALID_PARAM
;
700 BAIL_ON_WBC_ERROR(wbc_status
);
702 if ((params
->num_blobs
== 0) && (params
->blobs
!= NULL
)) {
703 wbc_status
= WBC_ERR_INVALID_PARAM
;
704 BAIL_ON_WBC_ERROR(wbc_status
);
707 ZERO_STRUCT(request
);
708 ZERO_STRUCT(response
);
710 strncpy(request
.data
.logoff
.user
, params
->username
,
711 sizeof(request
.data
.logoff
.user
)-1);
713 for (i
=0; i
<params
->num_blobs
; i
++) {
715 if (strcasecmp(params
->blobs
[i
].name
, "ccfilename") == 0) {
716 if (params
->blobs
[i
].blob
.data
) {
717 strncpy(request
.data
.logoff
.krb5ccname
,
718 (const char *)params
->blobs
[i
].blob
.data
,
719 sizeof(request
.data
.logoff
.krb5ccname
) - 1);
724 if (strcasecmp(params
->blobs
[i
].name
, "user_uid") == 0) {
725 if (params
->blobs
[i
].blob
.data
) {
726 memcpy(&request
.data
.logoff
.uid
,
727 params
->blobs
[i
].blob
.data
,
728 MIN(params
->blobs
[i
].blob
.length
,
729 sizeof(request
.data
.logoff
.uid
)));
734 if (strcasecmp(params
->blobs
[i
].name
, "flags") == 0) {
735 if (params
->blobs
[i
].blob
.data
) {
736 memcpy(&request
.flags
,
737 params
->blobs
[i
].blob
.data
,
738 MIN(params
->blobs
[i
].blob
.length
,
739 sizeof(request
.flags
)));
747 wbc_status
= wbcRequestResponse(WINBINDD_PAM_LOGOFF
,
751 /* Take the response above and return it to the caller */
752 if (response
.data
.auth
.nt_status
!= 0) {
754 wbc_status
= wbc_create_error_info(&response
,
756 BAIL_ON_WBC_ERROR(wbc_status
);
759 wbc_status
= WBC_ERR_AUTH_ERROR
;
760 BAIL_ON_WBC_ERROR(wbc_status
);
762 BAIL_ON_WBC_ERROR(wbc_status
);
768 /* Trigger a logoff notification to Winbind for a specific user */
769 wbcErr
wbcLogoffUser(const char *username
,
771 const char *ccfilename
)
773 struct winbindd_request request
;
774 struct winbindd_response response
;
775 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
780 wbc_status
= WBC_ERR_INVALID_PARAM
;
781 BAIL_ON_WBC_ERROR(wbc_status
);
784 ZERO_STRUCT(request
);
785 ZERO_STRUCT(response
);
787 strncpy(request
.data
.logoff
.user
, username
,
788 sizeof(request
.data
.logoff
.user
)-1);
789 request
.data
.logoff
.uid
= uid
;
792 strncpy(request
.data
.logoff
.krb5ccname
, ccfilename
,
793 sizeof(request
.data
.logoff
.krb5ccname
)-1);
798 wbc_status
= wbcRequestResponse(WINBINDD_PAM_LOGOFF
,
802 /* Take the response above and return it to the caller */
808 /* Change a password for a user with more detailed information upon failure */
809 wbcErr
wbcChangeUserPasswordEx(const struct wbcChangePasswordParams
*params
,
810 struct wbcAuthErrorInfo
**error
,
811 enum wbcPasswordChangeRejectReason
*reject_reason
,
812 struct wbcUserPasswordPolicyInfo
**policy
)
814 struct winbindd_request request
;
815 struct winbindd_response response
;
816 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
821 if (!params
->account_name
) {
822 wbc_status
= WBC_ERR_INVALID_PARAM
;
838 ZERO_STRUCT(request
);
839 ZERO_STRUCT(response
);
841 switch (params
->level
) {
842 case WBC_CHANGE_PASSWORD_LEVEL_PLAIN
:
843 cmd
= WINBINDD_PAM_CHAUTHTOK
;
845 if (!params
->account_name
) {
846 wbc_status
= WBC_ERR_INVALID_PARAM
;
850 strncpy(request
.data
.chauthtok
.user
, params
->account_name
,
851 sizeof(request
.data
.chauthtok
.user
) - 1);
853 if (params
->old_password
.plaintext
) {
854 strncpy(request
.data
.chauthtok
.oldpass
,
855 params
->old_password
.plaintext
,
856 sizeof(request
.data
.chauthtok
.oldpass
) - 1);
859 if (params
->new_password
.plaintext
) {
860 strncpy(request
.data
.chauthtok
.newpass
,
861 params
->new_password
.plaintext
,
862 sizeof(request
.data
.chauthtok
.newpass
) - 1);
866 case WBC_CHANGE_PASSWORD_LEVEL_RESPONSE
:
867 cmd
= WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
;
869 if (!params
->account_name
|| !params
->domain_name
) {
870 wbc_status
= WBC_ERR_INVALID_PARAM
;
874 if (params
->old_password
.response
.old_lm_hash_enc_length
&&
875 !params
->old_password
.response
.old_lm_hash_enc_data
) {
876 wbc_status
= WBC_ERR_INVALID_PARAM
;
880 if (params
->old_password
.response
.old_lm_hash_enc_length
== 0 &&
881 params
->old_password
.response
.old_lm_hash_enc_data
) {
882 wbc_status
= WBC_ERR_INVALID_PARAM
;
886 if (params
->old_password
.response
.old_nt_hash_enc_length
&&
887 !params
->old_password
.response
.old_nt_hash_enc_data
) {
888 wbc_status
= WBC_ERR_INVALID_PARAM
;
892 if (params
->old_password
.response
.old_nt_hash_enc_length
== 0 &&
893 params
->old_password
.response
.old_nt_hash_enc_data
) {
894 wbc_status
= WBC_ERR_INVALID_PARAM
;
898 if (params
->new_password
.response
.lm_length
&&
899 !params
->new_password
.response
.lm_data
) {
900 wbc_status
= WBC_ERR_INVALID_PARAM
;
904 if (params
->new_password
.response
.lm_length
== 0 &&
905 params
->new_password
.response
.lm_data
) {
906 wbc_status
= WBC_ERR_INVALID_PARAM
;
910 if (params
->new_password
.response
.nt_length
&&
911 !params
->new_password
.response
.nt_data
) {
912 wbc_status
= WBC_ERR_INVALID_PARAM
;
916 if (params
->new_password
.response
.nt_length
== 0 &&
917 params
->new_password
.response
.nt_data
) {
918 wbc_status
= WBC_ERR_INVALID_PARAM
;
922 strncpy(request
.data
.chng_pswd_auth_crap
.user
,
923 params
->account_name
,
924 sizeof(request
.data
.chng_pswd_auth_crap
.user
) - 1);
926 strncpy(request
.data
.chng_pswd_auth_crap
.domain
,
928 sizeof(request
.data
.chng_pswd_auth_crap
.domain
) - 1);
930 if (params
->new_password
.response
.nt_data
) {
931 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
=
932 params
->new_password
.response
.nt_length
;
933 memcpy(request
.data
.chng_pswd_auth_crap
.new_nt_pswd
,
934 params
->new_password
.response
.nt_data
,
935 request
.data
.chng_pswd_auth_crap
.new_nt_pswd_len
);
938 if (params
->new_password
.response
.lm_data
) {
939 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
=
940 params
->new_password
.response
.lm_length
;
941 memcpy(request
.data
.chng_pswd_auth_crap
.new_lm_pswd
,
942 params
->new_password
.response
.lm_data
,
943 request
.data
.chng_pswd_auth_crap
.new_lm_pswd_len
);
946 if (params
->old_password
.response
.old_nt_hash_enc_data
) {
947 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
=
948 params
->old_password
.response
.old_nt_hash_enc_length
;
949 memcpy(request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc
,
950 params
->old_password
.response
.old_nt_hash_enc_data
,
951 request
.data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
);
954 if (params
->old_password
.response
.old_lm_hash_enc_data
) {
955 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
=
956 params
->old_password
.response
.old_lm_hash_enc_length
;
957 memcpy(request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc
,
958 params
->old_password
.response
.old_lm_hash_enc_data
,
959 request
.data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
);
964 wbc_status
= WBC_ERR_INVALID_PARAM
;
971 wbc_status
= wbcRequestResponse(cmd
,
974 if (WBC_ERROR_IS_OK(wbc_status
)) {
978 /* Take the response above and return it to the caller */
980 if (response
.data
.auth
.nt_status
!= 0) {
982 wbc_status
= wbc_create_error_info(&response
,
984 BAIL_ON_WBC_ERROR(wbc_status
);
990 wbc_status
= wbc_create_password_policy_info(&response
,
992 BAIL_ON_WBC_ERROR(wbc_status
);
996 *reject_reason
= response
.data
.auth
.reject_reason
;
999 wbc_status
= WBC_ERR_PWD_CHANGE_FAILED
;
1000 BAIL_ON_WBC_ERROR(wbc_status
);
1006 /* Change a password for a user */
1007 wbcErr
wbcChangeUserPassword(const char *username
,
1008 const char *old_password
,
1009 const char *new_password
)
1011 wbcErr wbc_status
= WBC_ERR_SUCCESS
;
1012 struct wbcChangePasswordParams params
;
1014 ZERO_STRUCT(params
);
1016 params
.account_name
= username
;
1017 params
.level
= WBC_CHANGE_PASSWORD_LEVEL_PLAIN
;
1018 params
.old_password
.plaintext
= old_password
;
1019 params
.new_password
.plaintext
= new_password
;
1021 wbc_status
= wbcChangeUserPasswordEx(¶ms
,
1025 BAIL_ON_WBC_ERROR(wbc_status
);
1032 wbcErr
wbcLogonUser(const struct wbcLogonUserParams
*params
,
1033 struct wbcLogonUserInfo
**info
,
1034 struct wbcAuthErrorInfo
**error
,
1035 struct wbcUserPasswordPolicyInfo
**policy
)
1037 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
1038 struct winbindd_request request
;
1039 struct winbindd_response response
;
1042 ZERO_STRUCT(request
);
1043 ZERO_STRUCT(response
);
1056 wbc_status
= WBC_ERR_INVALID_PARAM
;
1057 BAIL_ON_WBC_ERROR(wbc_status
);
1060 if (!params
->username
) {
1061 wbc_status
= WBC_ERR_INVALID_PARAM
;
1062 BAIL_ON_WBC_ERROR(wbc_status
);
1065 if ((params
->num_blobs
> 0) && (params
->blobs
== NULL
)) {
1066 wbc_status
= WBC_ERR_INVALID_PARAM
;
1067 BAIL_ON_WBC_ERROR(wbc_status
);
1069 if ((params
->num_blobs
== 0) && (params
->blobs
!= NULL
)) {
1070 wbc_status
= WBC_ERR_INVALID_PARAM
;
1071 BAIL_ON_WBC_ERROR(wbc_status
);
1074 /* Initialize request */
1076 request
.flags
= WBFLAG_PAM_INFO3_TEXT
|
1077 WBFLAG_PAM_USER_SESSION_KEY
|
1080 if (!params
->password
) {
1081 wbc_status
= WBC_ERR_INVALID_PARAM
;
1082 BAIL_ON_WBC_ERROR(wbc_status
);
1085 strncpy(request
.data
.auth
.user
,
1087 sizeof(request
.data
.auth
.user
)-1);
1089 strncpy(request
.data
.auth
.pass
,
1091 sizeof(request
.data
.auth
.pass
)-1);
1093 for (i
=0; i
<params
->num_blobs
; i
++) {
1095 if (strcasecmp(params
->blobs
[i
].name
, "krb5_cc_type") == 0) {
1096 if (params
->blobs
[i
].blob
.data
) {
1097 strncpy(request
.data
.auth
.krb5_cc_type
,
1098 (const char *)params
->blobs
[i
].blob
.data
,
1099 sizeof(request
.data
.auth
.krb5_cc_type
) - 1);
1104 if (strcasecmp(params
->blobs
[i
].name
, "user_uid") == 0) {
1105 if (params
->blobs
[i
].blob
.data
) {
1106 memcpy(&request
.data
.auth
.uid
,
1107 params
->blobs
[i
].blob
.data
,
1108 MIN(sizeof(request
.data
.auth
.uid
),
1109 params
->blobs
[i
].blob
.length
));
1114 if (strcasecmp(params
->blobs
[i
].name
, "flags") == 0) {
1115 if (params
->blobs
[i
].blob
.data
) {
1118 params
->blobs
[i
].blob
.data
,
1120 params
->blobs
[i
].blob
.length
));
1121 request
.flags
|= flags
;
1126 if (strcasecmp(params
->blobs
[i
].name
, "membership_of") == 0) {
1127 if (params
->blobs
[i
].blob
.data
&&
1128 params
->blobs
[i
].blob
.data
[0] > 0) {
1129 strncpy(request
.data
.auth
.require_membership_of_sid
,
1130 (const char *)params
->blobs
[i
].blob
.data
,
1131 sizeof(request
.data
.auth
.require_membership_of_sid
) - 1);
1137 wbc_status
= wbcRequestResponse(WINBINDD_PAM_AUTH
,
1141 if (response
.data
.auth
.nt_status
!= 0) {
1143 wbc_status
= wbc_create_error_info(&response
,
1145 BAIL_ON_WBC_ERROR(wbc_status
);
1148 wbc_status
= WBC_ERR_AUTH_ERROR
;
1149 BAIL_ON_WBC_ERROR(wbc_status
);
1151 BAIL_ON_WBC_ERROR(wbc_status
);
1154 wbc_status
= wbc_create_logon_info(&response
,
1156 BAIL_ON_WBC_ERROR(wbc_status
);
1160 wbc_status
= wbc_create_password_policy_info(&response
,
1162 BAIL_ON_WBC_ERROR(wbc_status
);
1166 winbindd_free_response(&response
);
1171 static void wbcCredentialCacheInfoDestructor(void *ptr
)
1173 struct wbcCredentialCacheInfo
*i
=
1174 (struct wbcCredentialCacheInfo
*)ptr
;
1175 wbcFreeMemory(i
->blobs
);
1178 /* Authenticate a user with cached credentials */
1179 wbcErr
wbcCredentialCache(struct wbcCredentialCacheParams
*params
,
1180 struct wbcCredentialCacheInfo
**info
,
1181 struct wbcAuthErrorInfo
**error
)
1183 wbcErr status
= WBC_ERR_UNKNOWN_FAILURE
;
1184 struct wbcCredentialCacheInfo
*result
= NULL
;
1185 struct winbindd_request request
;
1186 struct winbindd_response response
;
1187 struct wbcNamedBlob
*initial_blob
= NULL
;
1188 struct wbcNamedBlob
*challenge_blob
= NULL
;
1191 ZERO_STRUCT(request
);
1192 ZERO_STRUCT(response
);
1196 if (error
!= NULL
) {
1199 if ((params
== NULL
)
1200 || (params
->account_name
== NULL
)
1201 || (params
->level
!= WBC_CREDENTIAL_CACHE_LEVEL_NTLMSSP
)) {
1202 status
= WBC_ERR_INVALID_PARAM
;
1206 for (i
=0; i
<params
->num_blobs
; i
++) {
1207 if (strcasecmp(params
->blobs
[i
].name
, "initial_blob") == 0) {
1208 if (initial_blob
!= NULL
) {
1209 status
= WBC_ERR_INVALID_PARAM
;
1212 initial_blob
= ¶ms
->blobs
[i
];
1215 if (strcasecmp(params
->blobs
[i
].name
, "challenge_blob") == 0) {
1216 if (challenge_blob
!= NULL
) {
1217 status
= WBC_ERR_INVALID_PARAM
;
1220 challenge_blob
= ¶ms
->blobs
[i
];
1225 if (params
->domain_name
!= NULL
) {
1226 status
= wbcRequestResponse(WINBINDD_INFO
, NULL
, &response
);
1227 if (!WBC_ERROR_IS_OK(status
)) {
1230 snprintf(request
.data
.ccache_ntlm_auth
.user
,
1231 sizeof(request
.data
.ccache_ntlm_auth
.user
)-1,
1232 "%s%c%s", params
->domain_name
,
1233 response
.data
.info
.winbind_separator
,
1234 params
->account_name
);
1236 strncpy(request
.data
.ccache_ntlm_auth
.user
,
1237 params
->account_name
,
1238 sizeof(request
.data
.ccache_ntlm_auth
.user
)-1);
1240 request
.data
.ccache_ntlm_auth
.uid
= getuid();
1242 request
.data
.ccache_ntlm_auth
.initial_blob_len
= 0;
1243 request
.data
.ccache_ntlm_auth
.challenge_blob_len
= 0;
1244 request
.extra_len
= 0;
1246 if (initial_blob
!= NULL
) {
1247 request
.data
.ccache_ntlm_auth
.initial_blob_len
=
1248 initial_blob
->blob
.length
;
1249 request
.extra_len
+= initial_blob
->blob
.length
;
1251 if (challenge_blob
!= NULL
) {
1252 request
.data
.ccache_ntlm_auth
.challenge_blob_len
=
1253 challenge_blob
->blob
.length
;
1254 request
.extra_len
+= challenge_blob
->blob
.length
;
1257 if (request
.extra_len
!= 0) {
1258 request
.extra_data
.data
= (char *)malloc(request
.extra_len
);
1259 if (request
.extra_data
.data
== NULL
) {
1260 status
= WBC_ERR_NO_MEMORY
;
1264 if (initial_blob
!= NULL
) {
1265 memcpy(request
.extra_data
.data
,
1266 initial_blob
->blob
.data
, initial_blob
->blob
.length
);
1268 if (challenge_blob
!= NULL
) {
1269 memcpy(request
.extra_data
.data
1270 + request
.data
.ccache_ntlm_auth
.initial_blob_len
,
1271 challenge_blob
->blob
.data
,
1272 challenge_blob
->blob
.length
);
1275 status
= wbcRequestResponse(WINBINDD_CCACHE_NTLMAUTH
, &request
,
1277 if (!WBC_ERROR_IS_OK(status
)) {
1281 result
= (struct wbcCredentialCacheInfo
*)wbcAllocateMemory(
1282 1, sizeof(struct wbcCredentialCacheInfo
),
1283 wbcCredentialCacheInfoDestructor
);
1284 if (result
== NULL
) {
1285 status
= WBC_ERR_NO_MEMORY
;
1288 result
->num_blobs
= 0;
1289 result
->blobs
= NULL
;
1290 status
= wbcAddNamedBlob(&result
->num_blobs
, &result
->blobs
,
1292 (uint8_t *)response
.extra_data
.data
,
1293 response
.data
.ccache_ntlm_auth
.auth_blob_len
);
1294 if (!WBC_ERROR_IS_OK(status
)) {
1297 status
= wbcAddNamedBlob(
1298 &result
->num_blobs
, &result
->blobs
, "session_key", 0,
1299 response
.data
.ccache_ntlm_auth
.session_key
,
1300 sizeof(response
.data
.ccache_ntlm_auth
.session_key
));
1301 if (!WBC_ERROR_IS_OK(status
)) {
1307 status
= WBC_ERR_SUCCESS
;
1309 free(request
.extra_data
.data
);
1310 winbindd_free_response(&response
);
1311 wbcFreeMemory(result
);
1315 /* Authenticate a user with cached credentials */
1316 wbcErr
wbcCredentialSave(const char *user
, const char *password
)
1318 struct winbindd_request request
;
1319 struct winbindd_response response
;
1321 ZERO_STRUCT(request
);
1322 ZERO_STRUCT(response
);
1324 strncpy(request
.data
.ccache_save
.user
, user
,
1325 sizeof(request
.data
.ccache_save
.user
)-1);
1326 strncpy(request
.data
.ccache_save
.pass
, password
,
1327 sizeof(request
.data
.ccache_save
.pass
)-1);
1328 request
.data
.ccache_save
.uid
= getuid();
1330 return wbcRequestResponse(WINBINDD_CCACHE_SAVE
, &request
, &response
);