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