1*7f2fe78bSCy Schubertfrom k5test import * 2*7f2fe78bSCy Schubert 3*7f2fe78bSCy Schubert# Test that the kdcpreauth client_keyblock() callback matches the key 4*7f2fe78bSCy Schubert# indicated by the etype info, and returns NULL if no key was selected. 5*7f2fe78bSCy Schuberttestpreauth = os.path.join(buildtop, 'plugins', 'preauth', 'test', 'test.so') 6*7f2fe78bSCy Schubertconf = {'plugins': {'kdcpreauth': {'module': 'test:' + testpreauth}, 7*7f2fe78bSCy Schubert 'clpreauth': {'module': 'test:' + testpreauth}}} 8*7f2fe78bSCy Schubertrealm = K5Realm(create_host=False, get_creds=False, krb5_conf=conf) 9*7f2fe78bSCy Schubertrealm.run([kadminl, 'modprinc', '+requires_preauth', realm.user_princ]) 10*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', realm.user_princ, 'teststring', 'testval']) 11*7f2fe78bSCy Schubertrealm.run([kadminl, 'addprinc', '-nokey', '+requires_preauth', 'nokeyuser']) 12*7f2fe78bSCy Schubertrealm.kinit(realm.user_princ, password('user'), expected_msg='testval') 13*7f2fe78bSCy Schubertrealm.kinit('nokeyuser', password('user'), expected_code=1, 14*7f2fe78bSCy Schubert expected_msg='no key') 15*7f2fe78bSCy Schubert 16*7f2fe78bSCy Schubert# Preauth type -123 is the test preauth module type; 133 is FAST 17*7f2fe78bSCy Schubert# PA-FX-COOKIE; 2 is encrypted timestamp. 18*7f2fe78bSCy Schubert 19*7f2fe78bSCy Schubert# Test normal preauth flow. 20*7f2fe78bSCy Schubertmark('normal') 21*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 22*7f2fe78bSCy Schubert '/Additional pre-authentication required', 23*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 24*7f2fe78bSCy Schubert 'Processing preauth types:', 25*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 26*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 27*7f2fe78bSCy Schubert 'Decrypted AS reply') 28*7f2fe78bSCy Schubertrealm.run(['./icred', realm.user_princ, password('user')], 29*7f2fe78bSCy Schubert expected_msg='testval', expected_trace=msgs) 30*7f2fe78bSCy Schubert 31*7f2fe78bSCy Schubert# Test successful optimistic preauth. 32*7f2fe78bSCy Schubertmark('optimistic') 33*7f2fe78bSCy Schubertexpected_trace = ('Attempting optimistic preauth', 34*7f2fe78bSCy Schubert 'Processing preauth types: -123', 35*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 36*7f2fe78bSCy Schubert 'Produced preauth for next request: -123', 37*7f2fe78bSCy Schubert 'Decrypted AS reply') 38*7f2fe78bSCy Schubertrealm.run(['./icred', '-o', '-123', realm.user_princ, password('user')], 39*7f2fe78bSCy Schubert expected_trace=expected_trace) 40*7f2fe78bSCy Schubert 41*7f2fe78bSCy Schubert# Test optimistic preauth failing on client, falling back to encrypted 42*7f2fe78bSCy Schubert# timestamp. 43*7f2fe78bSCy Schubertmark('optimistic (client failure)') 44*7f2fe78bSCy Schubertmsgs = ('Attempting optimistic preauth', 45*7f2fe78bSCy Schubert 'Processing preauth types: -123', 46*7f2fe78bSCy Schubert '/induced optimistic fail', 47*7f2fe78bSCy Schubert 'Sending unauthenticated request', 48*7f2fe78bSCy Schubert '/Additional pre-authentication required', 49*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 50*7f2fe78bSCy Schubert 'Processing preauth types:', 51*7f2fe78bSCy Schubert 'Encrypted timestamp (for ', 52*7f2fe78bSCy Schubert 'module encrypted_timestamp (2) (real) returned: 0/Success', 53*7f2fe78bSCy Schubert 'preauth for next request: PA-FX-COOKIE (133), PA-ENC-TIMESTAMP (2)', 54*7f2fe78bSCy Schubert 'Decrypted AS reply') 55*7f2fe78bSCy Schubertrealm.run(['./icred', '-o', '-123', '-X', 'fail_optimistic', realm.user_princ, 56*7f2fe78bSCy Schubert password('user')], expected_trace=msgs) 57*7f2fe78bSCy Schubert 58*7f2fe78bSCy Schubert# Test optimistic preauth failing on KDC, falling back to encrypted 59*7f2fe78bSCy Schubert# timestamp. 60*7f2fe78bSCy Schubertmark('optimistic (KDC failure)') 61*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', realm.user_princ, 'failopt', 'yes']) 62*7f2fe78bSCy Schubertmsgs = ('Attempting optimistic preauth', 63*7f2fe78bSCy Schubert 'Processing preauth types: -123', 64*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 65*7f2fe78bSCy Schubert 'Produced preauth for next request: -123', 66*7f2fe78bSCy Schubert '/Preauthentication failed', 67*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 68*7f2fe78bSCy Schubert 'Processing preauth types:', 69*7f2fe78bSCy Schubert 'Encrypted timestamp (for ', 70*7f2fe78bSCy Schubert 'module encrypted_timestamp (2) (real) returned: 0/Success', 71*7f2fe78bSCy Schubert 'preauth for next request: PA-FX-COOKIE (133), PA-ENC-TIMESTAMP (2)', 72*7f2fe78bSCy Schubert 'Decrypted AS reply') 73*7f2fe78bSCy Schubertrealm.run(['./icred', '-o', '-123', realm.user_princ, password('user')], 74*7f2fe78bSCy Schubert expected_trace=msgs) 75*7f2fe78bSCy Schubert# Leave failopt set for the next test. 76*7f2fe78bSCy Schubert 77*7f2fe78bSCy Schubert# Test optimistic preauth failing on KDC, stopping because the test 78*7f2fe78bSCy Schubert# module disabled fallback. 79*7f2fe78bSCy Schubertmark('optimistic (KDC failure, no fallback)') 80*7f2fe78bSCy Schubertmsgs = ('Attempting optimistic preauth', 81*7f2fe78bSCy Schubert 'Processing preauth types: -123', 82*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 83*7f2fe78bSCy Schubert 'Produced preauth for next request: -123', 84*7f2fe78bSCy Schubert '/Preauthentication failed') 85*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'disable_fallback', '-o', '-123', realm.user_princ, 86*7f2fe78bSCy Schubert password('user')], expected_code=1, 87*7f2fe78bSCy Schubert expected_msg='Preauthentication failed', expected_trace=msgs) 88*7f2fe78bSCy Schubertrealm.run([kadminl, 'delstr', realm.user_princ, 'failopt']) 89*7f2fe78bSCy Schubert 90*7f2fe78bSCy Schubert# Test KDC_ERR_MORE_PREAUTH_DATA_REQUIRED and secure cookies. 91*7f2fe78bSCy Schubertmark('second round-trip') 92*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', realm.user_princ, '2rt', 'secondtrip']) 93*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 94*7f2fe78bSCy Schubert '/Additional pre-authentication required', 95*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 96*7f2fe78bSCy Schubert 'Processing preauth types:', 97*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 98*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 99*7f2fe78bSCy Schubert '/More preauthentication data is required', 100*7f2fe78bSCy Schubert 'Continuing preauth mech -123', 101*7f2fe78bSCy Schubert 'Processing preauth types: -123, PA-FX-COOKIE (133)', 102*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 103*7f2fe78bSCy Schubert 'Decrypted AS reply') 104*7f2fe78bSCy Schubertrealm.run(['./icred', realm.user_princ, password('user')], 105*7f2fe78bSCy Schubert expected_msg='2rt: secondtrip', expected_trace=msgs) 106*7f2fe78bSCy Schubert 107*7f2fe78bSCy Schubert# Test client-side failure after KDC_ERR_MORE_PREAUTH_DATA_REQUIRED, 108*7f2fe78bSCy Schubert# falling back to encrypted timestamp. 109*7f2fe78bSCy Schubertmark('second round-trip (client failure)') 110*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 111*7f2fe78bSCy Schubert '/Additional pre-authentication required', 112*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 113*7f2fe78bSCy Schubert 'Processing preauth types:', 114*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 115*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 116*7f2fe78bSCy Schubert '/More preauthentication data is required', 117*7f2fe78bSCy Schubert 'Continuing preauth mech -123', 118*7f2fe78bSCy Schubert 'Processing preauth types: -123, PA-FX-COOKIE (133)', 119*7f2fe78bSCy Schubert '/induced 2rt fail', 120*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 121*7f2fe78bSCy Schubert 'Processing preauth types:', 122*7f2fe78bSCy Schubert 'Encrypted timestamp (for ', 123*7f2fe78bSCy Schubert 'module encrypted_timestamp (2) (real) returned: 0/Success', 124*7f2fe78bSCy Schubert 'preauth for next request: PA-FX-COOKIE (133), PA-ENC-TIMESTAMP (2)', 125*7f2fe78bSCy Schubert 'Decrypted AS reply') 126*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'fail_2rt', realm.user_princ, password('user')], 127*7f2fe78bSCy Schubert expected_msg='2rt: secondtrip', expected_trace=msgs) 128*7f2fe78bSCy Schubert 129*7f2fe78bSCy Schubert# Test client-side failure after KDC_ERR_MORE_PREAUTH_DATA_REQUIRED, 130*7f2fe78bSCy Schubert# stopping because the test module disabled fallback. 131*7f2fe78bSCy Schubertmark('second round-trip (client failure, no fallback)') 132*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 133*7f2fe78bSCy Schubert '/Additional pre-authentication required', 134*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 135*7f2fe78bSCy Schubert 'Processing preauth types:', 136*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 137*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 138*7f2fe78bSCy Schubert '/More preauthentication data is required', 139*7f2fe78bSCy Schubert 'Continuing preauth mech -123', 140*7f2fe78bSCy Schubert 'Processing preauth types: -123, PA-FX-COOKIE (133)', 141*7f2fe78bSCy Schubert '/induced 2rt fail') 142*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'fail_2rt', '-X', 'disable_fallback', 143*7f2fe78bSCy Schubert realm.user_princ, password('user')], expected_code=1, 144*7f2fe78bSCy Schubert expected_msg='Pre-authentication failed: induced 2rt fail', 145*7f2fe78bSCy Schubert expected_trace=msgs) 146*7f2fe78bSCy Schubert 147*7f2fe78bSCy Schubert# Test KDC-side failure after KDC_ERR_MORE_PREAUTH_DATA_REQUIRED, 148*7f2fe78bSCy Schubert# falling back to encrypted timestamp. 149*7f2fe78bSCy Schubertmark('second round-trip (KDC failure)') 150*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', realm.user_princ, 'fail2rt', 'yes']) 151*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 152*7f2fe78bSCy Schubert '/Additional pre-authentication required', 153*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 154*7f2fe78bSCy Schubert 'Processing preauth types:', 155*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 156*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 157*7f2fe78bSCy Schubert '/More preauthentication data is required', 158*7f2fe78bSCy Schubert 'Continuing preauth mech -123', 159*7f2fe78bSCy Schubert 'Processing preauth types: -123, PA-FX-COOKIE (133)', 160*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 161*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 162*7f2fe78bSCy Schubert '/Preauthentication failed', 163*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 164*7f2fe78bSCy Schubert 'Processing preauth types:', 165*7f2fe78bSCy Schubert 'Encrypted timestamp (for ', 166*7f2fe78bSCy Schubert 'module encrypted_timestamp (2) (real) returned: 0/Success', 167*7f2fe78bSCy Schubert 'preauth for next request: PA-FX-COOKIE (133), PA-ENC-TIMESTAMP (2)', 168*7f2fe78bSCy Schubert 'Decrypted AS reply') 169*7f2fe78bSCy Schubertrealm.run(['./icred', realm.user_princ, password('user')], 170*7f2fe78bSCy Schubert expected_msg='2rt: secondtrip', expected_trace=msgs) 171*7f2fe78bSCy Schubert# Leave fail2rt set for the next test. 172*7f2fe78bSCy Schubert 173*7f2fe78bSCy Schubert# Test KDC-side failure after KDC_ERR_MORE_PREAUTH_DATA_REQUIRED, 174*7f2fe78bSCy Schubert# stopping because the test module disabled fallback. 175*7f2fe78bSCy Schubertmark('second round-trip (KDC failure, no fallback)') 176*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 177*7f2fe78bSCy Schubert '/Additional pre-authentication required', 178*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 179*7f2fe78bSCy Schubert 'Processing preauth types:', 180*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 181*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 182*7f2fe78bSCy Schubert '/More preauthentication data is required', 183*7f2fe78bSCy Schubert 'Continuing preauth mech -123', 184*7f2fe78bSCy Schubert 'Processing preauth types: -123, PA-FX-COOKIE (133)', 185*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 186*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 187*7f2fe78bSCy Schubert '/Preauthentication failed') 188*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'disable_fallback', 189*7f2fe78bSCy Schubert realm.user_princ, password('user')], expected_code=1, 190*7f2fe78bSCy Schubert expected_msg='Preauthentication failed', expected_trace=msgs) 191*7f2fe78bSCy Schubertrealm.run([kadminl, 'delstr', realm.user_princ, 'fail2rt']) 192*7f2fe78bSCy Schubert 193*7f2fe78bSCy Schubert# Test tryagain flow by inducing a KDC_ERR_ENCTYPE_NOSUPP error on the KDC. 194*7f2fe78bSCy Schubertmark('tryagain') 195*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', realm.user_princ, 'err', 'testagain']) 196*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 197*7f2fe78bSCy Schubert '/Additional pre-authentication required', 198*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 199*7f2fe78bSCy Schubert 'Processing preauth types:', 200*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 201*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 202*7f2fe78bSCy Schubert '/KDC has no support for encryption type', 203*7f2fe78bSCy Schubert 'Recovering from KDC error 14 using preauth mech -123', 204*7f2fe78bSCy Schubert 'Preauth tryagain input types (-123): -123, PA-FX-COOKIE (133)', 205*7f2fe78bSCy Schubert 'Preauth module test (-123) tryagain returned: 0/Success', 206*7f2fe78bSCy Schubert 'Followup preauth for next request: -123, PA-FX-COOKIE (133)', 207*7f2fe78bSCy Schubert 'Decrypted AS reply') 208*7f2fe78bSCy Schubertrealm.run(['./icred', realm.user_princ, password('user')], 209*7f2fe78bSCy Schubert expected_msg='tryagain: testagain', expected_trace=msgs) 210*7f2fe78bSCy Schubert 211*7f2fe78bSCy Schubert# Test a client-side tryagain failure, falling back to encrypted 212*7f2fe78bSCy Schubert# timestamp. 213*7f2fe78bSCy Schubertmark('tryagain (client failure)') 214*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 215*7f2fe78bSCy Schubert '/Additional pre-authentication required', 216*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 217*7f2fe78bSCy Schubert 'Processing preauth types:', 218*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 219*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 220*7f2fe78bSCy Schubert '/KDC has no support for encryption type', 221*7f2fe78bSCy Schubert 'Recovering from KDC error 14 using preauth mech -123', 222*7f2fe78bSCy Schubert 'Preauth tryagain input types (-123): -123, PA-FX-COOKIE (133)', 223*7f2fe78bSCy Schubert '/induced tryagain fail', 224*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 225*7f2fe78bSCy Schubert 'Processing preauth types:', 226*7f2fe78bSCy Schubert 'Encrypted timestamp (for ', 227*7f2fe78bSCy Schubert 'module encrypted_timestamp (2) (real) returned: 0/Success', 228*7f2fe78bSCy Schubert 'preauth for next request: PA-FX-COOKIE (133), PA-ENC-TIMESTAMP (2)', 229*7f2fe78bSCy Schubert 'Decrypted AS reply') 230*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'fail_tryagain', realm.user_princ, 231*7f2fe78bSCy Schubert password('user')], expected_trace=msgs) 232*7f2fe78bSCy Schubert 233*7f2fe78bSCy Schubert# Test a client-side tryagain failure, stopping because the test 234*7f2fe78bSCy Schubert# module disabled fallback. 235*7f2fe78bSCy Schubertmark('tryagain (client failure, no fallback)') 236*7f2fe78bSCy Schubertmsgs = ('Sending unauthenticated request', 237*7f2fe78bSCy Schubert '/Additional pre-authentication required', 238*7f2fe78bSCy Schubert 'Preauthenticating using KDC method data', 239*7f2fe78bSCy Schubert 'Processing preauth types:', 240*7f2fe78bSCy Schubert 'Preauth module test (-123) (real) returned: 0/Success', 241*7f2fe78bSCy Schubert 'Produced preauth for next request: PA-FX-COOKIE (133), -123', 242*7f2fe78bSCy Schubert '/KDC has no support for encryption type', 243*7f2fe78bSCy Schubert 'Recovering from KDC error 14 using preauth mech -123', 244*7f2fe78bSCy Schubert 'Preauth tryagain input types (-123): -123, PA-FX-COOKIE (133)', 245*7f2fe78bSCy Schubert '/induced tryagain fail') 246*7f2fe78bSCy Schubertrealm.run(['./icred', '-X', 'fail_tryagain', '-X', 'disable_fallback', 247*7f2fe78bSCy Schubert realm.user_princ, password('user')], expected_code=1, 248*7f2fe78bSCy Schubert expected_msg='KDC has no support for encryption type', 249*7f2fe78bSCy Schubert expected_trace=msgs) 250*7f2fe78bSCy Schubert 251*7f2fe78bSCy Schubert# Test that multiple stepwise initial creds operations can be 252*7f2fe78bSCy Schubert# performed with the same krb5_context, with proper tracking of 253*7f2fe78bSCy Schubert# clpreauth module request handles. 254*7f2fe78bSCy Schubertmark('interleaved') 255*7f2fe78bSCy Schubertrealm.run([kadminl, 'addprinc', '-pw', 'pw', 'u1']) 256*7f2fe78bSCy Schubertrealm.run([kadminl, 'addprinc', '+requires_preauth', '-pw', 'pw', 'u2']) 257*7f2fe78bSCy Schubertrealm.run([kadminl, 'addprinc', '+requires_preauth', '-pw', 'pw', 'u3']) 258*7f2fe78bSCy Schubertrealm.run([kadminl, 'setstr', 'u2', '2rt', 'extra']) 259*7f2fe78bSCy Schubertout = realm.run(['./icinterleave', 'pw', 'u1', 'u2', 'u3']) 260*7f2fe78bSCy Schubertif out != ('step 1\nstep 2\nstep 3\nstep 1\nfinish 1\nstep 2\nno attr\n' 261*7f2fe78bSCy Schubert 'step 3\nno attr\nstep 2\n2rt: extra\nstep 3\nfinish 3\nstep 2\n' 262*7f2fe78bSCy Schubert 'finish 2\n'): 263*7f2fe78bSCy Schubert fail('unexpected output from icinterleave') 264*7f2fe78bSCy Schubert 265*7f2fe78bSCy Schubertsuccess('Pre-authentication framework tests') 266