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