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
|
/*
* Authors:
* Karl MacMillan <kmacmill@redhat.com>
*
* Copyright (C) 2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE /* for asprintf */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define KADMIN_PATH "/usr/kerberos/sbin/kadmin.local"
struct options
{
char *princ_name;
char *realm;
int kstdin, kstdout, kstderr;
};
void *xmalloc(size_t size)
{
void *foo = malloc(size);
if (!foo) {
fprintf(stderr, "malloc error of size %jd\n", size);
exit(1);
}
memset(foo, 0, size);
return foo;
}
void usage(void)
{
printf("ipa-keytab-util princ-name realm-name\n");
}
struct options *process_args(int argc, char **argv)
{
struct options* opts;
opts = xmalloc(sizeof(struct options));
if (argc != 3) {
usage();
exit(1);
}
opts->princ_name = argv[1];
opts->realm = argv[2];
return opts;
}
void drop_caps(void)
{
cap_t caps;
int ret;
if (geteuid() != 0)
return;
if (getuid() != 0)
return;
caps = cap_init();
if (!caps) {
perror("error initializing caps");
exit(1);
}
ret = cap_clear(caps);
if (ret != 0) {
perror("could not clear capps");
exit(1);
}
ret = cap_set_proc(caps);
if (ret != 0) {
perror("could not drop caps");
exit(1);
}
cap_free(caps);
}
pid_t exec_kadmin_local(struct options *opts)
{
int stdin_pipes[2];
int stdout_pipes[2];
int stderr_pipes[2];
int ret;
pid_t chpid;
char *princ;
/* create a pair of pipes for stdin / stdout
of the child process.
*/
if (pipe(stdin_pipes) == -1) {
perror("creating stdin");
exit(1);
}
if (pipe(stdout_pipes) == -1) {
perror("creating stdin");
exit(1);
}
if (pipe(stderr_pipes) == -1) {
perror("creating stdin");
exit(1);
}
chpid = fork();
if (chpid == -1) {
perror("fork");
exit(1);
}
/* CHILD */
if (chpid == 0) {
/* stdin */
close(stdin_pipes[1]);
dup2(stdin_pipes[0], 0);
/* stdout */
close(stdout_pipes[0]);
dup2(stdout_pipes[1], 1);
/* stderr */
close(stderr_pipes[0]);
dup2(stdout_pipes[1], 2);
/* now exec kadmin.local */
ret = asprintf(&princ, "admin@%s", opts->realm);
if (!princ) {
perror("creating bind princ");
exit(1);
}
ret = execl(KADMIN_PATH, "kadmin.local", "-p", princ, NULL);
free(princ);
if (ret == -1) {
perror("exec");
exit(1);
}
} else {
close(stdin_pipes[0]);
close(stdout_pipes[1]);
close(stderr_pipes[1]);
opts->kstdin = stdin_pipes[1];
opts->kstdout = stdout_pipes[0];
opts->kstderr = stdout_pipes[0];
}
return chpid;
}
void write_to_kadmin(struct options *opts, char *buf, int len)
{
int ret;
ret = write(opts->kstdin, buf, len);
if (ret != len) {
perror("write");
fprintf(stderr, "write is short %d:%d\n", len, ret);
exit(1);
}
fsync(opts->kstdin);
}
char *get_temp_filename(void)
{
char *fname;
/* ok - we have to use mktemp here even w/ the race
* because kadmin.local barfs if the file exists. The
* risk is pretty low and we will try to protect the files
* with selinux.
*
* TODO: generate these files in a safer place than /tmp
*/
fname = strdup("/tmp/ipa-keytab-util-XXXXXX");
if (!fname) {
fprintf(stderr, "could not allocate temporary file name");
exit(1);
}
fname = mktemp(fname);
return fname;
}
char *create_keytab(struct options *opts)
{
char *buf, *fname;
int ret;
fname = get_temp_filename();
ret = asprintf(&buf, "ktadd -k %s %s\n", fname, opts->princ_name);
if (ret == -1) {
perror("asprintf");
exit(1);
}
write_to_kadmin(opts, buf, ret);
free(buf);
write_to_kadmin(opts, "quit\n", sizeof("quit\n"));
return fname;
}
void read_keytab(char *fname)
{
FILE *fd;
char *data;
long flen, ret;
fd = fopen(fname, "r");
if (!fd) {
fprintf(stderr, "could not open file %s: ", fname);
perror(NULL);
exit(1);
}
fseek(fd, 0, SEEK_END);
flen = ftell(fd);
rewind(fd);
data = xmalloc(flen);
/* TODO: handle short reads */
ret = fread(data, 1, flen, fd);
if (ret != flen) {
fprintf(stderr, "short read");
exit(1);
}
fclose(fd);
/* write to stdout */
ret = fwrite(data, 1, flen, stdout);
if (ret != flen) {
fprintf(stderr, "short write");
exit(1);
}
}
void remove_keytab(char *filename)
{
unlink(filename);
}
/* TODO: add significantly better authorization */
int main(int argc, char **argv)
{
struct options *opts;
pid_t chpid;
int status, ret;
char *fname;
opts = process_args(argc, argv);
/* must really be root */
setuid(0);
drop_caps();
chpid = exec_kadmin_local(opts);
fname = create_keytab(opts);
ret = waitpid(-1, &status, 0);
if (WEXITSTATUS(status)) {
fprintf(stderr, "error creating keytab\n");
exit(1);
}
read_keytab(fname);
remove_keytab(fname);
return 0;
}
|