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