10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*6812Sraf * Common Development and Distribution License (the "License"). 6*6812Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211219Sraf 220Sstevel@tonic-gate /* 23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 32*6812Sraf #pragma weak _run_setkey = run_setkey 33*6812Sraf #pragma weak _run_crypt = run_crypt 34*6812Sraf #pragma weak _crypt_close = crypt_close 35*6812Sraf #pragma weak _makekey = makekey 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <stdio.h> 380Sstevel@tonic-gate #include <signal.h> 390Sstevel@tonic-gate #include <fcntl.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate #include <thread.h> 420Sstevel@tonic-gate #include <sys/types.h> 430Sstevel@tonic-gate #include <unistd.h> 440Sstevel@tonic-gate #include <strings.h> 450Sstevel@tonic-gate #include <crypt.h> 460Sstevel@tonic-gate #include "des_soft.h" 470Sstevel@tonic-gate #include "lib_gen.h" 480Sstevel@tonic-gate 490Sstevel@tonic-gate #define READER 0 500Sstevel@tonic-gate #define WRITER 1 510Sstevel@tonic-gate #define KSIZE 8 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* Global Variables */ 540Sstevel@tonic-gate static char key[KSIZE+1]; 550Sstevel@tonic-gate struct header { 560Sstevel@tonic-gate long offset; 570Sstevel@tonic-gate unsigned int count; 580Sstevel@tonic-gate }; 590Sstevel@tonic-gate 600Sstevel@tonic-gate static mutex_t lock = DEFAULTMUTEX; 610Sstevel@tonic-gate 620Sstevel@tonic-gate static int cryptopen(); 630Sstevel@tonic-gate static int writekey(); 640Sstevel@tonic-gate 650Sstevel@tonic-gate void _exit(); 660Sstevel@tonic-gate 670Sstevel@tonic-gate int 680Sstevel@tonic-gate run_setkey(int p[2], const char *keyparam) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate (void) mutex_lock(&lock); 710Sstevel@tonic-gate if (cryptopen(p) == -1) { 720Sstevel@tonic-gate (void) mutex_unlock(&lock); 730Sstevel@tonic-gate return (-1); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate (void) strncpy(key, keyparam, KSIZE); 760Sstevel@tonic-gate if (*key == 0) { 770Sstevel@tonic-gate (void) crypt_close_nolock(p); 780Sstevel@tonic-gate (void) mutex_unlock(&lock); 790Sstevel@tonic-gate return (0); 800Sstevel@tonic-gate } 810Sstevel@tonic-gate if (writekey(p, key) == -1) { 820Sstevel@tonic-gate (void) mutex_unlock(&lock); 830Sstevel@tonic-gate return (-1); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate (void) mutex_unlock(&lock); 860Sstevel@tonic-gate return (1); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate static char cmd[] = "exec /usr/bin/crypt -p 2>/dev/null"; 900Sstevel@tonic-gate static int 910Sstevel@tonic-gate cryptopen(int p[2]) 920Sstevel@tonic-gate { 930Sstevel@tonic-gate char c; 940Sstevel@tonic-gate 950Sstevel@tonic-gate if (__p2open(cmd, p) < 0) 960Sstevel@tonic-gate return (-1); 970Sstevel@tonic-gate if (read(p[WRITER], &c, 1) != 1) { /* check that crypt is working on */ 980Sstevel@tonic-gate /* other end */ 990Sstevel@tonic-gate (void) crypt_close(p); /* remove defunct process */ 1000Sstevel@tonic-gate return (-1); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate return (1); 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate static int 1060Sstevel@tonic-gate writekey(int p[2], char *keyarg) 1070Sstevel@tonic-gate { 1080Sstevel@tonic-gate void (*pstat) (); 1090Sstevel@tonic-gate pstat = signal(SIGPIPE, SIG_IGN); /* don't want pipe errors to cause */ 1100Sstevel@tonic-gate /* death */ 1110Sstevel@tonic-gate if (write(p[READER], keyarg, KSIZE) != KSIZE) { 1120Sstevel@tonic-gate (void) crypt_close(p); /* remove defunct process */ 1130Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1140Sstevel@tonic-gate return (-1); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1170Sstevel@tonic-gate return (1); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate int 1220Sstevel@tonic-gate run_crypt(long offset, char *buffer, unsigned int count, int p[2]) 1230Sstevel@tonic-gate { 1240Sstevel@tonic-gate struct header header; 1250Sstevel@tonic-gate void (*pstat) (); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate (void) mutex_lock(&lock); 1280Sstevel@tonic-gate header.count = count; 1290Sstevel@tonic-gate header.offset = offset; 1300Sstevel@tonic-gate pstat = signal(SIGPIPE, SIG_IGN); 1310Sstevel@tonic-gate if (write(p[READER], (char *)&header, sizeof (header)) 132*6812Sraf != sizeof (header)) { 1330Sstevel@tonic-gate (void) crypt_close_nolock(p); 1340Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1350Sstevel@tonic-gate (void) mutex_unlock(&lock); 1360Sstevel@tonic-gate return (-1); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate if (write(p[READER], buffer, count) < count) { 1390Sstevel@tonic-gate (void) crypt_close_nolock(p); 1400Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1410Sstevel@tonic-gate (void) mutex_unlock(&lock); 1420Sstevel@tonic-gate return (-1); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate if (read(p[WRITER], buffer, count) < count) { 1450Sstevel@tonic-gate (void) crypt_close_nolock(p); 1460Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1470Sstevel@tonic-gate (void) mutex_unlock(&lock); 1480Sstevel@tonic-gate return (-1); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate (void) signal(SIGPIPE, pstat); 1510Sstevel@tonic-gate (void) mutex_unlock(&lock); 1520Sstevel@tonic-gate return (0); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate int 1560Sstevel@tonic-gate makekey(int b[2]) 1570Sstevel@tonic-gate { 1580Sstevel@tonic-gate int i; 1590Sstevel@tonic-gate long gorp; 1600Sstevel@tonic-gate char tempbuf[KSIZE], *a, *temp; 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate (void) mutex_lock(&lock); 1630Sstevel@tonic-gate a = key; 1640Sstevel@tonic-gate temp = tempbuf; 1650Sstevel@tonic-gate for (i = 0; i < KSIZE; i++) 1660Sstevel@tonic-gate temp[i] = *a++; 1670Sstevel@tonic-gate gorp = getuid() + getgid(); 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate for (i = 0; i < 4; i++) 1700Sstevel@tonic-gate temp[i] ^= (char)((gorp>>(8*i))&0377); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (cryptopen(b) == -1) { 1730Sstevel@tonic-gate (void) mutex_unlock(&lock); 1740Sstevel@tonic-gate return (-1); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate if (writekey(b, temp) == -1) { 1770Sstevel@tonic-gate (void) mutex_unlock(&lock); 1780Sstevel@tonic-gate return (-1); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate (void) mutex_unlock(&lock); 1810Sstevel@tonic-gate return (0); 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate int 1850Sstevel@tonic-gate crypt_close_nolock(int p[2]) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate if (p[0] == 0 && p[1] == 0 || p[0] < 0 || p[1] < 0) { 1890Sstevel@tonic-gate return (-1); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate return (__p2close(p, NULL, SIGKILL)); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate int 1960Sstevel@tonic-gate crypt_close(int p[2]) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate (void) mutex_lock(&lock); 1990Sstevel@tonic-gate (void) crypt_close_nolock(p); 2000Sstevel@tonic-gate (void) mutex_unlock(&lock); 2010Sstevel@tonic-gate return (0); 2020Sstevel@tonic-gate } 203