Print this page
pass 2
first pass
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libsldap/common/ns_crypt.c
+++ new/usr/src/lib/libsldap/common/ns_crypt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 1999-2003 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 #pragma ident "%Z%%M% %I% %E% SMI"
31 31
32 32 #include <stdlib.h>
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
33 33 #include <string.h>
34 34 #include <libintl.h>
35 35 #include <locale.h>
36 36 #include <errno.h>
37 37 #include <unistd.h>
38 38 #include <ctype.h>
39 39 #include <syslog.h>
40 40 #include <sys/time.h>
41 41 #include "ns_sldap.h"
42 42 #include "ns_internal.h"
43 -/* EXPORT DELETE START */
44 43 #include <crypt.h>
45 44
46 -#define NS_DOMESTIC 1
47 -
48 45 static char t1[ROTORSIZE];
49 46 static char t2[ROTORSIZE];
50 47 static char t3[ROTORSIZE];
51 48 static char hexdig[] = "0123456789abcdef";
52 49
53 50 static mutex_t ns_crypt_lock = DEFAULTMUTEX;
54 51 static boolean_t crypt_inited = B_FALSE;
55 52
56 53 static int
57 54 is_cleartext(const char *pwd)
58 55 {
59 56 if (0 == strncmp(pwd, CRYPTMARK, strlen(CRYPTMARK)))
60 57 return (FALSE);
61 58 return (TRUE);
62 59 }
63 60
64 61
65 62 static char *
66 63 hex2ascii(char *aString, int aLen)
67 64 {
68 65 char *res;
69 66 int i = 0;
70 67
71 68 if ((res = (char *)calloc(aLen*2 + 1, 1)) == NULL) {
72 69 return (NULL);
73 70 }
74 71 for (;;) {
75 72 if (aLen < 1)
76 73 break;
77 74 res[i] = hexdig[(*aString & 0xf0) >> 4];
78 75 res[i + 1] = hexdig[*aString & 0x0f];
79 76 i += 2;
80 77 aLen--;
81 78 aString++;
82 79 }
83 80 return (res);
84 81 }
85 82
86 83
87 84 static int
88 85 unhex(char c)
89 86 {
90 87 return (c >= '0' && c <= '9' ? c - '0'
91 88 : c >= 'A' && c <= 'F' ? c - 'A' + 10
92 89 : c - 'a' + 10);
93 90 }
94 91
95 92
96 93 static char *
97 94 ascii2hex(char *anHexaStr, int *aResLen)
98 95 {
99 96 int theLen = 0;
100 97 char *theRes = malloc(strlen(anHexaStr) /2 + 1);
101 98
102 99 if (theRes == NULL)
103 100 return (NULL);
104 101 while (isxdigit(*anHexaStr)) {
105 102 theRes[theLen] = unhex(*anHexaStr) << 4;
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
106 103 if (++anHexaStr != '\0') {
107 104 theRes[theLen] += unhex(*anHexaStr);
108 105 anHexaStr++;
109 106 }
110 107 theLen++;
111 108 }
112 109 theRes[theLen] = '\0';
113 110 *aResLen = theLen;
114 111 return (theRes);
115 112 }
116 -/* EXPORT DELETE END */
117 113
118 114
119 115 static void
120 116 c_setup()
121 117 {
122 -/* EXPORT DELETE START */
123 118 int ic, i, k, temp;
124 119 unsigned random;
125 120 char buf[13];
126 121 int seed;
127 122
128 123 (void) mutex_lock(&ns_crypt_lock);
129 124 if (crypt_inited) {
130 125 (void) mutex_unlock(&ns_crypt_lock);
131 126 return;
132 127 }
133 128 (void) strcpy(buf, "Homer J");
134 129 buf[8] = buf[0];
135 130 buf[9] = buf[1];
136 131 (void) strncpy(buf, (char *)crypt(buf, &buf[8]), 13);
137 132 seed = 123;
138 133 for (i = 0; i < 13; i++)
139 134 seed = seed*buf[i] + i;
140 135 for (i = 0; i < ROTORSIZE; i++) {
141 136 t1[i] = i;
142 137 t3[i] = 0;
143 138 }
144 139 for (i = 0; i < ROTORSIZE; i++) {
145 140 seed = 5*seed + buf[i%13];
146 141 random = seed % 65521;
147 142 k = ROTORSIZE-1 - i;
148 143 ic = (random&MASK)%(k+1);
149 144 random >>= 8;
150 145 temp = t1[k];
151 146 t1[k] = t1[ic];
152 147 t1[ic] = temp;
153 148 if (t3[k] != 0) continue;
154 149 ic = (random&MASK) % k;
155 150 while (t3[ic] != 0) ic = (ic + 1) % k;
156 151 t3[k] = ic;
157 152 t3[ic] = k;
158 153 }
159 154 for (i = 0; i < ROTORSIZE; i++)
160 155 t2[t1[i]&MASK] = i;
161 156 crypt_inited = B_TRUE;
162 157 (void) mutex_unlock(&ns_crypt_lock);
163 158 }
164 159
165 160
166 161 static char *
167 162 modvalue(char *str, int len, int *mod_len)
168 163 {
169 164 int i, n1, n2;
170 165 char *s;
171 166
172 167 if (!crypt_inited)
173 168 c_setup();
174 169 i = 0;
175 170 n1 = 0;
176 171 n2 = 0;
177 172 if ((s = (char *)malloc(2 * len + 1)) != NULL) {
178 173 while (i < len) {
179 174 s[i] = t2[(t3[(t1[(str[i]+n1)&MASK]+n2)&MASK]-n2)&MASK]-n1;
180 175 i++;
181 176 n1++;
182 177 if (n1 == ROTORSIZE) {
↓ open down ↓ |
50 lines elided |
↑ open up ↑ |
183 178 n1 = 0;
184 179 n2++;
185 180 if (n2 == ROTORSIZE) n2 = 0;
186 181 }
187 182 }
188 183 s[i] = '\0';
189 184 if (mod_len != NULL)
190 185 *mod_len = i;
191 186 }
192 187 return (s);
193 -/* EXPORT DELETE END */
194 188 }
195 189
196 190
197 191 char *
198 192 evalue(char *ptr)
199 193 {
200 -/* EXPORT DELETE START */
201 194 char *modv, *str, *ev;
202 195 int modv_len;
203 196 size_t len;
204 197
205 198 /*
206 199 * if not cleartext, return a copy of what ptr
207 200 * points to as that is what evalue does below.
208 201 */
209 202 if (FALSE == is_cleartext(ptr)) {
210 203 str = strdup(ptr);
211 204 return (str);
212 205 }
213 206
214 207 modv = modvalue(ptr, strlen(ptr), &modv_len);
215 208 str = hex2ascii(modv, modv_len);
216 209 free(modv);
217 210 modv = NULL;
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
218 211 len = strlen(str) + strlen(CRYPTMARK) + 1;
219 212 ev = malloc(len);
220 213 if (ev == NULL) {
221 214 free(str);
222 215 return (NULL);
223 216 }
224 217 (void) snprintf(ev, len, CRYPTMARK "%s", str);
225 218 free(str);
226 219 str = NULL;
227 220 return (ev);
228 -#ifndef NS_DOMESTIC
229 -/* EXPORT DELETE END */
230 - return (strdup(ptr));
231 -/* EXPORT DELETE START */
232 -#endif
233 -/* EXPORT DELETE END */
234 221 }
235 222
236 223
237 224 char *
238 225 dvalue(char *ptr)
239 226 {
240 -/* EXPORT DELETE START */
241 227 char *modv, *str, *sb;
242 228 int len;
243 229
244 230 /* if cleartext return NULL (error!) */
245 231 if (TRUE == is_cleartext(ptr))
246 232 return (NULL);
247 233
248 234 sb = strchr(ptr, '}');
249 235 sb++;
250 236 len = strlen(sb);
251 237 str = ascii2hex(sb, &len);
252 238 modv = modvalue(str, len, NULL);
253 239 free(str);
254 240 str = NULL;
255 241 return (modv);
256 -#ifndef NS_DOMESTIC
257 -/* EXPORT DELETE END */
258 - return (strdup(ptr));
259 -/* EXPORT DELETE START */
260 -#endif
261 -/* EXPORT DELETE END */
262 242 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX