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 51914Scasper * Common Development and Distribution License (the "License"). 61914Scasper * 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 */ 213864Sraf 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 #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 29*6812Sraf #pragma weak _crypt = crypt 30*6812Sraf #pragma weak _encrypt = encrypt 31*6812Sraf #pragma weak _setkey = setkey 320Sstevel@tonic-gate 33*6812Sraf #include "lint.h" 340Sstevel@tonic-gate #include "mtlib.h" 350Sstevel@tonic-gate #include <synch.h> 360Sstevel@tonic-gate #include <thread.h> 370Sstevel@tonic-gate #include <ctype.h> 380Sstevel@tonic-gate #include <dlfcn.h> 390Sstevel@tonic-gate #include <errno.h> 400Sstevel@tonic-gate #include <stdio.h> 410Sstevel@tonic-gate #include <strings.h> 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <sys/time.h> 440Sstevel@tonic-gate #include <limits.h> 450Sstevel@tonic-gate #include <sys/types.h> 460Sstevel@tonic-gate #include <sys/stat.h> 470Sstevel@tonic-gate #include <fcntl.h> 480Sstevel@tonic-gate #include <syslog.h> 490Sstevel@tonic-gate #include <unistd.h> 503864Sraf #include <atomic.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include <crypt.h> 530Sstevel@tonic-gate #include <libc.h> 540Sstevel@tonic-gate #include "tsd.h" 550Sstevel@tonic-gate 560Sstevel@tonic-gate #define CRYPT_ALGORITHMS_ALLOW "CRYPT_ALGORITHMS_ALLOW" 570Sstevel@tonic-gate #define CRYPT_ALGORITHMS_DEPRECATE "CRYPT_ALGORITHMS_DEPRECATE" 580Sstevel@tonic-gate #define CRYPT_DEFAULT "CRYPT_DEFAULT" 590Sstevel@tonic-gate #define CRYPT_UNIX "__unix__" 600Sstevel@tonic-gate 610Sstevel@tonic-gate #define CRYPT_CONFFILE "/etc/security/crypt.conf" 620Sstevel@tonic-gate #define POLICY_CONF_FILE "/etc/security/policy.conf" 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define CRYPT_CONFLINELENGTH 1024 650Sstevel@tonic-gate 660Sstevel@tonic-gate #define CRYPT_MODULE_ISA "/$ISA/" 670Sstevel@tonic-gate #ifdef _LP64 680Sstevel@tonic-gate #define CRYPT_MODULE_DIR "/usr/lib/security/64/" 690Sstevel@tonic-gate #define CRYPT_ISA_DIR "/64/" 700Sstevel@tonic-gate #else /* !_LP64 */ 710Sstevel@tonic-gate #define CRYPT_MODULE_DIR "/usr/lib/security/" 720Sstevel@tonic-gate #define CRYPT_ISA_DIR "/" 730Sstevel@tonic-gate #endif /* _LP64 */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * MAX_ALGNAME_LEN: 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * In practical terms this is probably never any bigger than about 10, but... 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * It has to fix the encrypted password filed of struct spwd it is 810Sstevel@tonic-gate * theoretically the maximum length of the cipher minus the magic $ sign. 820Sstevel@tonic-gate * Though that would be unexpected. 830Sstevel@tonic-gate * Since it also has to fit in crypt.conf it is CRYPT_CONFLINELENGTH 840Sstevel@tonic-gate * minus the path to the module and the minimum white space. 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * CRYPT_MAXCIPHERTEXTLEN is defined in crypt.h and is smaller than 870Sstevel@tonic-gate * CRYPT_CONFLINELENGTH, and probably always will be. 880Sstevel@tonic-gate */ 890Sstevel@tonic-gate #define MAX_ALGNAME_LEN (CRYPT_MAXCIPHERTEXTLEN - 1) 900Sstevel@tonic-gate 910Sstevel@tonic-gate struct crypt_alg_s { 920Sstevel@tonic-gate void *a_libhandle; 930Sstevel@tonic-gate char *(*a_genhash)(char *, const size_t, const char *, 940Sstevel@tonic-gate const char *, const char **); 950Sstevel@tonic-gate char *(*a_gensalt)(char *, const size_t, 960Sstevel@tonic-gate const char *, const struct passwd *, const char **); 970Sstevel@tonic-gate char **a_params; 980Sstevel@tonic-gate int a_nparams; 990Sstevel@tonic-gate }; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate struct crypt_policy_s { 1020Sstevel@tonic-gate char *cp_default; 1030Sstevel@tonic-gate char *cp_allow; 1040Sstevel@tonic-gate char *cp_deny; 1050Sstevel@tonic-gate }; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate enum crypt_policy_error_e { 1080Sstevel@tonic-gate CPE_BOTH = 1, 1090Sstevel@tonic-gate CPE_MULTI 1100Sstevel@tonic-gate }; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate static struct crypt_policy_s *getcryptpolicy(void); 1130Sstevel@tonic-gate static void free_crypt_policy(struct crypt_policy_s *policy); 1140Sstevel@tonic-gate static struct crypt_alg_s *getalgbyname(const char *algname, boolean_t *found); 1150Sstevel@tonic-gate static void free_crypt_alg(struct crypt_alg_s *alg); 1160Sstevel@tonic-gate static char *getalgfromsalt(const char *salt); 1170Sstevel@tonic-gate static boolean_t alg_valid(const char *algname, 1180Sstevel@tonic-gate const struct crypt_policy_s *policy); 1190Sstevel@tonic-gate static char *isa_path(const char *path); 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate static char *_unix_crypt(const char *pw, const char *salt, char *iobuf); 1220Sstevel@tonic-gate static char *_unix_crypt_gensalt(char *gsbuffer, size_t gsbufflen, 1230Sstevel@tonic-gate const char *oldpuresalt, const struct passwd *userinfo, 1240Sstevel@tonic-gate const char *params[]); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * crypt - string encoding function 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * This function encodes strings in a suitable for for secure storage 1310Sstevel@tonic-gate * as passwords. It generates the password hash given the plaintext and salt. 1320Sstevel@tonic-gate * 1330Sstevel@tonic-gate * If the first character of salt is "$" then we use crypt.conf(4) to 1340Sstevel@tonic-gate * determine which plugin to use and run the crypt_genhash_impl(3c) function 1350Sstevel@tonic-gate * from it. 1360Sstevel@tonic-gate * Otherwise we use the old unix algorithm. 1370Sstevel@tonic-gate * 1380Sstevel@tonic-gate * RETURN VALUES 1390Sstevel@tonic-gate * On Success we return a pointer to the encoded string. The 1400Sstevel@tonic-gate * return value points to thread specific static data and should NOT 1410Sstevel@tonic-gate * be passed free(3c). 1420Sstevel@tonic-gate * On failure we return NULL and set errno to one of: 1430Sstevel@tonic-gate * EINVAL, ELIBACC, ENOMEM, ENOSYS. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate char * 1460Sstevel@tonic-gate crypt(const char *plaintext, const char *salt) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate struct crypt_alg_s *alg; 1490Sstevel@tonic-gate char *ctbuffer; 1500Sstevel@tonic-gate char *ciphertext; 1510Sstevel@tonic-gate char *algname; 1520Sstevel@tonic-gate boolean_t found; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate ctbuffer = tsdalloc(_T_CRYPT, CRYPT_MAXCIPHERTEXTLEN, NULL); 1550Sstevel@tonic-gate if (ctbuffer == NULL) 1560Sstevel@tonic-gate return (NULL); 1570Sstevel@tonic-gate bzero(ctbuffer, CRYPT_MAXCIPHERTEXTLEN); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * '$' is never a possible salt char with the traditional unix 1610Sstevel@tonic-gate * algorithm. If the salt passed in is NULL or the first char 1620Sstevel@tonic-gate * of the salt isn't a $ then do the traditional thing. 1630Sstevel@tonic-gate * We also do the traditional thing if the salt is only 1 char. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate if (salt == NULL || salt[0] != '$' || strlen(salt) == 1) { 1660Sstevel@tonic-gate return (_unix_crypt(plaintext, salt, ctbuffer)); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1700Sstevel@tonic-gate * Find the algorithm name from the salt and look it up in 1710Sstevel@tonic-gate * crypt.conf(4) to find out what shared object to use. 1720Sstevel@tonic-gate * If we can't find it in crypt.conf then getalgbyname would 1730Sstevel@tonic-gate * have returned with found = B_FALSE so we use the unix algorithm. 1740Sstevel@tonic-gate * If alg is NULL but found = B_TRUE then there is a problem with 1750Sstevel@tonic-gate * the plugin so we fail leaving errno set to what getalgbyname() 1760Sstevel@tonic-gate * set it to or EINVAL it if wasn't set. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate if ((algname = getalgfromsalt(salt)) == NULL) { 1790Sstevel@tonic-gate return (NULL); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate errno = 0; 1830Sstevel@tonic-gate alg = getalgbyname(algname, &found); 1840Sstevel@tonic-gate if ((alg == NULL) || !found) { 1850Sstevel@tonic-gate if (errno == 0) 1860Sstevel@tonic-gate errno = EINVAL; 1870Sstevel@tonic-gate ciphertext = NULL; 1880Sstevel@tonic-gate goto cleanup; 1890Sstevel@tonic-gate } else if (!found) { 1900Sstevel@tonic-gate ciphertext = _unix_crypt(plaintext, salt, ctbuffer); 1910Sstevel@tonic-gate } else { 1920Sstevel@tonic-gate ciphertext = alg->a_genhash(ctbuffer, CRYPT_MAXCIPHERTEXTLEN, 1930Sstevel@tonic-gate plaintext, salt, (const char **)alg->a_params); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate cleanup: 1970Sstevel@tonic-gate free_crypt_alg(alg); 1980Sstevel@tonic-gate if (algname != NULL) 1990Sstevel@tonic-gate free(algname); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate return (ciphertext); 2020Sstevel@tonic-gate } 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate /* 2050Sstevel@tonic-gate * crypt_gensalt - generate salt string for string encoding 2060Sstevel@tonic-gate * 2070Sstevel@tonic-gate * This function generates the salt string pased to crypt(3c). 2080Sstevel@tonic-gate * If oldsalt is NULL, the use the default algorithm. 2090Sstevel@tonic-gate * Other wise check the policy in policy.conf to ensure that it is 2100Sstevel@tonic-gate * either still allowed or not deprecated. 2110Sstevel@tonic-gate * 2120Sstevel@tonic-gate * RETURN VALUES 2130Sstevel@tonic-gate * Return a pointer to the new salt, the caller is responsible 2140Sstevel@tonic-gate * for using free(3c) on the return value. 2150Sstevel@tonic-gate * Returns NULL on error and sets errno to one of: 2160Sstevel@tonic-gate * EINVAL, ELIBACC, ENOMEM 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate char * 2190Sstevel@tonic-gate crypt_gensalt(const char *oldsalt, const struct passwd *userinfo) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate struct crypt_alg_s *alg = NULL; 2220Sstevel@tonic-gate struct crypt_policy_s *policy = NULL; 2230Sstevel@tonic-gate char *newsalt = NULL; 2240Sstevel@tonic-gate char *gsbuffer; 2250Sstevel@tonic-gate char *algname = NULL; 2260Sstevel@tonic-gate boolean_t found; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate gsbuffer = calloc(CRYPT_MAXCIPHERTEXTLEN, sizeof (char *)); 2290Sstevel@tonic-gate if (gsbuffer == NULL) { 2300Sstevel@tonic-gate errno = ENOMEM; 2310Sstevel@tonic-gate goto cleanup; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate policy = getcryptpolicy(); 2350Sstevel@tonic-gate if (policy == NULL) { 2360Sstevel@tonic-gate errno = EINVAL; 2370Sstevel@tonic-gate goto cleanup; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate algname = getalgfromsalt(oldsalt); 2410Sstevel@tonic-gate if (!alg_valid(algname, policy)) { 2420Sstevel@tonic-gate free(algname); 2430Sstevel@tonic-gate algname = strdup(policy->cp_default); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate if (strcmp(algname, CRYPT_UNIX) == 0) { 2470Sstevel@tonic-gate newsalt = _unix_crypt_gensalt(gsbuffer, CRYPT_MAXCIPHERTEXTLEN, 2480Sstevel@tonic-gate oldsalt, userinfo, NULL); 2490Sstevel@tonic-gate } else { 2500Sstevel@tonic-gate errno = 0; 2510Sstevel@tonic-gate alg = getalgbyname(algname, &found); 2520Sstevel@tonic-gate if (alg == NULL || !found) { 2530Sstevel@tonic-gate if (errno == 0) 2540Sstevel@tonic-gate errno = EINVAL; 2550Sstevel@tonic-gate goto cleanup; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate newsalt = alg->a_gensalt(gsbuffer, CRYPT_MAXCIPHERTEXTLEN, 2580Sstevel@tonic-gate oldsalt, userinfo, (const char **)alg->a_params); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate cleanup: 2620Sstevel@tonic-gate free_crypt_policy(policy); 2630Sstevel@tonic-gate free_crypt_alg(alg); 2640Sstevel@tonic-gate if (newsalt == NULL && gsbuffer != NULL) 2650Sstevel@tonic-gate free(gsbuffer); 2660Sstevel@tonic-gate if (algname != NULL) 2670Sstevel@tonic-gate free(algname); 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate return (newsalt); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * =========================================================================== 2740Sstevel@tonic-gate * The remainder of this file contains internal interfaces for 2750Sstevel@tonic-gate * the implementation of crypt(3c) and crypt_gensalt(3c) 2760Sstevel@tonic-gate * =========================================================================== 2770Sstevel@tonic-gate */ 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * getalgfromsalt - extract the algorithm name from the salt string 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate static char * 2840Sstevel@tonic-gate getalgfromsalt(const char *salt) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate char algname[CRYPT_MAXCIPHERTEXTLEN]; 2870Sstevel@tonic-gate int i; 2880Sstevel@tonic-gate int j; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate if (salt == NULL || strlen(salt) > CRYPT_MAXCIPHERTEXTLEN) 2910Sstevel@tonic-gate return (NULL); 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * Salts are in this format: 2940Sstevel@tonic-gate * $<algname>[,var=val,[var=val ...][$puresalt]$<ciphertext> 2950Sstevel@tonic-gate * 2960Sstevel@tonic-gate * The only bit we need to worry about here is extracting the 2970Sstevel@tonic-gate * name which is the string between the first "$" and the first 2980Sstevel@tonic-gate * of "," or second "$". 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate if (salt[0] != '$') { 3010Sstevel@tonic-gate return (strdup(CRYPT_UNIX)); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate i = 1; 3050Sstevel@tonic-gate j = 0; 3060Sstevel@tonic-gate while (salt[i] != '\0' && salt[i] != '$' && salt[i] != ',') { 3070Sstevel@tonic-gate algname[j] = salt[i]; 3080Sstevel@tonic-gate i++; 3090Sstevel@tonic-gate j++; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate if (j == 0) 3120Sstevel@tonic-gate return (NULL); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate algname[j] = '\0'; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate return (strdup(algname)); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * log_invalid_policy - syslog helper 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate static void 3240Sstevel@tonic-gate log_invalid_policy(enum crypt_policy_error_e error, char *value) 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate switch (error) { 3270Sstevel@tonic-gate case CPE_BOTH: 3280Sstevel@tonic-gate syslog(LOG_AUTH | LOG_ERR, 3290Sstevel@tonic-gate "crypt(3c): %s contains both %s and %s; only one may be " 3300Sstevel@tonic-gate "specified, using first entry in file.", POLICY_CONF_FILE, 3310Sstevel@tonic-gate CRYPT_ALGORITHMS_ALLOW, CRYPT_ALGORITHMS_DEPRECATE); 3320Sstevel@tonic-gate break; 3330Sstevel@tonic-gate case CPE_MULTI: 3340Sstevel@tonic-gate syslog(LOG_AUTH | LOG_ERR, 3350Sstevel@tonic-gate "crypt(3c): %s contains multiple %s entries;" 3360Sstevel@tonic-gate "using first entry file.", POLICY_CONF_FILE, value); 3370Sstevel@tonic-gate break; 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate static char * 3420Sstevel@tonic-gate getval(const char *ival) 3430Sstevel@tonic-gate { 3440Sstevel@tonic-gate char *tmp; 3450Sstevel@tonic-gate char *oval; 3460Sstevel@tonic-gate int off; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate if (ival == NULL) 3490Sstevel@tonic-gate return (NULL); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate if ((tmp = strchr(ival, '=')) == NULL) 3520Sstevel@tonic-gate return (NULL); 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate oval = strdup(tmp + 1); /* everything after the "=" */ 3550Sstevel@tonic-gate if (oval == NULL) 3560Sstevel@tonic-gate return (NULL); 3570Sstevel@tonic-gate off = strlen(oval) - 1; 3580Sstevel@tonic-gate if (off < 0) { 3590Sstevel@tonic-gate free(oval); 3600Sstevel@tonic-gate return (NULL); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate if (oval[off] == '\n') 3630Sstevel@tonic-gate oval[off] = '\0'; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate return (oval); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * getcryptpolicy - read /etc/security/policy.conf into a crypt_policy_s 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate static struct crypt_policy_s * 3720Sstevel@tonic-gate getcryptpolicy(void) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate FILE *pconf; 3750Sstevel@tonic-gate char line[BUFSIZ]; 3760Sstevel@tonic-gate struct crypt_policy_s *policy; 3770Sstevel@tonic-gate 3781914Scasper if ((pconf = fopen(POLICY_CONF_FILE, "rF")) == NULL) { 3790Sstevel@tonic-gate return (NULL); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate policy = malloc(sizeof (struct crypt_policy_s)); 3830Sstevel@tonic-gate if (policy == NULL) { 3840Sstevel@tonic-gate return (NULL); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate policy->cp_default = NULL; 3870Sstevel@tonic-gate policy->cp_allow = NULL; 3880Sstevel@tonic-gate policy->cp_deny = NULL; 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate while (!feof(pconf) && 3910Sstevel@tonic-gate (fgets(line, sizeof (line), pconf) != NULL)) { 3920Sstevel@tonic-gate if (strncasecmp(CRYPT_DEFAULT, line, 3930Sstevel@tonic-gate strlen(CRYPT_DEFAULT)) == 0) { 3940Sstevel@tonic-gate if (policy->cp_default != NULL) { 3950Sstevel@tonic-gate log_invalid_policy(CPE_MULTI, CRYPT_DEFAULT); 3960Sstevel@tonic-gate } else { 3970Sstevel@tonic-gate policy->cp_default = getval(line); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate if (strncasecmp(CRYPT_ALGORITHMS_ALLOW, line, 4010Sstevel@tonic-gate strlen(CRYPT_ALGORITHMS_ALLOW)) == 0) { 4020Sstevel@tonic-gate if (policy->cp_deny != NULL) { 4030Sstevel@tonic-gate log_invalid_policy(CPE_BOTH, NULL); 4040Sstevel@tonic-gate } else if (policy->cp_allow != NULL) { 4050Sstevel@tonic-gate log_invalid_policy(CPE_MULTI, 4060Sstevel@tonic-gate CRYPT_ALGORITHMS_ALLOW); 4070Sstevel@tonic-gate } else { 4080Sstevel@tonic-gate policy->cp_allow = getval(line); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate if (strncasecmp(CRYPT_ALGORITHMS_DEPRECATE, line, 4120Sstevel@tonic-gate strlen(CRYPT_ALGORITHMS_DEPRECATE)) == 0) { 4130Sstevel@tonic-gate if (policy->cp_allow != NULL) { 4140Sstevel@tonic-gate log_invalid_policy(CPE_BOTH, NULL); 4150Sstevel@tonic-gate } else if (policy->cp_deny != NULL) { 4160Sstevel@tonic-gate log_invalid_policy(CPE_MULTI, 4170Sstevel@tonic-gate CRYPT_ALGORITHMS_DEPRECATE); 4180Sstevel@tonic-gate } else { 4190Sstevel@tonic-gate policy->cp_deny = getval(line); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate (void) fclose(pconf); 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate if (policy->cp_default == NULL) { 4260Sstevel@tonic-gate policy->cp_default = strdup(CRYPT_UNIX); 4270Sstevel@tonic-gate if (policy->cp_default == NULL) 4280Sstevel@tonic-gate free_crypt_policy(policy); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate return (policy); 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * alg_valid - is this algorithm valid given the policy ? 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate static boolean_t 4390Sstevel@tonic-gate alg_valid(const char *algname, const struct crypt_policy_s *policy) 4400Sstevel@tonic-gate { 4410Sstevel@tonic-gate char *lasts; 4420Sstevel@tonic-gate char *list; 4430Sstevel@tonic-gate char *entry; 4440Sstevel@tonic-gate boolean_t allowed = B_FALSE; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate if ((algname == NULL) || (policy == NULL)) { 4470Sstevel@tonic-gate return (B_FALSE); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate if (strcmp(algname, policy->cp_default) == 0) { 4510Sstevel@tonic-gate return (B_TRUE); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate if (policy->cp_deny != NULL) { 4550Sstevel@tonic-gate list = policy->cp_deny; 4560Sstevel@tonic-gate allowed = B_FALSE; 4570Sstevel@tonic-gate } else if (policy->cp_allow != NULL) { 4580Sstevel@tonic-gate list = policy->cp_allow; 4590Sstevel@tonic-gate allowed = B_TRUE; 4600Sstevel@tonic-gate } else { 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * Neither of allow or deny policies are set so anything goes. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate return (B_TRUE); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate lasts = list; 4670Sstevel@tonic-gate while ((entry = strtok_r(NULL, ",", &lasts)) != NULL) { 4680Sstevel@tonic-gate if (strcmp(entry, algname) == 0) { 4690Sstevel@tonic-gate return (allowed); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate return (!allowed); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * getalgbyname - read crypt.conf(4) looking for algname 4780Sstevel@tonic-gate * 4790Sstevel@tonic-gate * RETURN VALUES 4800Sstevel@tonic-gate * On error NULL and errno is set 4810Sstevel@tonic-gate * On success the alg details including an open handle to the lib 4820Sstevel@tonic-gate * If crypt.conf(4) is okay but algname doesn't exist in it then 4830Sstevel@tonic-gate * return NULL the caller should then use the default algorithm 4840Sstevel@tonic-gate * as per the policy. 4850Sstevel@tonic-gate */ 4860Sstevel@tonic-gate static struct crypt_alg_s * 4870Sstevel@tonic-gate getalgbyname(const char *algname, boolean_t *found) 4880Sstevel@tonic-gate { 4890Sstevel@tonic-gate struct stat stb; 4900Sstevel@tonic-gate int configfd; 4910Sstevel@tonic-gate FILE *fconf = NULL; 4920Sstevel@tonic-gate struct crypt_alg_s *alg = NULL; 4930Sstevel@tonic-gate char line[CRYPT_CONFLINELENGTH]; 4940Sstevel@tonic-gate int linelen = 0; 4950Sstevel@tonic-gate int lineno = 0; 4960Sstevel@tonic-gate char *pathname = NULL; 4970Sstevel@tonic-gate char *lasts = NULL; 4980Sstevel@tonic-gate char *token = NULL; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate *found = B_FALSE; 5010Sstevel@tonic-gate if ((algname == NULL) || (strcmp(algname, CRYPT_UNIX) == 0)) { 5020Sstevel@tonic-gate return (NULL); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate if ((configfd = open(CRYPT_CONFFILE, O_RDONLY)) == -1) { 5060Sstevel@tonic-gate syslog(LOG_ALERT, "crypt: open(%s) failed: %s", 507*6812Sraf CRYPT_CONFFILE, strerror(errno)); 5080Sstevel@tonic-gate return (NULL); 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * Stat the file so we can check modes and ownerships 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate if (fstat(configfd, &stb) < 0) { 5150Sstevel@tonic-gate syslog(LOG_ALERT, "crypt: stat(%s) failed: %s", 516*6812Sraf CRYPT_CONFFILE, strerror(errno)); 5170Sstevel@tonic-gate goto cleanup; 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* 5210Sstevel@tonic-gate * Check the ownership of the file 5220Sstevel@tonic-gate */ 5230Sstevel@tonic-gate if (stb.st_uid != (uid_t)0) { 5240Sstevel@tonic-gate syslog(LOG_ALERT, 5250Sstevel@tonic-gate "crypt: Owner of %s is not root", CRYPT_CONFFILE); 5260Sstevel@tonic-gate goto cleanup; 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* 5300Sstevel@tonic-gate * Check the modes on the file 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate if (stb.st_mode & S_IWGRP) { 5330Sstevel@tonic-gate syslog(LOG_ALERT, 5340Sstevel@tonic-gate "crypt: %s writable by group", CRYPT_CONFFILE); 5350Sstevel@tonic-gate goto cleanup; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate if (stb.st_mode & S_IWOTH) { 5380Sstevel@tonic-gate syslog(LOG_ALERT, 539*6812Sraf "crypt: %s writable by world", CRYPT_CONFFILE); 5400Sstevel@tonic-gate goto cleanup; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5431914Scasper if ((fconf = fdopen(configfd, "rF")) == NULL) { 5440Sstevel@tonic-gate syslog(LOG_ALERT, "crypt: fdopen(%d) failed: %s", 545*6812Sraf configfd, strerror(errno)); 5460Sstevel@tonic-gate goto cleanup; 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate /* 5500Sstevel@tonic-gate * /etc/security/crypt.conf has 3 fields: 5510Sstevel@tonic-gate * <algname> <pathname> [<name[=val]>[<name[=val]>]] 5520Sstevel@tonic-gate */ 5530Sstevel@tonic-gate errno = 0; 5540Sstevel@tonic-gate while (!(*found) && 5550Sstevel@tonic-gate ((fgets(line, sizeof (line), fconf) != NULL) && !feof(fconf))) { 5560Sstevel@tonic-gate lineno++; 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * Skip over comments 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate if ((line[0] == '#') || (line[0] == '\n')) { 5610Sstevel@tonic-gate continue; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate linelen = strlen(line); 5650Sstevel@tonic-gate line[--linelen] = '\0'; /* chop the trailing \n */ 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate token = strtok_r(line, " \t", &lasts); 5680Sstevel@tonic-gate if (token == NULL) { 5690Sstevel@tonic-gate continue; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate if (strcmp(token, algname) == 0) { 5720Sstevel@tonic-gate *found = B_TRUE; 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate if (!found) { 5760Sstevel@tonic-gate errno = EINVAL; 5770Sstevel@tonic-gate goto cleanup; 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate token = strtok_r(NULL, " \t", &lasts); 5810Sstevel@tonic-gate if (token == NULL) { 5820Sstevel@tonic-gate /* 5830Sstevel@tonic-gate * Broken config file 5840Sstevel@tonic-gate */ 5850Sstevel@tonic-gate syslog(LOG_ALERT, "crypt(3c): %s may be corrupt at line %d", 5860Sstevel@tonic-gate CRYPT_CONFFILE, lineno); 5870Sstevel@tonic-gate *found = B_FALSE; 5880Sstevel@tonic-gate errno = EINVAL; 5890Sstevel@tonic-gate goto cleanup; 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate if ((pathname = isa_path(token)) == NULL) { 5930Sstevel@tonic-gate if (errno != ENOMEM) 5940Sstevel@tonic-gate errno = EINVAL; 5950Sstevel@tonic-gate *found = B_FALSE; 5960Sstevel@tonic-gate goto cleanup; 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate if ((alg = malloc(sizeof (struct crypt_alg_s))) == NULL) { 6000Sstevel@tonic-gate *found = B_FALSE; 6010Sstevel@tonic-gate goto cleanup; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate alg->a_libhandle = NULL; 6040Sstevel@tonic-gate alg->a_genhash = NULL; 6050Sstevel@tonic-gate alg->a_gensalt = NULL; 6060Sstevel@tonic-gate alg->a_params = NULL; 6070Sstevel@tonic-gate alg->a_nparams = 0; 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * The rest of the line is module specific params, space 6110Sstevel@tonic-gate * seprated. We wait until after we have checked the module is 6120Sstevel@tonic-gate * valid before parsing them into a_params, this saves us 6130Sstevel@tonic-gate * having to free them later if there is a problem. 6140Sstevel@tonic-gate */ 6150Sstevel@tonic-gate if ((alg->a_libhandle = dlopen(pathname, RTLD_NOW)) == NULL) { 6160Sstevel@tonic-gate syslog(LOG_ERR, "crypt(3c) unable to dlopen %s: %s", 6170Sstevel@tonic-gate pathname, dlerror()); 6180Sstevel@tonic-gate errno = ELIBACC; 6190Sstevel@tonic-gate *found = B_FALSE; 6200Sstevel@tonic-gate goto cleanup; 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate alg->a_genhash = 6240Sstevel@tonic-gate (char *(*)())dlsym(alg->a_libhandle, "crypt_genhash_impl"); 6250Sstevel@tonic-gate if (alg->a_genhash == NULL) { 6260Sstevel@tonic-gate syslog(LOG_ERR, "crypt(3c) unable to find cryp_genhash_impl" 6270Sstevel@tonic-gate "symbol in %s: %s", pathname, dlerror()); 6280Sstevel@tonic-gate errno = ELIBACC; 6290Sstevel@tonic-gate *found = B_FALSE; 6300Sstevel@tonic-gate goto cleanup; 6310Sstevel@tonic-gate } 6320Sstevel@tonic-gate alg->a_gensalt = 6330Sstevel@tonic-gate (char *(*)())dlsym(alg->a_libhandle, "crypt_gensalt_impl"); 6340Sstevel@tonic-gate if (alg->a_gensalt == NULL) { 6350Sstevel@tonic-gate syslog(LOG_ERR, "crypt(3c) unable to find crypt_gensalt_impl" 6360Sstevel@tonic-gate "symbol in %s: %s", pathname, dlerror()); 6370Sstevel@tonic-gate errno = ELIBACC; 6380Sstevel@tonic-gate *found = B_FALSE; 6390Sstevel@tonic-gate goto cleanup; 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * We have a good module so build the a_params if we have any. 6440Sstevel@tonic-gate * Count how much space we need first and then allocate an array 6450Sstevel@tonic-gate * to hold that many module params. 6460Sstevel@tonic-gate */ 6470Sstevel@tonic-gate if (lasts != NULL) { 6480Sstevel@tonic-gate int nparams = 0; 6490Sstevel@tonic-gate char *tparams; 6500Sstevel@tonic-gate char *tplasts; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate if ((tparams = strdup(lasts)) == NULL) { 6530Sstevel@tonic-gate *found = B_FALSE; 6540Sstevel@tonic-gate goto cleanup; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate (void) strtok_r(tparams, " \t", &tplasts); 6580Sstevel@tonic-gate do { 6590Sstevel@tonic-gate nparams++; 6600Sstevel@tonic-gate } while (strtok_r(NULL, " \t", &tplasts) != NULL); 6610Sstevel@tonic-gate free(tparams); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate alg->a_params = calloc(nparams + 1, sizeof (char *)); 6640Sstevel@tonic-gate if (alg->a_params == NULL) { 6650Sstevel@tonic-gate *found = B_FALSE; 6660Sstevel@tonic-gate goto cleanup; 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate while ((token = strtok_r(NULL, " \t", &lasts)) != NULL) { 6700Sstevel@tonic-gate alg->a_params[alg->a_nparams++] = token; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate cleanup: 6750Sstevel@tonic-gate if (*found == B_FALSE) { 6760Sstevel@tonic-gate free_crypt_alg(alg); 6770Sstevel@tonic-gate alg = NULL; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate if (pathname != NULL) { 6810Sstevel@tonic-gate free(pathname); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate if (fconf != NULL) { 6850Sstevel@tonic-gate (void) fclose(fconf); 6860Sstevel@tonic-gate } else { 6870Sstevel@tonic-gate (void) close(configfd); 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate return (alg); 6910Sstevel@tonic-gate } 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate static void 6940Sstevel@tonic-gate free_crypt_alg(struct crypt_alg_s *alg) 6950Sstevel@tonic-gate { 6960Sstevel@tonic-gate if (alg == NULL) 6970Sstevel@tonic-gate return; 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate if (alg->a_libhandle != NULL) { 7000Sstevel@tonic-gate (void) dlclose(alg->a_libhandle); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate if (alg->a_nparams != NULL) { 7030Sstevel@tonic-gate free(alg->a_params); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate free(alg); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate static void 7090Sstevel@tonic-gate free_crypt_policy(struct crypt_policy_s *policy) 7100Sstevel@tonic-gate { 7110Sstevel@tonic-gate if (policy == NULL) 7120Sstevel@tonic-gate return; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate if (policy->cp_default != NULL) { 7150Sstevel@tonic-gate bzero(policy->cp_default, strlen(policy->cp_default)); 7160Sstevel@tonic-gate free(policy->cp_default); 7170Sstevel@tonic-gate policy->cp_default = NULL; 7180Sstevel@tonic-gate } 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate if (policy->cp_allow != NULL) { 7210Sstevel@tonic-gate bzero(policy->cp_allow, strlen(policy->cp_allow)); 7220Sstevel@tonic-gate free(policy->cp_allow); 7230Sstevel@tonic-gate policy->cp_allow = NULL; 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate if (policy->cp_deny != NULL) { 7270Sstevel@tonic-gate bzero(policy->cp_deny, strlen(policy->cp_deny)); 7280Sstevel@tonic-gate free(policy->cp_deny); 7290Sstevel@tonic-gate policy->cp_deny = NULL; 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate free(policy); 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate /* 7370Sstevel@tonic-gate * isa_path - prepend the default dir or patch up the $ISA in path 7380Sstevel@tonic-gate * Caller is responsible for calling free(3c) on the result. 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate static char * 7410Sstevel@tonic-gate isa_path(const char *path) 7420Sstevel@tonic-gate { 7430Sstevel@tonic-gate char *ret = NULL; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate if ((path == NULL) || (strlen(path) > PATH_MAX)) { 7460Sstevel@tonic-gate return (NULL); 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate ret = calloc(PATH_MAX, sizeof (char)); 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* 7520Sstevel@tonic-gate * Module path doesn't start with "/" then prepend 7530Sstevel@tonic-gate * the default search path CRYPT_MODULE_DIR (/usr/lib/security/$ISA) 7540Sstevel@tonic-gate */ 7550Sstevel@tonic-gate if (path[0] != '/') { 7560Sstevel@tonic-gate if (snprintf(ret, PATH_MAX, "%s%s", CRYPT_MODULE_DIR, 7570Sstevel@tonic-gate path) > PATH_MAX) { 7580Sstevel@tonic-gate free(ret); 7590Sstevel@tonic-gate return (NULL); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate } else { /* patch up $ISA */ 7620Sstevel@tonic-gate char *isa; 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate if ((isa = strstr(path, CRYPT_MODULE_ISA)) != NULL) { 7650Sstevel@tonic-gate *isa = '\0'; 7660Sstevel@tonic-gate isa += strlen(CRYPT_MODULE_ISA); 7670Sstevel@tonic-gate if (snprintf(ret, PATH_MAX, "%s%s%s", path, 7680Sstevel@tonic-gate CRYPT_ISA_DIR, isa) > PATH_MAX) { 7690Sstevel@tonic-gate free(ret); 7700Sstevel@tonic-gate return (NULL); 7710Sstevel@tonic-gate } 7720Sstevel@tonic-gate } else { 7730Sstevel@tonic-gate free(ret); 7740Sstevel@tonic-gate ret = strdup(path); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate return (ret); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate /*ARGSUSED*/ 7830Sstevel@tonic-gate static char * 7840Sstevel@tonic-gate _unix_crypt_gensalt(char *gsbuffer, 7850Sstevel@tonic-gate size_t gsbufflen, 7860Sstevel@tonic-gate const char *oldpuresalt, 7870Sstevel@tonic-gate const struct passwd *userinfo, 7880Sstevel@tonic-gate const char *argv[]) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate static const char saltchars[] = 7910Sstevel@tonic-gate "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 7920Sstevel@tonic-gate struct timeval tv; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate gettimeofday(&tv, (void *) 0); 7950Sstevel@tonic-gate srand48(tv.tv_sec ^ tv.tv_usec); 7960Sstevel@tonic-gate gsbuffer[0] = saltchars[lrand48() % 64]; /* lrand48() is MT-SAFE */ 7970Sstevel@tonic-gate gsbuffer[1] = saltchars[lrand48() % 64]; /* lrand48() is MT-SAFE */ 7980Sstevel@tonic-gate gsbuffer[2] = '\0'; 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate return (gsbuffer); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate /* 8040Sstevel@tonic-gate * The rest of the code below comes from the old crypt.c and is the 8050Sstevel@tonic-gate * implementation of the hardwired/fallback traditional algorithm 8060Sstevel@tonic-gate * It has been otimized to take better advantage of MT features. 8070Sstevel@tonic-gate * 8080Sstevel@tonic-gate * It is included here to reduce the overhead of dlopen() 8090Sstevel@tonic-gate * for the common case. 8100Sstevel@tonic-gate */ 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 8140Sstevel@tonic-gate /* All Rights Reserved */ 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * This program implements a data encryption algorithm to encrypt passwords. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate 8220Sstevel@tonic-gate static mutex_t crypt_lock = DEFAULTMUTEX; 8230Sstevel@tonic-gate #define TSDBUFSZ (66 + 16) 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate static const char IP[] = { 8260Sstevel@tonic-gate 58, 50, 42, 34, 26, 18, 10, 2, 8270Sstevel@tonic-gate 60, 52, 44, 36, 28, 20, 12, 4, 8280Sstevel@tonic-gate 62, 54, 46, 38, 30, 22, 14, 6, 8290Sstevel@tonic-gate 64, 56, 48, 40, 32, 24, 16, 8, 8300Sstevel@tonic-gate 57, 49, 41, 33, 25, 17, 9, 1, 8310Sstevel@tonic-gate 59, 51, 43, 35, 27, 19, 11, 3, 8320Sstevel@tonic-gate 61, 53, 45, 37, 29, 21, 13, 5, 8330Sstevel@tonic-gate 63, 55, 47, 39, 31, 23, 15, 7, 8340Sstevel@tonic-gate }; 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate static const char FP[] = { 8370Sstevel@tonic-gate 40, 8, 48, 16, 56, 24, 64, 32, 8380Sstevel@tonic-gate 39, 7, 47, 15, 55, 23, 63, 31, 8390Sstevel@tonic-gate 38, 6, 46, 14, 54, 22, 62, 30, 8400Sstevel@tonic-gate 37, 5, 45, 13, 53, 21, 61, 29, 8410Sstevel@tonic-gate 36, 4, 44, 12, 52, 20, 60, 28, 8420Sstevel@tonic-gate 35, 3, 43, 11, 51, 19, 59, 27, 8430Sstevel@tonic-gate 34, 2, 42, 10, 50, 18, 58, 26, 8440Sstevel@tonic-gate 33, 1, 41, 9, 49, 17, 57, 25, 8450Sstevel@tonic-gate }; 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate static const char PC1_C[] = { 8480Sstevel@tonic-gate 57, 49, 41, 33, 25, 17, 9, 8490Sstevel@tonic-gate 1, 58, 50, 42, 34, 26, 18, 8500Sstevel@tonic-gate 10, 2, 59, 51, 43, 35, 27, 8510Sstevel@tonic-gate 19, 11, 3, 60, 52, 44, 36, 8520Sstevel@tonic-gate }; 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate static const char PC1_D[] = { 8550Sstevel@tonic-gate 63, 55, 47, 39, 31, 23, 15, 8560Sstevel@tonic-gate 7, 62, 54, 46, 38, 30, 22, 8570Sstevel@tonic-gate 14, 6, 61, 53, 45, 37, 29, 8580Sstevel@tonic-gate 21, 13, 5, 28, 20, 12, 4, 8590Sstevel@tonic-gate }; 8600Sstevel@tonic-gate 8610Sstevel@tonic-gate static const char shifts[] = { 8620Sstevel@tonic-gate 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 8630Sstevel@tonic-gate }; 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate static const char PC2_C[] = { 8660Sstevel@tonic-gate 14, 17, 11, 24, 1, 5, 8670Sstevel@tonic-gate 3, 28, 15, 6, 21, 10, 8680Sstevel@tonic-gate 23, 19, 12, 4, 26, 8, 8690Sstevel@tonic-gate 16, 7, 27, 20, 13, 2, 8700Sstevel@tonic-gate }; 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate static const char PC2_D[] = { 8730Sstevel@tonic-gate 41, 52, 31, 37, 47, 55, 8740Sstevel@tonic-gate 30, 40, 51, 45, 33, 48, 8750Sstevel@tonic-gate 44, 49, 39, 56, 34, 53, 8760Sstevel@tonic-gate 46, 42, 50, 36, 29, 32, 8770Sstevel@tonic-gate }; 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate static char C[28]; 8800Sstevel@tonic-gate static char D[28]; 8810Sstevel@tonic-gate static char *KS; 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate static char E[48]; 8840Sstevel@tonic-gate static const char e2[] = { 8850Sstevel@tonic-gate 32, 1, 2, 3, 4, 5, 8860Sstevel@tonic-gate 4, 5, 6, 7, 8, 9, 8870Sstevel@tonic-gate 8, 9, 10, 11, 12, 13, 8880Sstevel@tonic-gate 12, 13, 14, 15, 16, 17, 8890Sstevel@tonic-gate 16, 17, 18, 19, 20, 21, 8900Sstevel@tonic-gate 20, 21, 22, 23, 24, 25, 8910Sstevel@tonic-gate 24, 25, 26, 27, 28, 29, 8920Sstevel@tonic-gate 28, 29, 30, 31, 32, 1, 8930Sstevel@tonic-gate }; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * The KS array (768 bytes) is allocated once, and only if 8970Sstevel@tonic-gate * one of _unix_crypt(), encrypt() or setkey() is called. 8980Sstevel@tonic-gate * The complexity below is due to the fact that calloc() 8990Sstevel@tonic-gate * must not be called while holding any locks. 9000Sstevel@tonic-gate */ 9010Sstevel@tonic-gate static int 9020Sstevel@tonic-gate allocate_KS(void) 9030Sstevel@tonic-gate { 9040Sstevel@tonic-gate char *ks; 9050Sstevel@tonic-gate int failed; 9060Sstevel@tonic-gate int assigned; 9070Sstevel@tonic-gate 9083864Sraf if (KS != NULL) { /* already allocated */ 9093864Sraf membar_consumer(); 9100Sstevel@tonic-gate return (0); 9113864Sraf } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate ks = calloc(16, 48 * sizeof (char)); 9140Sstevel@tonic-gate failed = 0; 9150Sstevel@tonic-gate lmutex_lock(&crypt_lock); 9160Sstevel@tonic-gate if (KS != NULL) { /* someone else got here first */ 9170Sstevel@tonic-gate assigned = 0; 9180Sstevel@tonic-gate } else { 9190Sstevel@tonic-gate assigned = 1; 9203864Sraf membar_producer(); 9210Sstevel@tonic-gate if ((KS = ks) == NULL) /* calloc() failed */ 9220Sstevel@tonic-gate failed = 1; 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate lmutex_unlock(&crypt_lock); 9250Sstevel@tonic-gate if (!assigned) 9260Sstevel@tonic-gate free(ks); 9270Sstevel@tonic-gate return (failed); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate static void 9310Sstevel@tonic-gate unlocked_setkey(const char *key) 9320Sstevel@tonic-gate { 9330Sstevel@tonic-gate int i, j, k; 9340Sstevel@tonic-gate char t; 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate for (i = 0; i < 28; i++) { 9370Sstevel@tonic-gate C[i] = key[PC1_C[i]-1]; 9380Sstevel@tonic-gate D[i] = key[PC1_D[i]-1]; 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate for (i = 0; i < 16; i++) { 9410Sstevel@tonic-gate for (k = 0; k < shifts[i]; k++) { 9420Sstevel@tonic-gate t = C[0]; 9430Sstevel@tonic-gate for (j = 0; j < 28-1; j++) 9440Sstevel@tonic-gate C[j] = C[j+1]; 9450Sstevel@tonic-gate C[27] = t; 9460Sstevel@tonic-gate t = D[0]; 9470Sstevel@tonic-gate for (j = 0; j < 28-1; j++) 9480Sstevel@tonic-gate D[j] = D[j+1]; 9490Sstevel@tonic-gate D[27] = t; 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate for (j = 0; j < 24; j++) { 9520Sstevel@tonic-gate int index = i * 48; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate *(KS+index+j) = C[PC2_C[j]-1]; 9550Sstevel@tonic-gate *(KS+index+j+24) = D[PC2_D[j]-28-1]; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate for (i = 0; i < 48; i++) 9590Sstevel@tonic-gate E[i] = e2[i]; 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate static const char S[8][64] = { 9630Sstevel@tonic-gate 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 9640Sstevel@tonic-gate 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 9650Sstevel@tonic-gate 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 9660Sstevel@tonic-gate 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 9690Sstevel@tonic-gate 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 9700Sstevel@tonic-gate 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 9710Sstevel@tonic-gate 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 9740Sstevel@tonic-gate 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 9750Sstevel@tonic-gate 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 9760Sstevel@tonic-gate 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 9790Sstevel@tonic-gate 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 9800Sstevel@tonic-gate 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 9810Sstevel@tonic-gate 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 9840Sstevel@tonic-gate 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 9850Sstevel@tonic-gate 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 9860Sstevel@tonic-gate 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 9890Sstevel@tonic-gate 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9900Sstevel@tonic-gate 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 9910Sstevel@tonic-gate 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 9940Sstevel@tonic-gate 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 9950Sstevel@tonic-gate 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 9960Sstevel@tonic-gate 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 9990Sstevel@tonic-gate 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 10000Sstevel@tonic-gate 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 10010Sstevel@tonic-gate 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11, 10020Sstevel@tonic-gate }; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate static const char P[] = { 10050Sstevel@tonic-gate 16, 7, 20, 21, 10060Sstevel@tonic-gate 29, 12, 28, 17, 10070Sstevel@tonic-gate 1, 15, 23, 26, 10080Sstevel@tonic-gate 5, 18, 31, 10, 10090Sstevel@tonic-gate 2, 8, 24, 14, 10100Sstevel@tonic-gate 32, 27, 3, 9, 10110Sstevel@tonic-gate 19, 13, 30, 6, 10120Sstevel@tonic-gate 22, 11, 4, 25, 10130Sstevel@tonic-gate }; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate static char L[64]; 10160Sstevel@tonic-gate static char tempL[32]; 10170Sstevel@tonic-gate static char f[32]; 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate static char preS[48]; 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate /*ARGSUSED*/ 10220Sstevel@tonic-gate static void 10230Sstevel@tonic-gate unlocked_encrypt(char *block, int fake) 10240Sstevel@tonic-gate { 10250Sstevel@tonic-gate int i; 10260Sstevel@tonic-gate int t, j, k; 10270Sstevel@tonic-gate char *R = &L[32]; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate for (j = 0; j < 64; j++) 10300Sstevel@tonic-gate L[j] = block[IP[j]-1]; 10310Sstevel@tonic-gate for (i = 0; i < 16; i++) { 10320Sstevel@tonic-gate int index = i * 48; 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate for (j = 0; j < 32; j++) 10350Sstevel@tonic-gate tempL[j] = R[j]; 10360Sstevel@tonic-gate for (j = 0; j < 48; j++) 10370Sstevel@tonic-gate preS[j] = R[E[j]-1] ^ *(KS+index+j); 10380Sstevel@tonic-gate for (j = 0; j < 8; j++) { 10390Sstevel@tonic-gate t = 6 * j; 1040*6812Sraf k = S[j][(preS[t+0]<<5) + 1041*6812Sraf (preS[t+1]<<3) + 1042*6812Sraf (preS[t+2]<<2) + 1043*6812Sraf (preS[t+3]<<1) + 1044*6812Sraf (preS[t+4]<<0) + 1045*6812Sraf (preS[t+5]<<4)]; 10460Sstevel@tonic-gate t = 4*j; 10470Sstevel@tonic-gate f[t+0] = (k>>3)&01; 10480Sstevel@tonic-gate f[t+1] = (k>>2)&01; 10490Sstevel@tonic-gate f[t+2] = (k>>1)&01; 10500Sstevel@tonic-gate f[t+3] = (k>>0)&01; 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate for (j = 0; j < 32; j++) 10530Sstevel@tonic-gate R[j] = L[j] ^ f[P[j]-1]; 10540Sstevel@tonic-gate for (j = 0; j < 32; j++) 10550Sstevel@tonic-gate L[j] = tempL[j]; 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate for (j = 0; j < 32; j++) { 10580Sstevel@tonic-gate t = L[j]; 10590Sstevel@tonic-gate L[j] = R[j]; 10600Sstevel@tonic-gate R[j] = (char)t; 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate for (j = 0; j < 64; j++) 10630Sstevel@tonic-gate block[j] = L[FP[j]-1]; 10640Sstevel@tonic-gate } 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate char * 10670Sstevel@tonic-gate _unix_crypt(const char *pw, const char *salt, char *iobuf) 10680Sstevel@tonic-gate { 10690Sstevel@tonic-gate int c, i, j; 10700Sstevel@tonic-gate char temp; 10710Sstevel@tonic-gate char *block; 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate block = iobuf + 16; 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate if (iobuf == 0) { 10760Sstevel@tonic-gate errno = ENOMEM; 10770Sstevel@tonic-gate return (NULL); 10780Sstevel@tonic-gate } 10790Sstevel@tonic-gate if (allocate_KS() != 0) 10800Sstevel@tonic-gate return (NULL); 10810Sstevel@tonic-gate lmutex_lock(&crypt_lock); 10820Sstevel@tonic-gate for (i = 0; i < 66; i++) 10830Sstevel@tonic-gate block[i] = 0; 10840Sstevel@tonic-gate for (i = 0; (c = *pw) != '\0' && i < 64; pw++) { 10850Sstevel@tonic-gate for (j = 0; j < 7; j++, i++) 10860Sstevel@tonic-gate block[i] = (c>>(6-j)) & 01; 10870Sstevel@tonic-gate i++; 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate unlocked_setkey(block); 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate for (i = 0; i < 66; i++) 10930Sstevel@tonic-gate block[i] = 0; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate for (i = 0; i < 2; i++) { 10960Sstevel@tonic-gate c = *salt++; 10970Sstevel@tonic-gate iobuf[i] = (char)c; 10980Sstevel@tonic-gate if (c > 'Z') 10990Sstevel@tonic-gate c -= 6; 11000Sstevel@tonic-gate if (c > '9') 11010Sstevel@tonic-gate c -= 7; 11020Sstevel@tonic-gate c -= '.'; 11030Sstevel@tonic-gate for (j = 0; j < 6; j++) { 11040Sstevel@tonic-gate if ((c>>j) & 01) { 11050Sstevel@tonic-gate temp = E[6*i+j]; 11060Sstevel@tonic-gate E[6*i+j] = E[6*i+j+24]; 11070Sstevel@tonic-gate E[6*i+j+24] = temp; 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate for (i = 0; i < 25; i++) 11130Sstevel@tonic-gate unlocked_encrypt(block, 0); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate lmutex_unlock(&crypt_lock); 11160Sstevel@tonic-gate for (i = 0; i < 11; i++) { 11170Sstevel@tonic-gate c = 0; 11180Sstevel@tonic-gate for (j = 0; j < 6; j++) { 11190Sstevel@tonic-gate c <<= 1; 11200Sstevel@tonic-gate c |= block[6*i+j]; 11210Sstevel@tonic-gate } 11220Sstevel@tonic-gate c += '.'; 11230Sstevel@tonic-gate if (c > '9') 11240Sstevel@tonic-gate c += 7; 11250Sstevel@tonic-gate if (c > 'Z') 11260Sstevel@tonic-gate c += 6; 11270Sstevel@tonic-gate iobuf[i+2] = (char)c; 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate iobuf[i+2] = 0; 11300Sstevel@tonic-gate if (iobuf[1] == 0) 11310Sstevel@tonic-gate iobuf[1] = iobuf[0]; 11320Sstevel@tonic-gate return (iobuf); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /*ARGSUSED*/ 11370Sstevel@tonic-gate void 11380Sstevel@tonic-gate encrypt(char *block, int fake) 11390Sstevel@tonic-gate { 11400Sstevel@tonic-gate if (fake != 0) { 11410Sstevel@tonic-gate errno = ENOSYS; 11420Sstevel@tonic-gate return; 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate if (allocate_KS() != 0) 11450Sstevel@tonic-gate return; 11460Sstevel@tonic-gate lmutex_lock(&crypt_lock); 11470Sstevel@tonic-gate unlocked_encrypt(block, fake); 11480Sstevel@tonic-gate lmutex_unlock(&crypt_lock); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate void 11530Sstevel@tonic-gate setkey(const char *key) 11540Sstevel@tonic-gate { 11550Sstevel@tonic-gate if (allocate_KS() != 0) 11560Sstevel@tonic-gate return; 11570Sstevel@tonic-gate lmutex_lock(&crypt_lock); 11580Sstevel@tonic-gate unlocked_setkey(key); 11590Sstevel@tonic-gate lmutex_unlock(&crypt_lock); 11600Sstevel@tonic-gate } 1161