-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh_db.c
205 lines (164 loc) · 4.26 KB
/
h_db.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
/*
* k5_db.c
*
* Deal with kerberos database.
*
* $Id: h_db.c,v 1.3 2015/09/11 18:12:59 kouril Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <krb5.h>
#include <hdb.h>
#include <sys/fcntl.h>
#include <com_err.h>
#include "h_db.h"
char k5_db_error[1024] = "No Error";
static struct hdb_dbinfo *db_info;
static void
format_db_error(krb5_context context, int ret, const char *format, ...)
{
va_list ap;
const char *msg = krb5_get_error_message(context, ret);
va_start(ap, format);
vsnprintf(k5_db_error, sizeof(k5_db_error), format, ap);
va_end(ap);
strncat(k5_db_error, ": ", sizeof(k5_db_error) - strlen(k5_db_error) - 1);
strncat(k5_db_error, msg, sizeof(k5_db_error) - strlen(k5_db_error) - 1);
return;
}
int
hdb_init_info(krb5_context context, const char *kdc_conf_file)
{
krb5_error_code ret;
char **filelist = NULL;
krb5_context kdc_context = NULL;
ret = krb5_copy_context(context, &kdc_context);
if (ret) {
format_db_error(context, ret, "hdb_init: krb5_copy_context() failed");
return -1;
}
krb5_prepend_config_files(kdc_conf_file, NULL, &filelist);
if (ret) {
format_db_error(context, ret, "hdb_init: krb5_prepend_config_files() failed");
goto end;
}
ret = krb5_set_config_files(kdc_context, filelist);
if (ret) {
format_db_error(context, ret, "hdb_init: krb5_set_config_files() fauled");
goto end;
}
ret = hdb_get_dbinfo(kdc_context, &db_info);
if (ret) {
format_db_error(context, ret, "hdb_init: hdb_get_dbinfo() failed");
goto end;
}
end:
if (ret) {
if (db_info)
hdb_free_dbinfo(kdc_context, &db_info);
db_info = NULL;
}
if (filelist)
krb5_free_config_files(filelist);
krb5_free_context(kdc_context);
return (ret == 0) ? 0 : -1;
}
void
hdb_close_info(krb5_context context)
{
/* the context is different from what was used for the init but it should be harmless */
hdb_free_dbinfo(context, &db_info);
}
krb5_error_code
hdb_get_key(krb5_context context, krb5_principal princ, krb5_keyblock ** key, krb5_enctype ktype)
{
hdb_entry_ex entry;
krb5_error_code ret;
Key *k;
memset(&entry, 0, sizeof(entry));
*key = (krb5_keyblock *) malloc(sizeof(krb5_keyblock));
if (*key == NULL) {
snprintf(k5_db_error, sizeof(k5_db_error), "malloc failed in hdb_get_key()");
return -1;
}
ret = hdb_get_entry(context, princ, &entry);
if (ret)
return (ret);
ret = hdb_enctype2key(context, &entry.entry, NULL, ktype, &k);
if (ret) {
format_db_error(context, ret, "hdb_enctype2key() failed");
return (ret);
};
copy_EncryptionKey(&k->key, *key);
free_hdb_entry(&entry.entry);
return (0);
}
static krb5_error_code
create_db_handle(krb5_context context, struct hdb_dbinfo *info, krb5_realm realm, struct HDB **out)
{
const char *mkey;
krb5_error_code ret;
struct hdb_dbinfo *di;
struct HDB *db = NULL;
di = NULL;
while ((di = hdb_dbinfo_get_next(info, di)) != NULL) {
if (strcmp(hdb_dbinfo_get_realm(context, di), realm) == 0)
break;
}
if (di == NULL) {
snprintf(k5_db_error, sizeof(k5_db_error), "No database available for realm %s", realm);
return -1;
}
ret = hdb_create(context, &db, hdb_dbinfo_get_dbname(context, di));
if (ret) {
format_db_error(context, ret, "hdb_create() failed");
return -1;
}
mkey = hdb_dbinfo_get_mkey_file(context, di);
if (mkey) {
ret = hdb_set_master_keyfile(context, db, mkey);
if (ret) {
format_db_error(context, ret, "hdb_set_master_keyfile() failed");
goto end;
}
}
*out = db;
db = NULL;
ret = 0;
end:
if (db)
db->hdb_destroy(context, db);
return ret;
}
krb5_error_code
hdb_get_entry(krb5_context context, krb5_principal princ, krb5_db_entry * entry)
{
krb5_error_code ret;
struct HDB *db = NULL;
if (db_info == NULL) {
snprintf(k5_db_error, sizeof(k5_db_error), "Database info not initialized");
return -1;
}
ret = create_db_handle(context, db_info, princ->realm, &db);
if (ret)
return ret;
ret = db->hdb_open(context, db, O_RDONLY, 0);
if (ret) {
format_db_error(context, ret, "Failed to open database");
goto end;
};
ret = db->hdb_fetch_kvno(context, db, princ, HDB_F_DECRYPT, 0, entry);
db->hdb_close(context, db);
if (ret) {
format_db_error(context, ret, "Error fetching principal");
goto end;
};
ret = 0;
end:
db->hdb_destroy(context, db);
return ret;
}