forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_winbind.c
3392 lines (2870 loc) · 79.3 KB
/
pam_winbind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pam_winbind module
Copyright Andrew Tridgell <[email protected]> 2000
Copyright Tim Potter <[email protected]> 2000
Copyright Andrew Bartlett <[email protected]> 2002
Copyright Guenther Deschner <[email protected]> 2005-2008
largely based on pam_userdb by Cristian Gafton <[email protected]> also
contains large slabs of code from pam_unix by Elliot Lee
<[email protected]> (see copyright below for full details)
*/
#include "pam_winbind.h"
enum pam_winbind_request_type
{
PAM_WINBIND_AUTHENTICATE,
PAM_WINBIND_SETCRED,
PAM_WINBIND_ACCT_MGMT,
PAM_WINBIND_OPEN_SESSION,
PAM_WINBIND_CLOSE_SESSION,
PAM_WINBIND_CHAUTHTOK,
PAM_WINBIND_CLEANUP
};
static int wbc_error_to_pam_error(wbcErr status)
{
switch (status) {
case WBC_ERR_SUCCESS:
return PAM_SUCCESS;
case WBC_ERR_NOT_IMPLEMENTED:
return PAM_SERVICE_ERR;
case WBC_ERR_UNKNOWN_FAILURE:
break;
case WBC_ERR_NO_MEMORY:
return PAM_BUF_ERR;
case WBC_ERR_INVALID_SID:
case WBC_ERR_INVALID_PARAM:
break;
case WBC_ERR_WINBIND_NOT_AVAILABLE:
return PAM_AUTHINFO_UNAVAIL;
case WBC_ERR_DOMAIN_NOT_FOUND:
return PAM_AUTHINFO_UNAVAIL;
case WBC_ERR_INVALID_RESPONSE:
return PAM_BUF_ERR;
case WBC_ERR_NSS_ERROR:
return PAM_USER_UNKNOWN;
case WBC_ERR_AUTH_ERROR:
return PAM_AUTH_ERR;
case WBC_ERR_UNKNOWN_USER:
return PAM_USER_UNKNOWN;
case WBC_ERR_UNKNOWN_GROUP:
return PAM_USER_UNKNOWN;
case WBC_ERR_PWD_CHANGE_FAILED:
break;
}
/* be paranoid */
return PAM_AUTH_ERR;
}
static const char *_pam_error_code_str(int err)
{
switch (err) {
case PAM_SUCCESS:
return "PAM_SUCCESS";
case PAM_OPEN_ERR:
return "PAM_OPEN_ERR";
case PAM_SYMBOL_ERR:
return "PAM_SYMBOL_ERR";
case PAM_SERVICE_ERR:
return "PAM_SERVICE_ERR";
case PAM_SYSTEM_ERR:
return "PAM_SYSTEM_ERR";
case PAM_BUF_ERR:
return "PAM_BUF_ERR";
case PAM_PERM_DENIED:
return "PAM_PERM_DENIED";
case PAM_AUTH_ERR:
return "PAM_AUTH_ERR";
case PAM_CRED_INSUFFICIENT:
return "PAM_CRED_INSUFFICIENT";
case PAM_AUTHINFO_UNAVAIL:
return "PAM_AUTHINFO_UNAVAIL";
case PAM_USER_UNKNOWN:
return "PAM_USER_UNKNOWN";
case PAM_MAXTRIES:
return "PAM_MAXTRIES";
case PAM_NEW_AUTHTOK_REQD:
return "PAM_NEW_AUTHTOK_REQD";
case PAM_ACCT_EXPIRED:
return "PAM_ACCT_EXPIRED";
case PAM_SESSION_ERR:
return "PAM_SESSION_ERR";
case PAM_CRED_UNAVAIL:
return "PAM_CRED_UNAVAIL";
case PAM_CRED_EXPIRED:
return "PAM_CRED_EXPIRED";
case PAM_CRED_ERR:
return "PAM_CRED_ERR";
case PAM_NO_MODULE_DATA:
return "PAM_NO_MODULE_DATA";
case PAM_CONV_ERR:
return "PAM_CONV_ERR";
case PAM_AUTHTOK_ERR:
return "PAM_AUTHTOK_ERR";
case PAM_AUTHTOK_RECOVER_ERR:
return "PAM_AUTHTOK_RECOVER_ERR";
case PAM_AUTHTOK_LOCK_BUSY:
return "PAM_AUTHTOK_LOCK_BUSY";
case PAM_AUTHTOK_DISABLE_AGING:
return "PAM_AUTHTOK_DISABLE_AGING";
case PAM_TRY_AGAIN:
return "PAM_TRY_AGAIN";
case PAM_IGNORE:
return "PAM_IGNORE";
case PAM_ABORT:
return "PAM_ABORT";
case PAM_AUTHTOK_EXPIRED:
return "PAM_AUTHTOK_EXPIRED";
#ifdef PAM_MODULE_UNKNOWN
case PAM_MODULE_UNKNOWN:
return "PAM_MODULE_UNKNOWN";
#endif
#ifdef PAM_BAD_ITEM
case PAM_BAD_ITEM:
return "PAM_BAD_ITEM";
#endif
#ifdef PAM_CONV_AGAIN
case PAM_CONV_AGAIN:
return "PAM_CONV_AGAIN";
#endif
#ifdef PAM_INCOMPLETE
case PAM_INCOMPLETE:
return "PAM_INCOMPLETE";
#endif
default:
return NULL;
}
}
#define _PAM_LOG_FUNCTION_ENTER(function, ctx) \
do { \
_pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] ENTER: " \
function " (flags: 0x%04x)", ctx->pamh, ctx->flags); \
_pam_log_state(ctx); \
} while (0)
#define _PAM_LOG_FUNCTION_LEAVE(function, ctx, retval) \
do { \
_pam_log_debug(ctx, LOG_DEBUG, "[pamh: %p] LEAVE: " \
function " returning %d (%s)", ctx ? ctx->pamh : NULL, retval, \
_pam_error_code_str(retval)); \
_pam_log_state(ctx); \
} while (0)
/* data tokens */
#define MAX_PASSWD_TRIES 3
#ifdef HAVE_GETTEXT
static char initialized = 0;
static inline void textdomain_init(void);
static inline void textdomain_init(void)
{
if (!initialized) {
bindtextdomain(MODULE_NAME, LOCALEDIR);
initialized = 1;
}
return;
}
#endif
/* some syslogging */
static void _pam_log_int(const pam_handle_t *pamh,
int err,
const char *format,
va_list args) PRINTF_ATTRIBUTE(3, 0);
#ifdef HAVE_PAM_VSYSLOG
static void _pam_log_int(const pam_handle_t *pamh,
int err,
const char *format,
va_list args)
{
pam_vsyslog(pamh, err, format, args);
}
#else
static void _pam_log_int(const pam_handle_t *pamh,
int err,
const char *format,
va_list args)
{
char *base = NULL;
va_list args2;
const char *service;
int ret;
va_copy(args2, args);
pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
ret = vasprintf(&base, format, args);
if (ret == -1) {
/* what else todo ? */
vsyslog(err, format, args2);
va_end(args2);
return;
}
syslog(err, "%s(%s): %s", MODULE_NAME, service, base);
SAFE_FREE(base);
va_end(args2);
}
#endif /* HAVE_PAM_VSYSLOG */
static bool _pam_log_is_silent(int ctrl)
{
return on(ctrl, WINBIND_SILENT);
}
static void _pam_log(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
static void _pam_log(struct pwb_context *r, int err, const char *format, ...)
{
va_list args;
if (_pam_log_is_silent(r->ctrl)) {
return;
}
va_start(args, format);
_pam_log_int(r->pamh, err, format, args);
va_end(args);
}
static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
static void __pam_log(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
{
va_list args;
if (_pam_log_is_silent(ctrl)) {
return;
}
va_start(args, format);
_pam_log_int(pamh, err, format, args);
va_end(args);
}
static bool _pam_log_is_debug_enabled(int ctrl)
{
if (ctrl == -1) {
return false;
}
if (_pam_log_is_silent(ctrl)) {
return false;
}
if (!(ctrl & WINBIND_DEBUG_ARG)) {
return false;
}
return true;
}
static bool _pam_log_is_debug_state_enabled(int ctrl)
{
if (!(ctrl & WINBIND_DEBUG_STATE)) {
return false;
}
return _pam_log_is_debug_enabled(ctrl);
}
static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
static void _pam_log_debug(struct pwb_context *r, int err, const char *format, ...)
{
va_list args;
if (!r || !_pam_log_is_debug_enabled(r->ctrl)) {
return;
}
va_start(args, format);
_pam_log_int(r->pamh, err, format, args);
va_end(args);
}
static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
static void __pam_log_debug(const pam_handle_t *pamh, int ctrl, int err, const char *format, ...)
{
va_list args;
if (!_pam_log_is_debug_enabled(ctrl)) {
return;
}
va_start(args, format);
_pam_log_int(pamh, err, format, args);
va_end(args);
}
static void _pam_log_state_datum(struct pwb_context *ctx,
int item_type,
const char *key,
int is_string)
{
const void *data = NULL;
if (item_type != 0) {
pam_get_item(ctx->pamh, item_type, &data);
} else {
pam_get_data(ctx->pamh, key, &data);
}
if (data != NULL) {
const char *type = (item_type != 0) ? "ITEM" : "DATA";
if (is_string != 0) {
_pam_log_debug(ctx, LOG_DEBUG,
"[pamh: %p] STATE: %s(%s) = \"%s\" (%p)",
ctx->pamh, type, key, (const char *)data,
data);
} else {
_pam_log_debug(ctx, LOG_DEBUG,
"[pamh: %p] STATE: %s(%s) = %p",
ctx->pamh, type, key, data);
}
}
}
#define _PAM_LOG_STATE_DATA_POINTER(ctx, module_data_name) \
_pam_log_state_datum(ctx, 0, module_data_name, 0)
#define _PAM_LOG_STATE_DATA_STRING(ctx, module_data_name) \
_pam_log_state_datum(ctx, 0, module_data_name, 1)
#define _PAM_LOG_STATE_ITEM_POINTER(ctx, item_type) \
_pam_log_state_datum(ctx, item_type, #item_type, 0)
#define _PAM_LOG_STATE_ITEM_STRING(ctx, item_type) \
_pam_log_state_datum(ctx, item_type, #item_type, 1)
#ifdef DEBUG_PASSWORD
#define _LOG_PASSWORD_AS_STRING 1
#else
#define _LOG_PASSWORD_AS_STRING 0
#endif
#define _PAM_LOG_STATE_ITEM_PASSWORD(ctx, item_type) \
_pam_log_state_datum(ctx, item_type, #item_type, \
_LOG_PASSWORD_AS_STRING)
/*
* wrapper to preserve old behaviour of iniparser which ignored
* key values that had no value assigned like
* key =
* for a key like above newer iniparser will return a zero-length
* string, previously iniparser would return NULL
*
* JRA: For compatibility, tiniparser behaves like iniparser.
*/
static const char *tiniparser_getstring_nonempty(struct tiniparser_dictionary *d,
const char *key,
const char *def)
{
const char *ret = tiniparser_getstring(d, key, def);
if (ret && strlen(ret) == 0) {
ret = NULL;
}
return ret;
}
static void _pam_log_state(struct pwb_context *ctx)
{
if (!ctx || !_pam_log_is_debug_state_enabled(ctx->ctrl)) {
return;
}
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_SERVICE);
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER);
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_TTY);
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RHOST);
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_RUSER);
_PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_OLDAUTHTOK);
_PAM_LOG_STATE_ITEM_PASSWORD(ctx, PAM_AUTHTOK);
_PAM_LOG_STATE_ITEM_STRING(ctx, PAM_USER_PROMPT);
_PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_CONV);
#ifdef PAM_FAIL_DELAY
_PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_FAIL_DELAY);
#endif
#ifdef PAM_REPOSITORY
_PAM_LOG_STATE_ITEM_POINTER(ctx, PAM_REPOSITORY);
#endif
_PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_HOMEDIR);
_PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSCRIPT);
_PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_LOGONSERVER);
_PAM_LOG_STATE_DATA_STRING(ctx, PAM_WINBIND_PROFILEPATH);
_PAM_LOG_STATE_DATA_STRING(ctx,
PAM_WINBIND_NEW_AUTHTOK_REQD);
/* Use atoi to get PAM result code */
_PAM_LOG_STATE_DATA_STRING(ctx,
PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH);
_PAM_LOG_STATE_DATA_POINTER(ctx, PAM_WINBIND_PWD_LAST_SET);
}
static int _pam_parse(const pam_handle_t *pamh,
int flags,
int argc,
const char **argv,
enum pam_winbind_request_type type,
struct tiniparser_dictionary **result_d)
{
int ctrl = 0;
const char *config_file = NULL;
int i;
const char **v;
struct tiniparser_dictionary *d = NULL;
if (flags & PAM_SILENT) {
ctrl |= WINBIND_SILENT;
}
for (i=argc,v=argv; i-- > 0; ++v) {
if (!strncasecmp(*v, "config", strlen("config"))) {
ctrl |= WINBIND_CONFIG_FILE;
config_file = v[i];
break;
}
}
if (config_file == NULL) {
config_file = PAM_WINBIND_CONFIG_FILE;
}
d = tiniparser_load(config_file);
if (d == NULL) {
goto config_from_pam;
}
if (tiniparser_getboolean(d, "global:debug", false)) {
ctrl |= WINBIND_DEBUG_ARG;
}
if (tiniparser_getboolean(d, "global:debug_state", false)) {
ctrl |= WINBIND_DEBUG_STATE;
}
if (tiniparser_getboolean(d, "global:cached_login", false)) {
ctrl |= WINBIND_CACHED_LOGIN;
}
if (tiniparser_getboolean(d, "global:krb5_auth", false)) {
ctrl |= WINBIND_KRB5_AUTH;
}
if (tiniparser_getboolean(d, "global:silent", false)) {
ctrl |= WINBIND_SILENT;
}
if (tiniparser_getstring_nonempty(d, "global:krb5_ccache_type", NULL) != NULL) {
ctrl |= WINBIND_KRB5_CCACHE_TYPE;
}
if ((tiniparser_getstring_nonempty(d, "global:require-membership-of", NULL)
!= NULL) ||
(tiniparser_getstring_nonempty(d, "global:require_membership_of", NULL)
!= NULL)) {
ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
}
if (tiniparser_getboolean(d, "global:try_first_pass", false)) {
ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
}
if (tiniparser_getint(d, "global:warn_pwd_expire", 0)) {
ctrl |= WINBIND_WARN_PWD_EXPIRE;
}
if (tiniparser_getboolean(d, "global:mkhomedir", false)) {
ctrl |= WINBIND_MKHOMEDIR;
}
config_from_pam:
/* step through arguments */
for (i=argc,v=argv; i-- > 0; ++v) {
/* generic options */
if (!strcmp(*v,"debug"))
ctrl |= WINBIND_DEBUG_ARG;
else if (!strcasecmp(*v, "debug_state"))
ctrl |= WINBIND_DEBUG_STATE;
else if (!strcasecmp(*v, "silent"))
ctrl |= WINBIND_SILENT;
else if (!strcasecmp(*v, "use_authtok"))
ctrl |= WINBIND_USE_AUTHTOK_ARG;
else if (!strcasecmp(*v, "use_first_pass"))
ctrl |= WINBIND_USE_FIRST_PASS_ARG;
else if (!strcasecmp(*v, "try_first_pass"))
ctrl |= WINBIND_TRY_FIRST_PASS_ARG;
else if (!strcasecmp(*v, "unknown_ok"))
ctrl |= WINBIND_UNKNOWN_OK_ARG;
else if ((type == PAM_WINBIND_AUTHENTICATE
|| type == PAM_WINBIND_SETCRED)
&& !strncasecmp(*v, "require_membership_of",
strlen("require_membership_of")))
ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
else if ((type == PAM_WINBIND_AUTHENTICATE
|| type == PAM_WINBIND_SETCRED)
&& !strncasecmp(*v, "require-membership-of",
strlen("require-membership-of")))
ctrl |= WINBIND_REQUIRED_MEMBERSHIP;
else if (!strcasecmp(*v, "krb5_auth"))
ctrl |= WINBIND_KRB5_AUTH;
else if (!strncasecmp(*v, "krb5_ccache_type",
strlen("krb5_ccache_type")))
ctrl |= WINBIND_KRB5_CCACHE_TYPE;
else if (!strcasecmp(*v, "cached_login"))
ctrl |= WINBIND_CACHED_LOGIN;
else if (!strcasecmp(*v, "mkhomedir"))
ctrl |= WINBIND_MKHOMEDIR;
else if (!strncasecmp(*v, "warn_pwd_expire",
strlen("warn_pwd_expire")))
ctrl |= WINBIND_WARN_PWD_EXPIRE;
else if (type != PAM_WINBIND_CLEANUP) {
__pam_log(pamh, ctrl, LOG_ERR,
"pam_parse: unknown option: %s", *v);
return -1;
}
}
if (result_d) {
*result_d = d;
} else {
if (d) {
tiniparser_freedict(d);
}
}
return ctrl;
};
static int _pam_winbind_free_context(struct pwb_context *ctx)
{
if (!ctx) {
return 0;
}
if (ctx->dict) {
tiniparser_freedict(ctx->dict);
}
wbcCtxFree(ctx->wbc_ctx);
return 0;
}
static int _pam_winbind_init_context(pam_handle_t *pamh,
int flags,
int argc,
const char **argv,
enum pam_winbind_request_type type,
struct pwb_context **ctx_p)
{
struct pwb_context *r = NULL;
int ctrl_code;
#ifdef HAVE_GETTEXT
textdomain_init();
#endif
r = talloc_zero(NULL, struct pwb_context);
if (!r) {
return PAM_BUF_ERR;
}
talloc_set_destructor(r, _pam_winbind_free_context);
r->pamh = pamh;
r->flags = flags;
r->argc = argc;
r->argv = argv;
ctrl_code = _pam_parse(pamh, flags, argc, argv, type, &r->dict);
if (ctrl_code == -1) {
TALLOC_FREE(r);
return PAM_SYSTEM_ERR;
}
r->ctrl = ctrl_code;
r->wbc_ctx = wbcCtxCreate();
if (r->wbc_ctx == NULL) {
TALLOC_FREE(r);
return PAM_SYSTEM_ERR;
}
*ctx_p = r;
return PAM_SUCCESS;
}
static void _pam_winbind_cleanup_func(pam_handle_t *pamh,
void *data,
int error_status)
{
int ctrl = _pam_parse(pamh, 0, 0, NULL, PAM_WINBIND_CLEANUP, NULL);
if (_pam_log_is_debug_state_enabled(ctrl)) {
__pam_log_debug(pamh, ctrl, LOG_DEBUG,
"[pamh: %p] CLEAN: cleaning up PAM data %p "
"(error_status = %d)", pamh, data,
error_status);
}
TALLOC_FREE(data);
}
static const struct ntstatus_errors {
const char *ntstatus_string;
const char *error_string;
} ntstatus_errors[] = {
{"NT_STATUS_OK",
N_("Success")},
{"NT_STATUS_BACKUP_CONTROLLER",
N_("No primary Domain Controller available")},
{"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
N_("No domain controllers found")},
{"NT_STATUS_NO_LOGON_SERVERS",
N_("No logon servers")},
{"NT_STATUS_PWD_TOO_SHORT",
N_("Password too short")},
{"NT_STATUS_PWD_TOO_RECENT",
N_("The password of this user is too recent to change")},
{"NT_STATUS_PWD_HISTORY_CONFLICT",
N_("Password is already in password history")},
{"NT_STATUS_PASSWORD_EXPIRED",
N_("Your password has expired")},
{"NT_STATUS_PASSWORD_MUST_CHANGE",
N_("You need to change your password now")},
{"NT_STATUS_INVALID_WORKSTATION",
N_("You are not allowed to logon from this workstation")},
{"NT_STATUS_INVALID_LOGON_HOURS",
N_("You are not allowed to logon at this time")},
{"NT_STATUS_ACCOUNT_EXPIRED",
N_("Your account has expired. "
"Please contact your System administrator")}, /* SCNR */
{"NT_STATUS_ACCOUNT_DISABLED",
N_("Your account is disabled. "
"Please contact your System administrator")}, /* SCNR */
{"NT_STATUS_ACCOUNT_LOCKED_OUT",
N_("Your account has been locked. "
"Please contact your System administrator")}, /* SCNR */
{"NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT",
N_("Invalid Trust Account")},
{"NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT",
N_("Invalid Trust Account")},
{"NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT",
N_("Invalid Trust Account")},
{"NT_STATUS_ACCESS_DENIED",
N_("Access is denied")},
{NULL, NULL}
};
static const char *_get_ntstatus_error_string(const char *nt_status_string)
{
int i;
for (i=0; ntstatus_errors[i].ntstatus_string != NULL; i++) {
if (!strcasecmp(ntstatus_errors[i].ntstatus_string,
nt_status_string)) {
return _(ntstatus_errors[i].error_string);
}
}
return NULL;
}
/* --- authentication management functions --- */
/* Attempt a conversation */
static int converse(const pam_handle_t *pamh,
int nargs,
const struct pam_message **message,
struct pam_response **response)
{
int retval;
const struct pam_conv *conv;
retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
if (retval == PAM_SUCCESS) {
retval = conv->conv(nargs,
discard_const_p(const struct pam_message *, message),
response, conv->appdata_ptr);
}
return retval; /* propagate error status */
}
static int _make_remark(struct pwb_context *ctx,
int type,
const char *text)
{
int retval = PAM_SUCCESS;
const struct pam_message *pmsg[1];
struct pam_message msg[1];
struct pam_response *resp;
if (ctx->flags & WINBIND_SILENT) {
return PAM_SUCCESS;
}
pmsg[0] = &msg[0];
msg[0].msg = discard_const_p(char, text);
msg[0].msg_style = type;
resp = NULL;
retval = converse(ctx->pamh, 1, pmsg, &resp);
if (resp) {
_pam_drop_reply(resp, 1);
}
return retval;
}
static int _make_remark_v(struct pwb_context *ctx,
int type,
const char *format,
va_list args) PRINTF_ATTRIBUTE(3, 0);
static int _make_remark_v(struct pwb_context *ctx,
int type,
const char *format,
va_list args)
{
char *var;
int ret;
ret = vasprintf(&var, format, args);
if (ret < 0) {
_pam_log(ctx, LOG_ERR, "memory allocation failure");
return ret;
}
ret = _make_remark(ctx, type, var);
SAFE_FREE(var);
return ret;
}
static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
static int _make_remark_format(struct pwb_context *ctx, int type, const char *format, ...)
{
int ret;
va_list args;
va_start(args, format);
ret = _make_remark_v(ctx, type, format, args);
va_end(args);
return ret;
}
static int pam_winbind_request_log(struct pwb_context *ctx,
int retval,
const char *user,
const char *fn)
{
switch (retval) {
case PAM_AUTH_ERR:
/* incorrect password */
_pam_log(ctx, LOG_WARNING, "user '%s' denied access "
"(incorrect password or invalid membership)", user);
return retval;
case PAM_ACCT_EXPIRED:
/* account expired */
_pam_log(ctx, LOG_WARNING, "user '%s' account expired",
user);
return retval;
case PAM_AUTHTOK_EXPIRED:
/* password expired */
_pam_log(ctx, LOG_WARNING, "user '%s' password expired",
user);
return retval;
case PAM_NEW_AUTHTOK_REQD:
/* new password required */
_pam_log(ctx, LOG_WARNING, "user '%s' new password "
"required", user);
return retval;
case PAM_USER_UNKNOWN:
/* the user does not exist */
_pam_log_debug(ctx, LOG_NOTICE, "user '%s' not found",
user);
if (ctx->ctrl & WINBIND_UNKNOWN_OK_ARG) {
return PAM_IGNORE;
}
return retval;
case PAM_AUTHTOK_ERR:
/* Authentication token manipulation error */
_pam_log(ctx, LOG_WARNING, "user `%s' authentication token change failed "
"(pwd complexity/history/min_age not met?)", user);
return retval;
case PAM_SUCCESS:
/* Otherwise, the authentication looked good */
if (strcmp(fn, "wbcLogonUser") == 0) {
_pam_log(ctx, LOG_NOTICE,
"user '%s' granted access", user);
} else {
_pam_log(ctx, LOG_NOTICE,
"user '%s' OK", user);
}
return retval;
default:
/* we don't know anything about this return value */
_pam_log(ctx, LOG_ERR,
"internal module error (retval = %s(%d), user = '%s')",
_pam_error_code_str(retval), retval, user);
return retval;
}
}
static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
struct wbcAuthErrorInfo *e,
wbcErr status,
const char *username,
const char *fn)
{
int ret = PAM_AUTH_ERR;
if (WBC_ERROR_IS_OK(status)) {
_pam_log_debug(ctx, LOG_DEBUG, "request %s succeeded",
fn);
ret = PAM_SUCCESS;
return pam_winbind_request_log(ctx, ret, username, fn);
}
if (e) {
if (e->pam_error != PAM_SUCCESS) {
_pam_log(ctx, LOG_ERR,
"request %s failed: %s, "
"PAM error: %s (%d), NTSTATUS: %s, "
"Error message was: %s",
fn,
wbcErrorString(status),
_pam_error_code_str(e->pam_error),
e->pam_error,
e->nt_string,
e->display_string);
ret = e->pam_error;
return pam_winbind_request_log(ctx, ret, username, fn);
}
_pam_log(ctx, LOG_ERR, "request %s failed, but PAM error 0!", fn);
ret = PAM_SERVICE_ERR;
return pam_winbind_request_log(ctx, ret, username, fn);
}
ret = wbc_error_to_pam_error(status);
return pam_winbind_request_log(ctx, ret, username, fn);
}
#if defined(HAVE_PAM_RADIO_TYPE)
static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
{
struct pam_message msg;
const struct pam_message *pmsg;
struct pam_response *resp = NULL;
int ret;
bool retval = false;
pmsg = &msg;
msg.msg_style = PAM_RADIO_TYPE;
msg.msg = _("Do you want to change your password now?");
ret = converse(ctx->pamh, 1, &pmsg, &resp);
if (resp == NULL) {
if (ret == PAM_SUCCESS) {
_pam_log(ctx, LOG_CRIT, "pam_winbind: system error!\n");
return false;
}
}
if (ret != PAM_SUCCESS) {
return false;
}
_pam_log(ctx, LOG_CRIT, "Received [%s] reply from application.\n", resp->resp);
if ((resp->resp != NULL) && (strcasecmp(resp->resp, "yes") == 0)) {
retval = true;
}
_pam_drop_reply(resp, 1);
return retval;
}
#else
static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
{
return false;
}
#endif
/**
* send a password expiry message if required
*
* @param ctx PAM winbind context.
* @param next_change expected (calculated) next expiry date.
* @param already_expired pointer to a boolean to indicate if the password is
* already expired.
*
* @return boolean Returns true if message has been sent, false if not.
*/
static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
time_t next_change,
time_t now,
int warn_pwd_expire,
bool *already_expired,
bool *change_pwd)
{
int days = 0;
struct tm tm_now, tm_next_change;
bool retval = false;
int ret;
if (already_expired) {
*already_expired = false;
}
if (change_pwd) {
*change_pwd = false;
}
if (next_change <= now) {
PAM_WB_REMARK_DIRECT(ctx, "NT_STATUS_PASSWORD_EXPIRED");
if (already_expired) {
*already_expired = true;
}
return true;
}
if ((next_change < 0) ||
(next_change > now + warn_pwd_expire * SECONDS_PER_DAY)) {
return false;
}
if ((localtime_r(&now, &tm_now) == NULL) ||
(localtime_r(&next_change, &tm_next_change) == NULL)) {
return false;
}
days = (tm_next_change.tm_yday+tm_next_change.tm_year*365) -
(tm_now.tm_yday+tm_now.tm_year*365);
if (days == 0) {
ret = _make_remark(ctx, PAM_TEXT_INFO,
_("Your password expires today.\n"));
/*
* If change_pwd and already_expired is null.
* We are just sending a notification message.
* We don't expect any response in this case.
*/
if (!change_pwd && !already_expired) {
return true;
}
/*
* successfully sent the warning message.
* Give the user a chance to change pwd.
*/
if (ret == PAM_SUCCESS) {
if (change_pwd) {
retval = _pam_winbind_change_pwd(ctx);
if (retval) {
*change_pwd = true;
}
}
}
return true;
}
if (days > 0 && days < warn_pwd_expire) {
ret = _make_remark_format(ctx, PAM_TEXT_INFO,
_("Your password will expire in %d %s.\n"),
days, (days > 1) ? _("days"):_("day"));
/*
* If change_pwd and already_expired is null.
* We are just sending a notification message.
* We don't expect any response in this case.
*/
if (!change_pwd && !already_expired) {
return true;
}
/*
* successfully sent the warning message.
* Give the user a chance to change pwd.
*/
if (ret == PAM_SUCCESS) {
if (change_pwd) {
retval = _pam_winbind_change_pwd(ctx);
if (retval) {
*change_pwd = true;
}