xref: /titanic_44/usr/src/lib/libcrypt/common/cryptio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 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) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #pragma weak run_setkey = _run_setkey
35*7c478bd9Sstevel@tonic-gate #pragma weak run_crypt = _run_crypt
36*7c478bd9Sstevel@tonic-gate #pragma weak crypt_close = _crypt_close
37*7c478bd9Sstevel@tonic-gate #pragma weak makekey = _makekey
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "synonyms.h"
40*7c478bd9Sstevel@tonic-gate #include "mtlib.h"
41*7c478bd9Sstevel@tonic-gate #include <stdio.h>
42*7c478bd9Sstevel@tonic-gate #include <signal.h>
43*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
44*7c478bd9Sstevel@tonic-gate #include <errno.h>
45*7c478bd9Sstevel@tonic-gate #include <thread.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
47*7c478bd9Sstevel@tonic-gate #include <unistd.h>
48*7c478bd9Sstevel@tonic-gate #include <strings.h>
49*7c478bd9Sstevel@tonic-gate #include <crypt.h>
50*7c478bd9Sstevel@tonic-gate #include "des_soft.h"
51*7c478bd9Sstevel@tonic-gate #include "lib_gen.h"
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	READER	0
54*7c478bd9Sstevel@tonic-gate #define	WRITER	1
55*7c478bd9Sstevel@tonic-gate #define	KSIZE 	8
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate /*  Global Variables  */
58*7c478bd9Sstevel@tonic-gate static char key[KSIZE+1];
59*7c478bd9Sstevel@tonic-gate struct header {
60*7c478bd9Sstevel@tonic-gate 	long offset;
61*7c478bd9Sstevel@tonic-gate 	unsigned int count;
62*7c478bd9Sstevel@tonic-gate };
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static int cryptopen();
67*7c478bd9Sstevel@tonic-gate static int writekey();
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate void	_exit();
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int
72*7c478bd9Sstevel@tonic-gate run_setkey(int p[2], const char *keyparam)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
75*7c478bd9Sstevel@tonic-gate 	if (cryptopen(p) == -1) {
76*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
77*7c478bd9Sstevel@tonic-gate 		return (-1);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	(void)  strncpy(key, keyparam, KSIZE);
80*7c478bd9Sstevel@tonic-gate 	if (*key == 0) {
81*7c478bd9Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
82*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
83*7c478bd9Sstevel@tonic-gate 		return (0);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 	if (writekey(p, key) == -1) {
86*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
87*7c478bd9Sstevel@tonic-gate 		return (-1);
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
90*7c478bd9Sstevel@tonic-gate 	return (1);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate static char cmd[] = "exec /usr/bin/crypt -p 2>/dev/null";
94*7c478bd9Sstevel@tonic-gate static int
95*7c478bd9Sstevel@tonic-gate cryptopen(int p[2])
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	char c;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (__p2open(cmd, p) < 0)
100*7c478bd9Sstevel@tonic-gate 		return (-1);
101*7c478bd9Sstevel@tonic-gate 	if (read(p[WRITER], &c, 1) != 1) { /* check that crypt is working on */
102*7c478bd9Sstevel@tonic-gate 					    /* other end */
103*7c478bd9Sstevel@tonic-gate 		(void)  crypt_close(p); /* remove defunct process */
104*7c478bd9Sstevel@tonic-gate 		return (-1);
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	return (1);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate static int
110*7c478bd9Sstevel@tonic-gate writekey(int p[2], char *keyarg)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	void (*pstat) ();
113*7c478bd9Sstevel@tonic-gate 	pstat = signal(SIGPIPE, SIG_IGN); /* don't want pipe errors to cause */
114*7c478bd9Sstevel@tonic-gate 					    /*  death */
115*7c478bd9Sstevel@tonic-gate 	if (write(p[READER], keyarg, KSIZE) != KSIZE) {
116*7c478bd9Sstevel@tonic-gate 		(void)  crypt_close(p); /* remove defunct process */
117*7c478bd9Sstevel@tonic-gate 		(void)  signal(SIGPIPE, pstat);
118*7c478bd9Sstevel@tonic-gate 		return (-1);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 	(void)  signal(SIGPIPE, pstat);
121*7c478bd9Sstevel@tonic-gate 	return (1);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate int
126*7c478bd9Sstevel@tonic-gate run_crypt(long offset, char *buffer, unsigned int count, int p[2])
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	struct header header;
129*7c478bd9Sstevel@tonic-gate 	void (*pstat) ();
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
132*7c478bd9Sstevel@tonic-gate 	header.count = count;
133*7c478bd9Sstevel@tonic-gate 	header.offset = offset;
134*7c478bd9Sstevel@tonic-gate 	pstat = signal(SIGPIPE, SIG_IGN);
135*7c478bd9Sstevel@tonic-gate 	if (write(p[READER], (char *)&header, sizeof (header))
136*7c478bd9Sstevel@tonic-gate 		!= sizeof (header)) {
137*7c478bd9Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
138*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
139*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
140*7c478bd9Sstevel@tonic-gate 		return (-1);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	if (write(p[READER], buffer, count) < count) {
143*7c478bd9Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
144*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
145*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
146*7c478bd9Sstevel@tonic-gate 		return (-1);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 	if (read(p[WRITER], buffer,  count) < count) {
149*7c478bd9Sstevel@tonic-gate 		(void) crypt_close_nolock(p);
150*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGPIPE, pstat);
151*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
152*7c478bd9Sstevel@tonic-gate 		return (-1);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, pstat);
155*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
156*7c478bd9Sstevel@tonic-gate 	return (0);
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate int
160*7c478bd9Sstevel@tonic-gate makekey(int b[2])
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	int i;
163*7c478bd9Sstevel@tonic-gate 	long gorp;
164*7c478bd9Sstevel@tonic-gate 	char tempbuf[KSIZE], *a, *temp;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
167*7c478bd9Sstevel@tonic-gate 	a = key;
168*7c478bd9Sstevel@tonic-gate 	temp = tempbuf;
169*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < KSIZE; i++)
170*7c478bd9Sstevel@tonic-gate 		temp[i] = *a++;
171*7c478bd9Sstevel@tonic-gate 	gorp = getuid() + getgid();
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
174*7c478bd9Sstevel@tonic-gate 		temp[i] ^= (char)((gorp>>(8*i))&0377);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if (cryptopen(b) == -1) {
177*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
178*7c478bd9Sstevel@tonic-gate 		return (-1);
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 	if (writekey(b, temp) == -1) {
181*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
182*7c478bd9Sstevel@tonic-gate 		return (-1);
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
185*7c478bd9Sstevel@tonic-gate 	return (0);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate int
189*7c478bd9Sstevel@tonic-gate crypt_close_nolock(int p[2])
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if (p[0] == 0 && p[1] == 0 || p[0] < 0 || p[1] < 0) {
193*7c478bd9Sstevel@tonic-gate 		return (-1);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	return (__p2close(p, NULL, SIGKILL));
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate int
200*7c478bd9Sstevel@tonic-gate crypt_close(int p[2])
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&lock);
203*7c478bd9Sstevel@tonic-gate 	(void) crypt_close_nolock(p);
204*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
205*7c478bd9Sstevel@tonic-gate 	return (0);
206*7c478bd9Sstevel@tonic-gate }
207