forked from IBM/db2-samples
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIBMkrb5.c
2292 lines (2050 loc) · 72.9 KB
/
IBMkrb5.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
/****************************************************************************
** Licensed Materials - Property of IBM
**
** Governed under the terms of the International
** License Agreement for Non-Warranted Sample Code.
**
** (C) COPYRIGHT International Business Machines Corp. 2012
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*****************************************************************************
**
** SOURCE FILE NAME: IBMkrb5.c
**
** SAMPLE: Kerberos 5 authentication security plugin
**
** GSS-APIs USED:
** gss_accept_sec_context - accept the security context
** gss_acquire_cred - acquire credentials handle
** gss_delete_sec_context - delete security context
** gss_display_name - display test representation of internal name
** gss_display_status - display text message for status code
** gss_import_name - convert text name into internal format
** gss_init_sec_context - initialize the security context
** gss_inquire_context - obtain info about current context
** gss_inquire_cred - obtain info about default credential
** gss_krb5_acquire_cred_ccache - obtain GSS-API cred handle from
** krb5 cred cache
** gss_release_buffer - release storage associated with internal buffer
** gss_release_cred - release storage associated with credential handle
** gss_release_name - releases storage associated with internal name
**
** KRB5 APIs USED:
** krb5_build_principal_ext - build principal from component strings
** krb5_cc_destroy - destroy credentials cache
** krb5_cc_intialize - intialize credentials cache
** krb5_cc_resolve - resolve credentials cache
* krb5_free_context - free storage associated with krb5 context
** krb5_free_principal - free storage assciated with principal
** krb5_free_string - free text string
** krb5_get_in_tkt_with_password - obtain tgt using userid/password
** krb5_init_context - create a new krb5 context
** krb5_parse_name - create a krb5 internal name from a text name
** krb5_sname_to_principal - generate principal name from service name
** krb5_svc_get_msg - get text message from krb5 error code
** krb5_unparse_name - convert principal to text string
** krb5_us_timeofday - returns time in seconds & microseconds
**
** STRUCTURES USED:
** gss_buffer_desc
** gss_cred_id_t
** gss_ctx_id_t
** gss_name_t
** krb5_data
** krb5_context
** krb5_principal
** krb5_creds
** krb5_ccache
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing C applications, see the Application
** Development Guide.
**
** For information on using SQL statements, see the SQL Reference.
**
** For information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, building, and running DB2
** applications, visit the DB2 application development website:
** http://www.software.ibm.com/data/db2/udb/ad
****************************************************************************
**
** This file is setup to depend on one of three Kerberos implementations
** 1) NAS - Network Authentication Service, AIX only
** 2) Solaris Kerberos - Implementation of Kerberos on Solaris
** 3) MIT source - platforms that direclty match the MIT source implementation
**
** In order for this file to compile, one of the following must be defined
** by the caller.
** NAS_SUPPORT
** SOLARIS_KERBEROS_SUPPORT
** MIT_KERBEROS_SUPPORT
**
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined SOLARIS_KERBEROS_SUPPORT
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_ext.h>
/* krb5.h is broken in Solaris. It does not properly define 'extern "C"' at the
beginning of the file, so we need to do it ourself. This is on 5.10 */
#if defined(__cplusplus)
#define KRB5INT_BEGIN_DECLS extern "C" {
#define KRB5INT_END_DECLS }
#endif
#include <kerberosv5/krb5.h>
#include <kerberosv5/com_err.h>
#elif defined MIT_KERBEROS_SUPPORT
#include <gssapi.h>
#include <gssapi/gssapi_krb5.h>
#include <krb5.h>
/* MIT KRB5 1.5 has a new error handling message
Mininum supported version of Linux for DB2 is still at 1.4 */
#if !defined(__linux)
#define MIT_KERBEROS_HAS_GET_ERROR_MSG
#endif
#elif defined NAS_SUPPORT
#include <gssapi/gssapi_krb5.h>
#include <ibm_svc/krb5_svc.h>
#else
#error undefined Kerberos implementation
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Since we have included the platform specific GSSAPI header files
we will get colisions if we include gssapiDB2.h. So make sure that
it is not included via db2secPlugin.h */
#define _GSSAPIDB2_H
#include "db2secPlugin.h"
static const char getenvError[] = "getenv( DB2INSTANCE ) = NULL";
/* Global var to keep track of memory allocated during server init */
static char *pluginServerPrincipalName = NULL;
static gss_name_t pluginServerName = GSS_C_NO_NAME;
static gss_cred_id_t pluginServerCredHandle = GSS_C_NO_CREDENTIAL;
/* Client and server message logging functions */
db2secLogMessage *pServerLogMessage;
db2secLogMessage *pClientLogMessage;
/******************************************************************************
*
* Function Name = plugin_gss_display_status
*
* Descriptive Name = Plugin Wrapper to gss_display_status
*
* Function = This wrapper function needs to exist since the NAS
* libraries implemented this function with a slightly
* different prototype than outlined in IETF RFC2744
*
* Dependencies = None
*
* Restrictions = None
*
* Input =
*
* Output =
*
* Normal Return = GSS_S_COMPLETE
*
* Error Return = GSS-API status codes returned by gss_display_status
*
*******************************************************************************/
OM_uint32 SQL_API_FN plugin_gss_display_status( OM_uint32 *minor_status,
OM_uint32 status_value,
int status_type,
const gss_OID mech_type,
OM_uint32 * message_context,
gss_buffer_t status_string )
{
OM_uint32 majorStatus;
majorStatus = gss_display_status( minor_status,
status_value,
status_type,
(gss_OID) mech_type,
message_context,
status_string );
return( majorStatus );
}
/******************************************************************************
*
* Function Name = getGSSErrorMsg
*
* Descriptive Name = Get the text GSS-API text error message based on the
* major and minor status codes
*
* Function =
*
* Dependencies = Memory to hold error messages are alloacted and
* db2secFreeErrormsg() is assumed to be called to free
* buffer once it is no longer neede
*
* Restrictions = Only the first message for the major and minor status
* codes are obtained. The messages are then
* concatenating into the ErrorMsg.
* No checks are performed to ascertain if buffer already
* allocated.
*
* Input =
*
* Output =
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = None. No error message will be returned in case of error
*
*******************************************************************************/
void getGSSErrorMsg( OM_uint32 majorStatus,
OM_uint32 minorStatus,
char **ppErrorMsg,
db2int32 *pErrorMsgLen,
const char *funcName )
{
OM_uint32 major = GSS_S_COMPLETE;
OM_uint32 minor = GSS_S_COMPLETE;
OM_uint32 msgContext = 0;
gss_buffer_desc majorBuff = GSS_C_EMPTY_BUFFER;
gss_buffer_desc minorBuff = GSS_C_EMPTY_BUFFER;
size_t length = 0;
/*
* Get the first major status message
*/
if( majorStatus != GSS_S_COMPLETE )
{
major = gss_display_status( &minor,
majorStatus,
GSS_C_GSS_CODE,
GSS_C_NULL_OID,
&msgContext,
&majorBuff );
if( major != GSS_S_COMPLETE )
{
/* Nothing to really do here since we're already in an error condition */
goto exit;
}
}
/*
* Get the first minor status message
*/
if( minorStatus != GSS_S_COMPLETE )
{
major = gss_display_status( &minor,
minorStatus,
GSS_C_MECH_CODE,
GSS_C_NULL_OID,
&msgContext,
&minorBuff );
if( major != GSS_S_COMPLETE )
{
/* Nothing to really do here since we're already in an error condition */
goto exit;
}
}
/* Allocate memory for error msg and copy
*
* Note: the +6 to the length is to account for the punctuation and spacing
* used in concatentating the error messages
* the +30 is to account for the two integers that are printed.
*/
length = majorBuff.length + minorBuff.length + strlen(funcName) + 36;
*ppErrorMsg = (char *) malloc( length );
if( *ppErrorMsg == NULL )
{
goto exit;
}
*pErrorMsgLen = length;
if( majorBuff.length > 0 )
{
if( minorBuff.length > 0 )
{
snprintf( *ppErrorMsg, length, "%s: (%u,%i) %.*s. %.*s",
funcName, majorStatus, (krb5_error_code)minorStatus,
(int)majorBuff.length, (char *)majorBuff.value,
(int)minorBuff.length, (char *)minorBuff.value );
}
else
{
snprintf( *ppErrorMsg, length, "%s: (%u,%i)%.*s",
funcName, majorStatus, (krb5_error_code)minorStatus,
(int)majorBuff.length, (char *)majorBuff.value );
}
}
else
{
if( minorBuff.length > 0 )
{
snprintf( *ppErrorMsg, length, "%s: (%u,%i) %.*s",
funcName, majorStatus, (krb5_error_code)minorStatus,
(int)minorBuff.length, (char *)minorBuff.value );
}
else
{
snprintf( *ppErrorMsg, length, "%s: (%u,%i)", funcName,
majorStatus, (krb5_error_code)minorStatus );
}
}
exit:
/* Free GSS-API allocated memory */
if( majorBuff.length != 0 )
{
gss_release_buffer( &minor, &majorBuff );
}
if( minorBuff.length != 0 )
{
gss_release_buffer( &minor, &minorBuff );
}
return;
}
/******************************************************************************
*
* Function Name = mapPrincToAuthid
*
* Descriptive Name = Maps the Kerberos Principal into a DB2 AUTHID
*
* Function =
*
* Dependencies = None
*
* Restrictions = Assumes that the principal name is in the format
* name/instance@REALM
*
* Input = pName - Buffer containing the principal name
*
* Output = authid - preallocated buffer (of size
* DB2SEC_MAX_AUTHID_LENGTH) containing the AUTHID
* authidLen - length of the AUTHID string
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = DB2SEC_PLUGIN_BADUSER
*
*******************************************************************************/
SQL_API_RC mapPrincToAuthid( gss_buffer_t pName,
char *authid,
db2int32 *authidLen )
{
SQL_API_RC rc = DB2SEC_PLUGIN_OK;
int count;
char *pTextName;
/* The authid will simply be derived from the first part of the fully
* qualified principal name.
* e.g., The authid from 'name/instance@REALM' will be 'name'
* DB2 will uppercase the authids and verify adherance to the naming rules
* so no need to do it here
*/
pTextName = (char *) (pName->value);
for( count=0; count < pName->length; count++ )
{
if( (pTextName[count] == '@') || (pTextName[count] == '/') )
{
break;
}
}
/*
* ALTERNATE MAPPINGS SUGGESTIONS:
* 1 - Return full principal name
* 2 - Read maping from a file
*/
if( count > DB2SEC_MAX_AUTHID_LENGTH )
{
rc = DB2SEC_PLUGIN_BADUSER;
goto error;
}
memcpy( authid, pName->value, count );
*authidLen = count;
exit:
return( rc );
error:
goto exit;
}
/******************************************************************************
*
* Function Name = mapGSSAPItoDB2SECerror
*
* Descriptive Name = Map a GSS-API major/minor code into DB2SEC error code
*
* Function =
*
* Dependencies = None
*
* Restrictions = None
*
* Input = major - Major status code
* minor - Minor status code
*
* Output = None
*
* Normal Return = Various DB2SEC error code
*
* Error Return = None
*
*******************************************************************************/
SQL_API_RC mapGSSAPItoDB2SECerror( OM_uint32 major, OM_uint32 minor )
{
SQL_API_RC rc = DB2SEC_PLUGIN_OK;
krb5_error_code kMinorStatus = (krb5_error_code) minor;
switch( major )
{
case GSS_S_COMPLETE:
rc = DB2SEC_PLUGIN_OK;
break;
case GSS_S_BAD_NAME:
rc = DB2SEC_PLUGIN_BAD_PRINCIPAL_NAME;
break;
case GSS_S_NO_CRED:
rc = DB2SEC_PLUGIN_NO_CRED;
break;
case GSS_S_CREDENTIALS_EXPIRED:
rc = DB2SEC_PLUGIN_CRED_EXPIRED;
break;
case GSS_S_FAILURE:
switch( kMinorStatus )
{
case KRB5_FCC_NOFILE:
rc = DB2SEC_PLUGIN_NO_CRED;
break;
case KRB5KRB_AP_ERR_BAD_INTEGRITY:
rc = DB2SEC_PLUGIN_BADPWD;
break;
case KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN:
rc = DB2SEC_PLUGIN_BADUSER;
break;
case KRB5KDC_ERR_NAME_EXP:
rc = DB2SEC_PLUGIN_UID_EXPIRED;
break;
case KRB5KDC_ERR_KEY_EXP:
rc = DB2SEC_PLUGIN_PWD_EXPIRED;
break;
case KRB5_CC_NOTFOUND:
rc = DB2SEC_PLUGIN_NO_CRED;
break;
default:
rc = DB2SEC_PLUGIN_UNKNOWNERROR;
break;
}
break;
default:
rc = DB2SEC_PLUGIN_UNKNOWNERROR;
break;
}
return( rc );
}
/******************************************************************************
*
* Function Name = db2secFreeErrormsg
*
* Descriptive Name = Free error message buffer
*
* Function =
*
* Dependencies = Memory to hold error messages are alloacted and
* db2secFreeErrormsg() is assumed to be called to free
* buffer once it is no longer neede
*
* Restrictions =
*
* Input = ppErrorMsg - Pointer to string holding the error message
* that was previously allocated by one of the
* db2sec* plugin functions
*
* Output = None
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = None
*
*******************************************************************************/
SQL_API_RC SQL_API_FN db2secFreeErrormsg( char *ppErrorMsg )
{
free( ppErrorMsg );
return( DB2SEC_PLUGIN_OK );
}
/******************************************************************************
*
* Function Name = getKrb5ErrorMsg
*
* Descriptive Name = Get the text GSS-API text error message based on the
* major and minor status codes
*
* Function =
*
* Dependencies =
*
* Restrictions = Only the first message for the major and minor status
* codes are obtained. The messages are then
* concatenating into the ErrorMsg.
* No checks are performed to ascertain if buffer already
* allocated.
*
* Input =
*
* Output =
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = None. No error message will be returned in case of error
*
*******************************************************************************/
void getKrb5ErrorMsg( krb5_error_code kStatus,
char **ppErrorMsg,
db2int32 *pErrorMsgLen,
const char *funcName )
{
char *message = NULL;
int length = 0;
#if defined MIT_KERBEROS_SUPPORT && defined MIT_KERBEROS_HAS_GET_ERROR_MSG
/* krb5_get_error_message is new to the MIT source in 1.5 */
krb5_context kContext = NULL;
krb5_error_code kstatus;
static char szErrContext[] = "Failed to create krb5 context";
/* Create a new krb5 context */
kstatus = krb5_init_context( &kContext );
if( kstatus )
{
message = szErrContext;
}
else
{
message = (char *) krb5_get_error_message(kContext, kStatus);
}
#elif defined MIT_KERBEROS_SUPPORT || defined SOLARIS_KERBEROS_SUPPORT
message = (char *) error_message(kStatus);
#elif defined NAS_SUPPORT
krb5_svc_get_msg( (const krb5_ui_4) kStatus, &message );
#endif
if( message )
{
/* +16 to account for the punctuation in the string and (0x%x) */
length = strlen(funcName) + sizeof(kStatus) + strlen(message) + 16;
*ppErrorMsg = (char *) malloc( length );
if( *ppErrorMsg == NULL )
{
goto exit;
}
snprintf( *ppErrorMsg, length, "%s: (0x%x) %s", funcName, kStatus,message );
*pErrorMsgLen = length;
}
exit:
#if defined MIT_KERBEROS_SUPPORT && defined MIT_KERBEROS_HAS_GET_ERROR_MSG
if(kContext != NULL)
{
krb5_free_error_message(kContext, message); /* This function can be used starting krb5 1.5 */
krb5_free_context( kContext ); /* destroy context */
}
#elif defined NAS_SUPPORT
if(message != NULL)
{
krb5_free_string( NULL, message );
}
#endif
return;
}
/**
** CLIENT FUNCTIONS
**/
/******************************************************************************
*
* Function Name = db2secFreeToken
*
* Descriptive Name = Free token allocated by plug-in
*
* Function = Since no token is allocated by this plug-in, this
* function is a no-op.
*
* Dependencies = None
*
* Restrictions = None
*
* Input = token - Pointer to plug-in allocated token
*
* Output = ppErrorMsg - Pointer to a string that will contain
* an error message
* errorMsgLen - Pointer to the length of the error message
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = None
*
*******************************************************************************/
SQL_API_RC SQL_API_FN db2secFreeToken( void *token,
char **ppErrorMsg,
db2int32 *errorMsgLen )
{
/* This is a no-op since we don't use the plugin token */
return( DB2SEC_PLUGIN_OK );
}
/******************************************************************************
*
* Function Name = db2secGetDefaultLoginContext
*
* Descriptive Name =
*
* Function = This function will return the userid (principal name)
* and authid from the default login context.
*
* Dependencies = None. This function will clean up all allocated
* resources before returing to the caller
*
* Restrictions =
*
* Input = useridtype (not used)
* dbname (not used)
*
* Output = authid
* userid
* token (not used)
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = DB2SEC_PLUGIN_NOTLOGGEDIN
* DB2SEC_PLUGIN_CRED_EXPIRED
* DB2SEC_PLUGIN_UNSPECIFIEDERROR
*
*******************************************************************************/
SQL_API_RC SQL_API_FN db2secGetDefaultLoginContext (
char authid[DB2SEC_MAX_AUTHID_LENGTH],
db2int32 *pAuthIdLen,
char userid[DB2SEC_MAX_USERID_LENGTH],
db2int32 *useridLen,
db2int32 useridType,
char userNamespace[DB2SEC_MAX_USERNAMESPACE_LENGTH],
db2int32 *userNamespaceLen,
db2int32 *userNamespaceType,
const char *dbName,
db2int32 dbNameLen,
void **ppToken,
char **ppErrorMsg,
db2int32 *pErrorMsgLen )
{
SQL_API_RC rc = DB2SEC_PLUGIN_OK;
OM_uint32 majorStatus = GSS_S_COMPLETE;
OM_uint32 minorStatus;
gss_name_t gssName = GSS_C_NO_NAME;
gss_buffer_desc name = GSS_C_EMPTY_BUFFER;
gss_cred_id_t credHandle = GSS_C_NO_CREDENTIAL;
db2int32 x = 0;
*ppErrorMsg = NULL;
*pErrorMsgLen = 0;
/* Token is not allocated by this plugin */
if( ppToken )
{
*ppToken = NULL;
}
/* Namespace is not used */
*userNamespaceLen = 0;
*userNamespaceType = DB2SEC_USER_NAMESPACE_UNDEFINED;
#if defined NAS_SUPPORT
/* There is a bug in the NAS code where if the cred_handle is set to
* GSS_C_NO_CREDENTIAL, NAS has a difficult time retreiving the correct
* credential handle if the application has kinit, kdestroy, and then kinit
* as another principal while the application is still up. The workaround is
* to explicitly grab the credential handle for the default login context and
* pass it in to the problematic function
*/
majorStatus = gss_acquire_cred( &minorStatus,
GSS_C_NO_NAME,
0,
GSS_C_NO_OID_SET,
GSS_C_INITIATE,
&credHandle,
NULL,
NULL );
if( majorStatus != GSS_S_COMPLETE )
{
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_acquire_cred" );
goto error;
}
#endif
/* Obtain the name associated with the default credential */
majorStatus = gss_inquire_cred( &minorStatus,
credHandle,
&gssName,
NULL, /* lifetime */
NULL, /* cred usage */
NULL ); /* mechanism */
if( majorStatus != GSS_S_COMPLETE )
{
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_inquire_cred" );
goto error;
}
/* Convert internal name into text format */
majorStatus = gss_display_name( &minorStatus, gssName, &name, NULL );
if( majorStatus != GSS_S_COMPLETE )
{
if( majorStatus == GSS_S_BAD_NAME )
{
rc = DB2SEC_PLUGIN_BADUSER;
}
else
{
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
}
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_display_name");
goto error;
}
/* Don't use complete principal name as the user name.
*
* */
*useridLen = (name.length < DB2SEC_MAX_USERID_LENGTH) ?
name.length : DB2SEC_MAX_USERID_LENGTH;
memcpy( userid, name.value, *useridLen );
for ( x = 0; x < *useridLen; x++ )
{
if ( userid[x] == '/' || userid[x] == '@' )
{
userid[x] = '\0';
*useridLen = x ;
break;
}
}
/* If the logged on userid contains any uppercase characters,
then the userid is automatically considered bad. This is because
all userids on UNIX/Linux are assumed to be completely in lowercase
or else it is impossible to uniquely map an AUTHID back to a userid
as is required for group lookups.
*/
for( x = 0; x < *useridLen; x++ )
{
if( userid[x] >= 'A' && userid[x] <= 'Z' )
{
rc = DB2SEC_PLUGIN_BADUSER;
goto error;
}
}
rc = mapPrincToAuthid( &name, authid, pAuthIdLen );
if( rc != DB2SEC_PLUGIN_OK )
{
goto error;
}
exit:
if( gssName != GSS_C_NO_NAME )
{
majorStatus = gss_release_name( &minorStatus, &gssName );
if( majorStatus != GSS_S_COMPLETE )
{
if( *pErrorMsgLen == 0 )
{
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_release_name");
}
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
}
}
if( name.length > 0 )
{
majorStatus = gss_release_buffer( &minorStatus, &name );
if( majorStatus != GSS_S_COMPLETE )
{
if( *pErrorMsgLen == 0 && rc == DB2SEC_PLUGIN_OK )
{
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_release_buffer");
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
}
}
}
if( credHandle != GSS_C_NO_CREDENTIAL )
{
majorStatus = gss_release_cred( &minorStatus, &credHandle );
if( majorStatus != GSS_S_COMPLETE )
{
if( *pErrorMsgLen == 0 && rc == DB2SEC_PLUGIN_OK )
{
getGSSErrorMsg( majorStatus, minorStatus, ppErrorMsg, pErrorMsgLen,
"gss_release_cred");
rc = mapGSSAPItoDB2SECerror( majorStatus, minorStatus );
}
}
}
return( rc );
error:
goto exit;
}
/******************************************************************************
*
* Function Name = db2secGenerateInitialCred
*
* Descriptive Name = Generate the intial credentials based on the provided
* username/password pair and return the GSS-API
* credentials handle.
*
* Function =
*
* Dependencies = None
*
* Restrictions = - A forwardable TGT will always be requested
* - No change password functionality provided
* - If a REALM is specified with the username, then it will
* be included in the userid buffer and not parsed into
* the userNamespace field
* - Certain krb5 objects must be created and exist for the
* GSS-API cred handle to be valid. To make sure that
* these krb5 objects are cleaned up properly, their
* handles will be stored in a structure pointed to by
* pInitInfo so that they may be freed during
* db2secFreeInitInfo().
*
* Input = userid - User name
* useridLen - Length of User name
* userNamespace - (unused)
* userNamespaceLen - (not used)
* userNamespaceType - (not used)
* password - User's password
* passwordLen - Password string length
* newPassword - (not used)
* newPasswordLen - (not used)
* dbName - (not used)
* dbnameLen - (not used)
*
* Output = pGSSCredHandle - Pointer to the GSS-API cred handle
* ppInitInfo - Pointer to a buffer allocated by the
* function to keep track of krb5 objects
* allocated to create the GSS-API cred handle
* ppErrorMsg - Pointer to a string that will contain
* an error message
* errorMsgLen - Pointer to the length of the error message
*
* Normal Return = DB2SEC_PLUGIN_OK
*
* Error Return = DB2SEC_PLUGIN_CHANGEPASSWORD_NOTSUPPORTED
* DB2SEC_PLUGIN_BAD_INPUT_PARAMETERS
* DB2SEC_PLUGIN_UNKNOWNERROR
* DB2SEC_PLUGIN_BADPWD
* DB2SEC_PLUGIN_BADUSER
* DB2SEC_PLUGIN_PWD_EXPIRED
* DB2SEC_PLUGIN_UID_EXPIRED
*
*******************************************************************************/
SQL_API_RC SQL_API_FN db2secGenerateInitialCred( const char *userid,
db2int32 useridLen,
const char *userNamespace,
db2int32 userNamespaceLen,
db2int32 userNamespaceType,
const char *password,
db2int32 passwordLen,
const char *newPassword,
db2int32 newPasswordLen,
const char *dbName,
db2int32 dbNameLen,
gss_cred_id_t *pGSSCredHandle,
void **ppInitInfo,
char **ppErrorMsg,
db2int32 *pErrorMsgLen )
{
SQL_API_RC rc = DB2SEC_PLUGIN_OK;
OM_uint32 majorStatus=GSS_S_COMPLETE;
OM_uint32 minorStatus;
int x = 0;
#if defined NAS_SUPPORT || defined MIT_KERBEROS_SUPPORT
krb5_data tgtname={ 0, KRB5_TGS_NAME_SIZE, KRB5_TGS_NAME };
krb5_context kContext=NULL;
krb5_error_code kStatus=0;
krb5_principal userPrinc=NULL;
krb5_principal server=NULL;
krb5_creds kCreds;
krb5_ccache cCache=NULL;
char cacheName[80];
char nameSeed[40]; // 10 (seconds) + 7 (useconds) + 19 (address)
krb5_int32 seconds;
krb5_int32 useconds;
int retryGrabTGT = 0;
krb5_get_init_creds_opt options;
const char* cache_old_name = NULL;
#elif defined SOLARIS_KERBEROS_SUPPORT
gss_name_t userGSSName;
gss_buffer_desc usernameBufferDesc = {0};
gss_buffer_desc passwordBufferDesc = {0};
#endif
*ppErrorMsg = NULL;
*pErrorMsgLen = 0;
if( newPasswordLen > 0 )
{
rc = DB2SEC_PLUGIN_CHANGEPASSWORD_NOTSUPPORTED;
goto exit;
}
if( !pGSSCredHandle )
{
rc = DB2SEC_PLUGIN_BAD_INPUT_PARAMETERS;
goto exit;
}
/* If the logged on userid contains any uppercase characters,
then the userid is automatically considered bad. This is because
all userids on UNIX/Linux are assumed to be completely in lowercase
or else it is impossible to uniquely map an AUTHID back to a userid
as is required for group lookups.
*/
for( x = 0; x < useridLen; x++ )
{
if ( userid[x] == '/' || userid[x] == '@' )
{
break;
}
if( userid[x] >= 'A' && userid[x] <= 'Z' )
{
rc = DB2SEC_PLUGIN_BADUSER;
goto error;
}
}
#if defined SOLARIS_KERBEROS_SUPPORT
passwordBufferDesc.value = (void *)password;
passwordBufferDesc.length = (size_t)passwordLen;
usernameBufferDesc.value = (void*)userid;