Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | * Copyright (c) 1999 Dug Song. All rights reserved. |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 3 | * Copyright (c) 2002 Markus Friedl. All rights reserved. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 24 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 27 | #include "uuencode.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 29 | RCSID("$OpenBSD: radix.c,v 1.22 2002/09/09 14:54:15 markus Exp $"); |
Damien Miller | bf7f466 | 2000-06-23 10:16:38 +1000 | [diff] [blame] | 30 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 31 | #ifdef AFS |
| 32 | #include <krb.h> |
| 33 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 34 | #include <radix.h> |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 35 | #include "bufaux.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 37 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 38 | creds_to_radix(CREDENTIALS *creds, u_char *buf, size_t buflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | { |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 40 | Buffer b; |
| 41 | int ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 43 | buffer_init(&b); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 45 | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | /* Null string to repeat the realm. */ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 59 | buffer_put_char(&b, '\0'); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 61 | 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 67 | /* 32 bit size + data */ |
Markus Friedl | 435673d | 2002-04-23 16:35:15 +0200 | [diff] [blame] | 68 | buffer_put_string(&b, creds->ticket_st.dat, creds->ticket_st.length); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 69 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 70 | ret = uuencode(buffer_ptr(&b), buffer_len(&b), (char *)buf, buflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 72 | buffer_free(&b); |
| 73 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | } |
| 75 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 76 | #define GETSTRING(b, t, tlen) \ |
| 77 | do { \ |
Damien Miller | 635fe98 | 2002-04-23 21:00:33 +1000 | [diff] [blame] | 78 | int i, found = 0; \ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 79 | for (i = 0; i < tlen; i++) { \ |
| 80 | if (buffer_len(b) == 0) \ |
| 81 | goto done; \ |
| 82 | t[i] = buffer_get_char(b); \ |
Damien Miller | 635fe98 | 2002-04-23 21:00:33 +1000 | [diff] [blame] | 83 | if (t[i] == '\0') { \ |
| 84 | found = 1; \ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 85 | break; \ |
Damien Miller | 635fe98 | 2002-04-23 21:00:33 +1000 | [diff] [blame] | 86 | } \ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 87 | } \ |
Damien Miller | 635fe98 | 2002-04-23 21:00:33 +1000 | [diff] [blame] | 88 | if (!found) \ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 89 | goto done; \ |
| 90 | } while(0) |
| 91 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 92 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | radix_to_creds(const char *buf, CREDENTIALS *creds) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | { |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 95 | Buffer b; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 96 | u_char *space; |
| 97 | char c, version, *p; |
| 98 | u_int endTime, len; |
| 99 | int blen, ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 101 | ret = 0; |
| 102 | blen = strlen(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 104 | /* sanity check for size */ |
| 105 | if (blen > 8192) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 108 | buffer_init(&b); |
| 109 | space = buffer_append_space(&b, blen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 111 | /* check version and length! */ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 112 | len = uudecode(buf, space, blen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | if (len < 1) |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 114 | goto done; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 116 | version = buffer_get_char(&b); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 118 | GETSTRING(&b, creds->service, sizeof creds->service); |
| 119 | GETSTRING(&b, creds->instance, sizeof creds->instance); |
| 120 | GETSTRING(&b, creds->realm, sizeof creds->realm); |
| 121 | GETSTRING(&b, creds->pname, sizeof creds->pname); |
| 122 | GETSTRING(&b, creds->pinst, sizeof creds->pinst); |
| 123 | |
| 124 | if (buffer_len(&b) == 0) |
| 125 | goto done; |
| 126 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | /* Ignore possibly different realm. */ |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 128 | while (buffer_len(&b) > 0 && (c = buffer_get_char(&b)) != '\0') |
| 129 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 131 | if (buffer_len(&b) == 0) |
| 132 | goto done; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 134 | creds->issue_date = buffer_get_int(&b); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 135 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 136 | endTime = buffer_get_int(&b); |
| 137 | creds->lifetime = krb_time_to_life(creds->issue_date, endTime); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 138 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 139 | len = buffer_len(&b); |
| 140 | if (len < sizeof(creds->session)) |
| 141 | goto done; |
| 142 | memcpy(&creds->session, buffer_ptr(&b), sizeof(creds->session)); |
| 143 | buffer_consume(&b, sizeof(creds->session)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 144 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 145 | creds->kvno = buffer_get_short(&b); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 147 | p = buffer_get_string(&b, &len); |
| 148 | if (len < 0 || len > sizeof(creds->ticket_st.dat)) |
| 149 | goto done; |
| 150 | memcpy(&creds->ticket_st.dat, p, len); |
| 151 | creds->ticket_st.length = len; |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 152 | |
Damien Miller | bad0e01 | 2002-04-23 20:46:56 +1000 | [diff] [blame] | 153 | ret = 1; |
| 154 | done: |
| 155 | buffer_free(&b); |
| 156 | return ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | #endif /* AFS */ |