blob: 580e7e07fa6a72da3829731a2aca5040b7912ea7 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * Copyright (c) 1999 Dug Song. All rights reserved.
Damien Millerbad0e012002-04-23 20:46:56 +10003 * Copyright (c) 2002 Markus Friedl. All rights reserved.
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Millere4340be2000-09-16 13:29:08 +11005 * 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.
Damien Miller95def091999-11-25 00:26:21 +110024 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100027#include "uuencode.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000029RCSID("$OpenBSD: radix.c,v 1.21 2002/06/19 00:27:55 deraadt Exp $");
Damien Millerbf7f4662000-06-23 10:16:38 +100030
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031#ifdef AFS
32#include <krb.h>
33
Ben Lindstrombba81212001-06-25 05:01:22 +000034#include <radix.h>
Damien Millerbad0e012002-04-23 20:46:56 +100035#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
Damien Miller4af51302000-04-16 11:18:38 +100037int
Ben Lindstrom46c16222000-12-22 01:43:59 +000038creds_to_radix(CREDENTIALS *creds, u_char *buf, size_t buflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
Damien Millerbad0e012002-04-23 20:46:56 +100040 Buffer b;
41 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Damien Millerbad0e012002-04-23 20:46:56 +100043 buffer_init(&b);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Millerbad0e012002-04-23 20:46:56 +100045 buffer_put_char(&b, 1); /* version */
46
47 buffer_append(&b, creds->service, strlen(creds->service));
48 buffer_put_char(&b, '\0');
49 buffer_append(&b, creds->instance, strlen(creds->instance));
50 buffer_put_char(&b, '\0');
51 buffer_append(&b, creds->realm, strlen(creds->realm));
52 buffer_put_char(&b, '\0');
53 buffer_append(&b, creds->pname, strlen(creds->pname));
54 buffer_put_char(&b, '\0');
55 buffer_append(&b, creds->pinst, strlen(creds->pinst));
56 buffer_put_char(&b, '\0');
57
Damien Miller95def091999-11-25 00:26:21 +110058 /* Null string to repeat the realm. */
Damien Millerbad0e012002-04-23 20:46:56 +100059 buffer_put_char(&b, '\0');
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Millerbad0e012002-04-23 20:46:56 +100061 buffer_put_int(&b, creds->issue_date);
62 buffer_put_int(&b, krb_life_to_time(creds->issue_date,
63 creds->lifetime));
64 buffer_append(&b, creds->session, sizeof(creds->session));
65 buffer_put_short(&b, creds->kvno);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Millerbad0e012002-04-23 20:46:56 +100067 /* 32 bit size + data */
Markus Friedl435673d2002-04-23 16:35:15 +020068 buffer_put_string(&b, creds->ticket_st.dat, creds->ticket_st.length);
Damien Miller95def091999-11-25 00:26:21 +110069
Damien Millerbad0e012002-04-23 20:46:56 +100070 ret = uuencode(buffer_ptr(&b), buffer_len(&b), (char *)buf, buflen);
Damien Miller95def091999-11-25 00:26:21 +110071
Damien Millerbad0e012002-04-23 20:46:56 +100072 buffer_free(&b);
73 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074}
75
Damien Millerbad0e012002-04-23 20:46:56 +100076#define GETSTRING(b, t, tlen) \
77 do { \
Damien Miller635fe982002-04-23 21:00:33 +100078 int i, found = 0; \
Damien Millerbad0e012002-04-23 20:46:56 +100079 for (i = 0; i < tlen; i++) { \
80 if (buffer_len(b) == 0) \
81 goto done; \
82 t[i] = buffer_get_char(b); \
Damien Miller635fe982002-04-23 21:00:33 +100083 if (t[i] == '\0') { \
84 found = 1; \
Damien Millerbad0e012002-04-23 20:46:56 +100085 break; \
Damien Miller635fe982002-04-23 21:00:33 +100086 } \
Damien Millerbad0e012002-04-23 20:46:56 +100087 } \
Damien Miller635fe982002-04-23 21:00:33 +100088 if (!found) \
Damien Millerbad0e012002-04-23 20:46:56 +100089 goto done; \
90 } while(0)
91
Damien Miller4af51302000-04-16 11:18:38 +100092int
Damien Miller95def091999-11-25 00:26:21 +110093radix_to_creds(const char *buf, CREDENTIALS *creds)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094{
Damien Millerbad0e012002-04-23 20:46:56 +100095 Buffer b;
96 char c, version, *space, *p;
97 u_int endTime;
98 int len, blen, ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Millerbad0e012002-04-23 20:46:56 +1000100 ret = 0;
101 blen = strlen(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Millerbad0e012002-04-23 20:46:56 +1000103 /* sanity check for size */
104 if (blen > 8192)
Damien Miller95def091999-11-25 00:26:21 +1100105 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Millerbad0e012002-04-23 20:46:56 +1000107 buffer_init(&b);
108 space = buffer_append_space(&b, blen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller95def091999-11-25 00:26:21 +1100110 /* check version and length! */
Damien Millerbad0e012002-04-23 20:46:56 +1000111 len = uudecode(buf, space, blen);
Damien Miller95def091999-11-25 00:26:21 +1100112 if (len < 1)
Damien Millerbad0e012002-04-23 20:46:56 +1000113 goto done;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Millerbad0e012002-04-23 20:46:56 +1000115 version = buffer_get_char(&b);
Damien Miller95def091999-11-25 00:26:21 +1100116
Damien Millerbad0e012002-04-23 20:46:56 +1000117 GETSTRING(&b, creds->service, sizeof creds->service);
118 GETSTRING(&b, creds->instance, sizeof creds->instance);
119 GETSTRING(&b, creds->realm, sizeof creds->realm);
120 GETSTRING(&b, creds->pname, sizeof creds->pname);
121 GETSTRING(&b, creds->pinst, sizeof creds->pinst);
122
123 if (buffer_len(&b) == 0)
124 goto done;
125
Damien Miller95def091999-11-25 00:26:21 +1100126 /* Ignore possibly different realm. */
Damien Millerbad0e012002-04-23 20:46:56 +1000127 while (buffer_len(&b) > 0 && (c = buffer_get_char(&b)) != '\0')
128 ;
Damien Miller95def091999-11-25 00:26:21 +1100129
Damien Millerbad0e012002-04-23 20:46:56 +1000130 if (buffer_len(&b) == 0)
131 goto done;
Damien Miller95def091999-11-25 00:26:21 +1100132
Damien Millerbad0e012002-04-23 20:46:56 +1000133 creds->issue_date = buffer_get_int(&b);
Damien Miller95def091999-11-25 00:26:21 +1100134
Damien Millerbad0e012002-04-23 20:46:56 +1000135 endTime = buffer_get_int(&b);
136 creds->lifetime = krb_time_to_life(creds->issue_date, endTime);
Damien Miller95def091999-11-25 00:26:21 +1100137
Damien Millerbad0e012002-04-23 20:46:56 +1000138 len = buffer_len(&b);
139 if (len < sizeof(creds->session))
140 goto done;
141 memcpy(&creds->session, buffer_ptr(&b), sizeof(creds->session));
142 buffer_consume(&b, sizeof(creds->session));
Damien Miller95def091999-11-25 00:26:21 +1100143
Damien Millerbad0e012002-04-23 20:46:56 +1000144 creds->kvno = buffer_get_short(&b);
Damien Miller95def091999-11-25 00:26:21 +1100145
Damien Millerbad0e012002-04-23 20:46:56 +1000146 p = buffer_get_string(&b, &len);
147 if (len < 0 || len > sizeof(creds->ticket_st.dat))
148 goto done;
149 memcpy(&creds->ticket_st.dat, p, len);
150 creds->ticket_st.length = len;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000151
Damien Millerbad0e012002-04-23 20:46:56 +1000152 ret = 1;
153done:
154 buffer_free(&b);
155 return ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157#endif /* AFS */