crypt.c revision 91754
11984Scsgr/* 251462Smarkm * Copyright (c) 1999 351462Smarkm * Mark Murray. All rights reserved. 443092Smarkm * 551462Smarkm * Redistribution and use in source and binary forms, with or without 651462Smarkm * modification, are permitted provided that the following conditions 751462Smarkm * are met: 851462Smarkm * 1. Redistributions of source code must retain the above copyright 951462Smarkm * notice, this list of conditions and the following disclaimer. 1051462Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1151462Smarkm * notice, this list of conditions and the following disclaimer in the 1251462Smarkm * documentation and/or other materials provided with the distribution. 1351462Smarkm * 1451462Smarkm * THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND 1551462Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1651462Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1751462Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL MARK MURRAY OR CONTRIBUTORS BE LIABLE 1851462Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1951462Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2051462Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2151462Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2251462Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2351462Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2451462Smarkm * SUCH DAMAGE. 2542981Sbrandon */ 2642981Sbrandon 2783551Sdillon#include <sys/cdefs.h> 2883551Sdillon__FBSDID("$FreeBSD: head/lib/libcrypt/crypt.c 91754 2002-03-06 17:18:09Z markm $"); 2942981Sbrandon 3070419Speter#include <sys/types.h> 3117141Sjkh#include <string.h> 3270419Speter#include <libutil.h> 3391754Smarkm#include <unistd.h> 3451462Smarkm#include "crypt.h" 351984Scsgr 3664918Sgreenstatic const struct { 3764918Sgreen const char *const name; 3864918Sgreen char *(*const func)(const char *, const char *); 3964918Sgreen const char *const magic; 4064918Sgreen} crypt_types[] = { 4170419Speter#ifdef HAS_DES 4270419Speter { 4364918Sgreen "des", 4464918Sgreen crypt_des, 4564918Sgreen NULL 4664918Sgreen }, 4765053Sgreen#endif 4864918Sgreen { 4970421Speter "md5", 5070421Speter crypt_md5, 5170421Speter "$1$" 5270421Speter }, 5374106Smarkm#ifdef HAS_BLOWFISH 5470421Speter { 5574106Smarkm "blf", 5674106Smarkm crypt_blowfish, 5774106Smarkm "$2" 5874106Smarkm }, 5974106Smarkm#endif 6074106Smarkm { 6164918Sgreen NULL, 6291754Smarkm NULL, 6364918Sgreen NULL 6464918Sgreen } 6564918Sgreen}; 6664918Sgreen 6770419Speterstatic int crypt_type = -1; 6864918Sgreen 6970419Speterstatic void 7070419Spetercrypt_setdefault(void) 7170419Speter{ 7270419Speter char *def; 7391754Smarkm size_t i; 7470419Speter 7570419Speter if (crypt_type != -1) 7670419Speter return; 7770419Speter def = auth_getval("crypt_default"); 7870419Speter if (def == NULL) { 7970419Speter crypt_type = 0; 8070419Speter return; 8170419Speter } 8270419Speter for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 8370419Speter if (strcmp(def, crypt_types[i].name) == 0) { 8491754Smarkm crypt_type = (int)i; 8570419Speter return; 8670419Speter } 8770419Speter } 8870419Speter crypt_type = 0; 8970419Speter} 9070419Speter 9164918Sgreenconst char * 9270419Spetercrypt_get_format(void) 9370419Speter{ 9464918Sgreen 9570419Speter crypt_setdefault(); 9664918Sgreen return (crypt_types[crypt_type].name); 9764918Sgreen} 9864918Sgreen 9964918Sgreenint 10091754Smarkmcrypt_set_format(const char *type) 10170419Speter{ 10291754Smarkm size_t i; 10364918Sgreen 10470419Speter crypt_setdefault(); 10564918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 10664918Sgreen if (strcmp(type, crypt_types[i].name) == 0) { 10791754Smarkm crypt_type = (int)i; 10864918Sgreen return (1); 10964918Sgreen } 11064918Sgreen } 11164918Sgreen return (0); 11264918Sgreen} 11364918Sgreen 11443092Smarkmchar * 11591754Smarkmcrypt(const char *passwd, const char *salt) 1161984Scsgr{ 11791754Smarkm size_t i; 11864918Sgreen 11970419Speter crypt_setdefault(); 12064918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 12164918Sgreen if (crypt_types[i].magic != NULL && strncmp(salt, 12264918Sgreen crypt_types[i].magic, strlen(crypt_types[i].magic)) == 0) 12364918Sgreen return (crypt_types[i].func(passwd, salt)); 12464918Sgreen } 12564918Sgreen return (crypt_types[crypt_type].func(passwd, salt)); 1261984Scsgr} 127