summaryrefslogtreecommitdiff
path: root/kerberosIV/kpropd/kpropd.c
blob: eee0f4e23bd9db3cbd4e49333f9f661a3920e7f4 (plain)
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
/*      $OpenBSD: kpropd.c,v 1.8 2000/01/12 19:54:47 ericj Exp $  */
/* $KTH: kpropd.c,v 2.24 1997/12/05 01:34:57 assar Exp $ */

/*
 * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the Kungliga Tekniska
 *      Högskolan and its contributors.
 * 
 * 4. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <errno.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <sys/file.h>
#include <sys/wait.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <err.h>


#include <kerberosIV/krb.h>
#include <kerberosIV/krb_db.h>
#include <klog.h>
#include <kerberosIV/prot.h>
#include <kdc.h>

#include <krb_log.h>

#include "kprop.h"

#ifndef SBINDIR
#define SBINDIR "/usr/sbin"
#endif

struct sockaddr_in master, slave;

char *database = DBM_FILE;

char *lockfile = DB_DIR "/slave_propagation";

char *logfile = K_LOGFIL;

char *kdb_util = SBINDIR "/kdb_util";

char *kdb_util_command = "load";

char *srvtab = "";

char realm[REALM_SZ];

static
int
copy_data(int from, int to, des_cblock *session, des_key_schedule schedule)
{
    unsigned char tmp[4];
    char buf[KPROP_BUFSIZ + 26];
    u_int32_t length;
    int n;
    
    int kerr;
    MSG_DAT m;

    while(1){
	n = krb_net_read(from, tmp, 4);
	if(n == 0)
	    break;
	if(n < 0){
	    klog(L_KRB_PERR, "krb_net_read: %s", strerror(errno));
	    return -1;
	}
	if(n != 4){
	    klog(L_KRB_PERR, "Premature end of data");
	    return -1;
	}
	length = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
	if(length > sizeof(buf)){
	    klog(L_KRB_PERR, "Giant packet received: %d", length);
	    return -1;
	}
	if(krb_net_read(from, buf, length) != length){
	    klog(L_KRB_PERR, "Premature end of data");
	    return -1;
	}
	kerr = krb_rd_priv (buf, length, schedule, session, &master, &slave, &m);
	if(kerr != KSUCCESS){
	    klog(L_KRB_PERR, "Kerberos error: %s", krb_get_err_text(kerr));
	    return -1;
	}
	write(to, m.app_data, m.app_length);
    }
    return 0;
}


static
int
kprop(int s)
{
    char buf[128];
    int n;
    KTEXT_ST ticket;
    AUTH_DAT ad;
    char sinst[INST_SZ];
    char command[1024];
    des_key_schedule schedule;
    int mode;
    int kerr;
    int lock;
    
    n = sizeof(master);
    if(getpeername(s, (struct sockaddr*)&master, &n) < 0){
	klog(L_KRB_PERR, "getpeername: %s", strerror(errno));
	return 1;
    }
    
    n = sizeof(slave);
    if(getsockname(s, (struct sockaddr*)&slave, &n) < 0){
	klog(L_KRB_PERR, "getsockname: %s", strerror(errno));
	return 1;
    }

    klog(L_KRB_PERR, "Connection from %s", inet_ntoa(master.sin_addr));

    n = krb_net_read(s, buf, KPROP_PROT_VERSION_LEN + 2);
    if(n < KPROP_PROT_VERSION_LEN + 2){
	klog(L_KRB_PERR, "Premature end of data");
	return 1;
    }
    if(memcmp(buf, KPROP_PROT_VERSION, KPROP_PROT_VERSION_LEN) != 0){
	klog(L_KRB_PERR, "Bad protocol version string received");
	return 1;
    }
    mode = (buf[n-2] << 8) | buf[n-1];
    if(mode != KPROP_TRANSFER_PRIVATE){
	klog(L_KRB_PERR, "Bad transfer mode received: %d", mode);
	return 1;
    }
    k_getsockinst(s, sinst, sizeof(sinst));
    kerr = krb_recvauth(KOPT_DO_MUTUAL, s, &ticket,
			KPROP_SERVICE_NAME, sinst,
			&master, &slave,
			&ad, srvtab, schedule, 
			buf);
    if(kerr != KSUCCESS){
	klog(L_KRB_PERR, "Kerberos error: %s", krb_get_err_text(kerr));
	return 1;
    }

    if(strcmp(ad.pname, KPROP_SERVICE_NAME) ||
#if 0
       strcmp(ad.pinst, /* XXX remote host */) ||
#else
       strcmp(ad.pinst, KRB_MASTER) ||
#endif
       strcmp(ad.prealm, realm)){
	klog(L_KRB_PERR, "Connection from unauthorized client: %s", 
	     krb_unparse_name_long(ad.pname, ad.pinst, ad.prealm));
	return 1;
    }

    des_set_key(&ad.session, schedule);
    
    lock = open(lockfile, O_WRONLY|O_CREAT, 0600);
    if(lock < 0){
	klog(L_KRB_PERR, "Failed to open file: %s", strerror(errno));
	return 1;
    }
    if(flock(lock, K_LOCK_EX | K_LOCK_NB)){
	close(lock);
	klog(L_KRB_PERR, "Failed to lock file: %s", strerror(errno));
	return 1;
    }
    
    if(ftruncate(lock, 0) < 0){
	close(lock);
	klog(L_KRB_PERR, "Failed to lock file: %s", strerror(errno));
	return 1;
    }

    if(copy_data(s, lock, &ad.session, schedule)){
	close(lock);
	return 1;
    }
    close(lock);
    snprintf(command, sizeof(command),
	     "%s %s %s %s", kdb_util, kdb_util_command, 
	     lockfile, database);
    if(system(command) == 0){
	klog(L_KRB_PERR, "Propagation finished successfully");
	return 0;
    }
    klog(L_KRB_PERR, "*** Propagation failed ***");
    return 1;
}

static int
doit(void)
{
    return kprop(0);
}

static int
doit_interactive(void)
{
    struct sockaddr_in sa;
    int salen;
    int s, s2;
    int ret;

    s = socket(AF_INET, SOCK_STREAM, 0);
    if(s < 0){
	klog(L_KRB_PERR, "socket: %s", strerror(errno));
	return 1;
    }
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = k_getportbyname ("krb_prop", "tcp", htons(KPROP_PORT));
    ret = bind(s, (struct sockaddr*)&sa, sizeof(sa));
    if (ret < 0) {
	klog(L_KRB_PERR, "bind: %s", strerror(errno));
	return 1;
    }
    ret = listen(s, SOMAXCONN);
    if (ret < 0) {
	klog(L_KRB_PERR, "listen: %s", strerror(errno));
	return 1;
    }
    for(;;) {
	salen = sizeof(sa);
	s2 = accept(s, (struct sockaddr*)&sa, &salen);
	switch(fork()){
	case -1:
	    klog(L_KRB_PERR, "fork: %s", strerror(errno));
	    return 1;
	case 0:
	    close(s);
	    kprop(s2);
	    return 1;
	default: {
		int status;
		close(s2);
		wait(&status);
	    }
	}
    }
}

static void
usage (void)
{
     fprintf (stderr, 
	      "Usage: kpropd [-i] [-d database] [-l log] [-m] [-[p|P] program]"
	      " [-r realm] [-s srvtab]\n");
     exit (1);
}

int
main(int argc, char **argv)
{
    int opt;
    int interactive = 0;

    krb_get_lrealm(realm, 1);
    
    while((opt = getopt(argc, argv, ":d:l:mp:P:r:s:i")) != -1){
	switch(opt){
	case 'd':
	    database = optarg;
	    break;
	case 'l':
	    logfile = optarg;
	    break;
	case 'm':
	    kdb_util_command = "merge";
	    break;
	case 'p':
	case 'P':
	    kdb_util = optarg;
	    break;
	case 'r':
	    strncpy(realm, optarg, sizeof(realm));
	    break;
	case 's':
	    srvtab = optarg;
	    break;
	case 'i':
	    interactive = 1;
	    break;
	default:
	    klog(L_KRB_PERR, "Bad option: -%c", optopt);
	    usage ();
	    exit(1);
	}
    }
    kset_logfile(logfile);
    if (interactive)
	return doit_interactive ();
    else
	return doit ();
}