net_vampire: separate keytab code from samsync code.
[samba.git] / source3 / libnet / libnet_keytab.c
1 /*
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Guenther Deschner 2008.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23
24 #ifdef HAVE_KRB5
25
26 /****************************************************************
27 ****************************************************************/
28
29 static int keytab_close(struct libnet_keytab_context *ctx)
30 {
31         if (!ctx) {
32                 return 0;
33         }
34
35         if (ctx->keytab && ctx->context) {
36                 krb5_kt_close(ctx->context, ctx->keytab);
37         }
38
39         if (ctx->context) {
40                 krb5_free_context(ctx->context);
41         }
42
43         if (ctx->ads) {
44                 ads_destroy(&ctx->ads);
45         }
46
47         TALLOC_FREE(ctx);
48
49         return 0;
50 }
51
52 /****************************************************************
53 ****************************************************************/
54
55 krb5_error_code libnet_keytab_init(TALLOC_CTX *mem_ctx,
56                                    const char *keytab_name,
57                                    struct libnet_keytab_context **ctx)
58 {
59         krb5_error_code ret = 0;
60         krb5_context context = NULL;
61         krb5_keytab keytab = NULL;
62         const char *keytab_string = NULL;
63
64         struct libnet_keytab_context *r;
65
66         r = TALLOC_ZERO_P(mem_ctx, struct libnet_keytab_context);
67         if (!r) {
68                 return ENOMEM;
69         }
70
71         talloc_set_destructor(r, keytab_close);
72
73         initialize_krb5_error_table();
74         ret = krb5_init_context(&context);
75         if (ret) {
76                 DEBUG(1,("keytab_init: could not krb5_init_context: %s\n",
77                         error_message(ret)));
78                 return ret;
79         }
80
81         ret = smb_krb5_open_keytab(context, keytab_name, true, &keytab);
82         if (ret) {
83                 DEBUG(1,("keytab_init: smb_krb5_open_keytab failed (%s)\n",
84                         error_message(ret)));
85                 krb5_free_context(context);
86                 return ret;
87         }
88
89         ret = smb_krb5_keytab_name(mem_ctx, context, keytab, &keytab_string);
90         if (ret) {
91                 krb5_kt_close(context, keytab);
92                 krb5_free_context(context);
93                 return ret;
94         }
95
96         r->context = context;
97         r->keytab = keytab;
98         r->keytab_name = keytab_string;
99
100         *ctx = r;
101
102         return 0;
103 }
104
105 /****************************************************************
106 ****************************************************************/
107
108 krb5_error_code libnet_keytab_add(struct libnet_keytab_context *ctx)
109 {
110 #if defined(ENCTYPE_ARCFOUR_HMAC)
111         krb5_error_code ret = 0;
112         krb5_enctype enctypes[2] = { ENCTYPE_ARCFOUR_HMAC, 0 };
113         int i;
114
115         for (i=0; i<ctx->count; i++) {
116
117                 struct libnet_keytab_entry *entry = &ctx->entries[i];
118                 krb5_data password;
119
120                 password.data = (char *)entry->password.data;
121                 password.length = entry->password.length;
122
123                 ret = smb_krb5_kt_add_entry(ctx->context,
124                                             ctx->keytab,
125                                             entry->kvno,
126                                             entry->principal,
127                                             enctypes,
128                                             password,
129                                             true);
130                 if (ret) {
131                         DEBUG(1,("libnet_keytab_add: "
132                                 "Failed to add entry to keytab file\n"));
133                         return ret;
134                 }
135         }
136
137         return ret;
138 #else
139         return -1;
140 #endif /* defined(ENCTYPE_ARCFOUR_HMAC) */
141 }
142
143 #endif /* HAVE_KRB5 */