1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma weak run_setkey = _run_setkey
35*0Sstevel@tonic-gate #pragma weak run_crypt = _run_crypt
36*0Sstevel@tonic-gate #pragma weak crypt_close = _crypt_close
37*0Sstevel@tonic-gate #pragma weak makekey = _makekey
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "synonyms.h"
40*0Sstevel@tonic-gate #include "mtlib.h"
41*0Sstevel@tonic-gate #include <stdio.h>
42*0Sstevel@tonic-gate #include <signal.h>
43*0Sstevel@tonic-gate #include <fcntl.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate #include <thread.h>
46*0Sstevel@tonic-gate #include <sys/types.h>
47*0Sstevel@tonic-gate #include <unistd.h>
48*0Sstevel@tonic-gate #include <strings.h>
49*0Sstevel@tonic-gate #include <crypt.h>
50*0Sstevel@tonic-gate #include "des_soft.h"
51*0Sstevel@tonic-gate #include "lib_gen.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #define	READER	0
54*0Sstevel@tonic-gate #define	WRITER	1
55*0Sstevel@tonic-gate #define	KSIZE 	8
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*  Global Variables  */
58*0Sstevel@tonic-gate static char key[KSIZE+1];
59*0Sstevel@tonic-gate struct header {
60*0Sstevel@tonic-gate 	long offset;
61*0Sstevel@tonic-gate 	unsigned int count;
62*0Sstevel@tonic-gate };
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int cryptopen();
67*0Sstevel@tonic-gate static int writekey();
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate void	_exit();
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate int
72*0Sstevel@tonic-gate run_setkey(int p[2], const char *keyparam)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
75*0Sstevel@tonic-gate 	if (cryptopen(p) == -1) {
76*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
77*0Sstevel@tonic-gate 		return (-1);
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 	(void)  strncpy(key, keyparam, KSIZE);
80*0Sstevel@tonic-gate 	if (*key == 0) {
81*0Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
82*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
83*0Sstevel@tonic-gate 		return (0);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	if (writekey(p, key) == -1) {
86*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
87*0Sstevel@tonic-gate 		return (-1);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
90*0Sstevel@tonic-gate 	return (1);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static char cmd[] = "exec /usr/bin/crypt -p 2>/dev/null";
94*0Sstevel@tonic-gate static int
95*0Sstevel@tonic-gate cryptopen(int p[2])
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	char c;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if (__p2open(cmd, p) < 0)
100*0Sstevel@tonic-gate 		return (-1);
101*0Sstevel@tonic-gate 	if (read(p[WRITER], &c, 1) != 1) { /* check that crypt is working on */
102*0Sstevel@tonic-gate 					    /* other end */
103*0Sstevel@tonic-gate 		(void)  crypt_close(p); /* remove defunct process */
104*0Sstevel@tonic-gate 		return (-1);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 	return (1);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static int
110*0Sstevel@tonic-gate writekey(int p[2], char *keyarg)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	void (*pstat) ();
113*0Sstevel@tonic-gate 	pstat = signal(SIGPIPE, SIG_IGN); /* don't want pipe errors to cause */
114*0Sstevel@tonic-gate 					    /*  death */
115*0Sstevel@tonic-gate 	if (write(p[READER], keyarg, KSIZE) != KSIZE) {
116*0Sstevel@tonic-gate 		(void)  crypt_close(p); /* remove defunct process */
117*0Sstevel@tonic-gate 		(void)  signal(SIGPIPE, pstat);
118*0Sstevel@tonic-gate 		return (-1);
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 	(void)  signal(SIGPIPE, pstat);
121*0Sstevel@tonic-gate 	return (1);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate int
126*0Sstevel@tonic-gate run_crypt(long offset, char *buffer, unsigned int count, int p[2])
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	struct header header;
129*0Sstevel@tonic-gate 	void (*pstat) ();
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
132*0Sstevel@tonic-gate 	header.count = count;
133*0Sstevel@tonic-gate 	header.offset = offset;
134*0Sstevel@tonic-gate 	pstat = signal(SIGPIPE, SIG_IGN);
135*0Sstevel@tonic-gate 	if (write(p[READER], (char *)&header, sizeof (header))
136*0Sstevel@tonic-gate 		!= sizeof (header)) {
137*0Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
138*0Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
139*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
140*0Sstevel@tonic-gate 		return (-1);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	if (write(p[READER], buffer, count) < count) {
143*0Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
144*0Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
145*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
146*0Sstevel@tonic-gate 		return (-1);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 	if (read(p[WRITER], buffer,  count) < count) {
149*0Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
150*0Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
151*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
152*0Sstevel@tonic-gate 		return (-1);
153*0Sstevel@tonic-gate 	}
154*0Sstevel@tonic-gate 	(void) signal(SIGPIPE, pstat);
155*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
156*0Sstevel@tonic-gate 	return (0);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate int
160*0Sstevel@tonic-gate makekey(int b[2])
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	int i;
163*0Sstevel@tonic-gate 	long gorp;
164*0Sstevel@tonic-gate 	char tempbuf[KSIZE], *a, *temp;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
167*0Sstevel@tonic-gate 	a = key;
168*0Sstevel@tonic-gate 	temp = tempbuf;
169*0Sstevel@tonic-gate 	for (i = 0; i < KSIZE; i++)
170*0Sstevel@tonic-gate 		temp[i] = *a++;
171*0Sstevel@tonic-gate 	gorp = getuid() + getgid();
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
174*0Sstevel@tonic-gate 		temp[i] ^= (char)((gorp>>(8*i))&0377);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (cryptopen(b) == -1) {
177*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
178*0Sstevel@tonic-gate 		return (-1);
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	if (writekey(b, temp) == -1) {
181*0Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
182*0Sstevel@tonic-gate 		return (-1);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
185*0Sstevel@tonic-gate 	return (0);
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate int
189*0Sstevel@tonic-gate crypt_close_nolock(int p[2])
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if (p[0] == 0 && p[1] == 0 || p[0] < 0 || p[1] < 0) {
193*0Sstevel@tonic-gate 		return (-1);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	return (__p2close(p, NULL, SIGKILL));
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate int
200*0Sstevel@tonic-gate crypt_close(int p[2])
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	(void) mutex_lock(&lock);
203*0Sstevel@tonic-gate 	(void) crypt_close_nolock(p);
204*0Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
205*0Sstevel@tonic-gate 	return (0);
206*0Sstevel@tonic-gate }
207