xref: /titanic_44/usr/src/cmd/ssh/libssh/common/radix.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Dug Song.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2002 Markus Friedl.  All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
6*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
7*7c478bd9Sstevel@tonic-gate  * are met:
8*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
9*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
10*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
11*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
12*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include "includes.h"
27*7c478bd9Sstevel@tonic-gate #include "uuencode.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: radix.c,v 1.22 2002/09/09 14:54:15 markus Exp $");
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #ifdef AFS
34*7c478bd9Sstevel@tonic-gate #include <krb.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <radix.h>
37*7c478bd9Sstevel@tonic-gate #include "bufaux.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate int
creds_to_radix(CREDENTIALS * creds,u_char * buf,size_t buflen)40*7c478bd9Sstevel@tonic-gate creds_to_radix(CREDENTIALS *creds, u_char *buf, size_t buflen)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	Buffer b;
43*7c478bd9Sstevel@tonic-gate 	int ret;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 	buffer_init(&b);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, 1);	/* version */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->service, strlen(creds->service));
50*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
51*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->instance, strlen(creds->instance));
52*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
53*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->realm, strlen(creds->realm));
54*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
55*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->pname, strlen(creds->pname));
56*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
57*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->pinst, strlen(creds->pinst));
58*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	/* Null string to repeat the realm. */
61*7c478bd9Sstevel@tonic-gate 	buffer_put_char(&b, '\0');
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	buffer_put_int(&b, creds->issue_date);
64*7c478bd9Sstevel@tonic-gate 	buffer_put_int(&b, krb_life_to_time(creds->issue_date,
65*7c478bd9Sstevel@tonic-gate 	    creds->lifetime));
66*7c478bd9Sstevel@tonic-gate 	buffer_append(&b, creds->session, sizeof(creds->session));
67*7c478bd9Sstevel@tonic-gate 	buffer_put_short(&b, creds->kvno);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/* 32 bit size + data */
70*7c478bd9Sstevel@tonic-gate 	buffer_put_string(&b, creds->ticket_st.dat, creds->ticket_st.length);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	ret = uuencode(buffer_ptr(&b), buffer_len(&b), (char *)buf, buflen);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	buffer_free(&b);
75*7c478bd9Sstevel@tonic-gate 	return ret;
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define GETSTRING(b, t, tlen) \
79*7c478bd9Sstevel@tonic-gate 	do { \
80*7c478bd9Sstevel@tonic-gate 		int i, found = 0; \
81*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < tlen; i++) { \
82*7c478bd9Sstevel@tonic-gate 			if (buffer_len(b) == 0) \
83*7c478bd9Sstevel@tonic-gate 				goto done; \
84*7c478bd9Sstevel@tonic-gate 			t[i] = buffer_get_char(b); \
85*7c478bd9Sstevel@tonic-gate 			if (t[i] == '\0') { \
86*7c478bd9Sstevel@tonic-gate 				found = 1; \
87*7c478bd9Sstevel@tonic-gate 				break; \
88*7c478bd9Sstevel@tonic-gate 			} \
89*7c478bd9Sstevel@tonic-gate 		} \
90*7c478bd9Sstevel@tonic-gate 		if (!found) \
91*7c478bd9Sstevel@tonic-gate 			goto done; \
92*7c478bd9Sstevel@tonic-gate 	} while(0)
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate int
radix_to_creds(const char * buf,CREDENTIALS * creds)95*7c478bd9Sstevel@tonic-gate radix_to_creds(const char *buf, CREDENTIALS *creds)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	Buffer b;
98*7c478bd9Sstevel@tonic-gate 	u_char *space;
99*7c478bd9Sstevel@tonic-gate 	char c, version, *p;
100*7c478bd9Sstevel@tonic-gate 	u_int endTime, len;
101*7c478bd9Sstevel@tonic-gate 	int blen, ret;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	ret = 0;
104*7c478bd9Sstevel@tonic-gate 	blen = strlen(buf);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	/* sanity check for size */
107*7c478bd9Sstevel@tonic-gate 	if (blen > 8192)
108*7c478bd9Sstevel@tonic-gate 		return 0;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	buffer_init(&b);
111*7c478bd9Sstevel@tonic-gate 	space = buffer_append_space(&b, blen);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	/* check version and length! */
114*7c478bd9Sstevel@tonic-gate 	len = uudecode(buf, space, blen);
115*7c478bd9Sstevel@tonic-gate 	if (len < 1)
116*7c478bd9Sstevel@tonic-gate 		goto done;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	version = buffer_get_char(&b);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	GETSTRING(&b, creds->service, sizeof creds->service);
121*7c478bd9Sstevel@tonic-gate 	GETSTRING(&b, creds->instance, sizeof creds->instance);
122*7c478bd9Sstevel@tonic-gate 	GETSTRING(&b, creds->realm, sizeof creds->realm);
123*7c478bd9Sstevel@tonic-gate 	GETSTRING(&b, creds->pname, sizeof creds->pname);
124*7c478bd9Sstevel@tonic-gate 	GETSTRING(&b, creds->pinst, sizeof creds->pinst);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (buffer_len(&b) == 0)
127*7c478bd9Sstevel@tonic-gate 		goto done;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	/* Ignore possibly different realm. */
130*7c478bd9Sstevel@tonic-gate 	while (buffer_len(&b) > 0 && (c = buffer_get_char(&b)) != '\0')
131*7c478bd9Sstevel@tonic-gate 		;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (buffer_len(&b) == 0)
134*7c478bd9Sstevel@tonic-gate 		goto done;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	creds->issue_date = buffer_get_int(&b);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	endTime = buffer_get_int(&b);
139*7c478bd9Sstevel@tonic-gate 	creds->lifetime = krb_time_to_life(creds->issue_date, endTime);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	len = buffer_len(&b);
142*7c478bd9Sstevel@tonic-gate 	if (len < sizeof(creds->session))
143*7c478bd9Sstevel@tonic-gate 		goto done;
144*7c478bd9Sstevel@tonic-gate 	memcpy(&creds->session, buffer_ptr(&b), sizeof(creds->session));
145*7c478bd9Sstevel@tonic-gate 	buffer_consume(&b, sizeof(creds->session));
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	creds->kvno = buffer_get_short(&b);
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	p = buffer_get_string(&b, &len);
150*7c478bd9Sstevel@tonic-gate 	if (len < 0 || len > sizeof(creds->ticket_st.dat))
151*7c478bd9Sstevel@tonic-gate 		goto done;
152*7c478bd9Sstevel@tonic-gate 	memcpy(&creds->ticket_st.dat, p, len);
153*7c478bd9Sstevel@tonic-gate 	creds->ticket_st.length = len;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	ret = 1;
156*7c478bd9Sstevel@tonic-gate done:
157*7c478bd9Sstevel@tonic-gate 	buffer_free(&b);
158*7c478bd9Sstevel@tonic-gate 	return ret;
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate #endif /* AFS */
161