2 Unix SMB/CIFS implementation.
3 dump the remote SAM using rpc samsync operations
5 Copyright (C) Guenther Deschner 2008.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libnet/libnet.h"
26 /****************************************************************
27 ****************************************************************/
29 static int keytab_close(struct libnet_keytab_context
*ctx
)
35 if (ctx
->keytab
&& ctx
->context
) {
36 krb5_kt_close(ctx
->context
, ctx
->keytab
);
40 krb5_free_context(ctx
->context
);
44 ads_destroy(&ctx
->ads
);
52 /****************************************************************
53 ****************************************************************/
55 krb5_error_code
libnet_keytab_init(TALLOC_CTX
*mem_ctx
,
56 const char *keytab_name
,
57 struct libnet_keytab_context
**ctx
)
59 krb5_error_code ret
= 0;
60 krb5_context context
= NULL
;
61 krb5_keytab keytab
= NULL
;
62 const char *keytab_string
= NULL
;
64 struct libnet_keytab_context
*r
;
66 r
= TALLOC_ZERO_P(mem_ctx
, struct libnet_keytab_context
);
71 talloc_set_destructor(r
, keytab_close
);
73 initialize_krb5_error_table();
74 ret
= krb5_init_context(&context
);
76 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
81 ret
= smb_krb5_open_keytab(context
, keytab_name
, true, &keytab
);
83 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
85 krb5_free_context(context
);
89 ret
= smb_krb5_keytab_name(mem_ctx
, context
, keytab
, &keytab_string
);
91 krb5_kt_close(context
, keytab
);
92 krb5_free_context(context
);
98 r
->keytab_name
= keytab_string
;
105 /****************************************************************
106 ****************************************************************/
108 krb5_error_code
libnet_keytab_add(struct libnet_keytab_context
*ctx
)
110 #if defined(ENCTYPE_ARCFOUR_HMAC)
111 krb5_error_code ret
= 0;
112 krb5_enctype enctypes
[2] = { ENCTYPE_ARCFOUR_HMAC
, 0 };
115 for (i
=0; i
<ctx
->count
; i
++) {
117 struct libnet_keytab_entry
*entry
= &ctx
->entries
[i
];
120 password
.data
= (char *)entry
->password
.data
;
121 password
.length
= entry
->password
.length
;
123 ret
= smb_krb5_kt_add_entry_ext(ctx
->context
,
132 DEBUG(1,("libnet_keytab_add: "
133 "Failed to add entry to keytab file\n"));
141 #endif /* defined(ENCTYPE_ARCFOUR_HMAC) */
144 struct libnet_keytab_entry
*libnet_keytab_search(struct libnet_keytab_context
*ctx
,
145 const char *principal
, int kvno
,
146 const krb5_enctype enctype
,
149 krb5_error_code ret
= 0;
150 krb5_kt_cursor cursor
;
151 krb5_keytab_entry kt_entry
;
152 struct libnet_keytab_entry
*entry
= NULL
;
154 ZERO_STRUCT(kt_entry
);
157 ret
= krb5_kt_start_seq_get(ctx
->context
, ctx
->keytab
, &cursor
);
162 while (krb5_kt_next_entry(ctx
->context
, ctx
->keytab
, &kt_entry
, &cursor
) == 0) {
163 char *princ_s
= NULL
;
165 if (kt_entry
.vno
!= kvno
) {
166 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
170 if (kt_entry
.key
.enctype
!= enctype
) {
171 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
175 ret
= smb_krb5_unparse_name(ctx
->context
, kt_entry
.principal
, &princ_s
);
177 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
181 if (strcmp(principal
, princ_s
) != 0) {
182 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
187 entry
= talloc_zero(mem_ctx
, struct libnet_keytab_entry
);
189 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
194 entry
->name
= talloc_strdup(entry
, princ_s
);
196 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
202 entry
->principal
= talloc_strdup(entry
, princ_s
);
203 if (!entry
->principal
) {
204 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
210 entry
->password
= data_blob_talloc(entry
, kt_entry
.key
.contents
, kt_entry
.key
.length
);
211 if (!entry
->password
.data
) {
212 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
218 smb_krb5_kt_free_entry(ctx
->context
, &kt_entry
);
223 krb5_kt_end_seq_get(ctx
->context
, ctx
->keytab
, &cursor
);
227 #endif /* HAVE_KRB5 */