crypt.c revision 74106
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. 2551462Smarkm * 2650476Speter * $FreeBSD: head/lib/libcrypt/crypt.c 74106 2001-03-11 16:05:43Z markm $ 2743092Smarkm * 2842981Sbrandon */ 2942981Sbrandon 3043092Smarkm#if defined(LIBC_SCCS) && !defined(lint) 3170419Speterstatic const char rcsid[] = 3270419Speter"$FreeBSD: head/lib/libcrypt/crypt.c 74106 2001-03-11 16:05:43Z markm $"; 3343092Smarkm#endif /* LIBC_SCCS and not lint */ 3442981Sbrandon 3570419Speter#include <sys/types.h> 3617141Sjkh#include <string.h> 3770419Speter#include <libutil.h> 3851462Smarkm#include "crypt.h" 391984Scsgr 4064918Sgreenstatic const struct { 4164918Sgreen const char *const name; 4264918Sgreen char *(*const func)(const char *, const char *); 4364918Sgreen const char *const magic; 4464918Sgreen} crypt_types[] = { 4570419Speter#ifdef HAS_DES 4670419Speter { 4764918Sgreen "des", 4864918Sgreen crypt_des, 4964918Sgreen NULL 5064918Sgreen }, 5165053Sgreen#endif 5264918Sgreen { 5370421Speter "md5", 5470421Speter crypt_md5, 5570421Speter "$1$" 5670421Speter }, 5774106Smarkm#ifdef HAS_BLOWFISH 5870421Speter { 5974106Smarkm "blf", 6074106Smarkm crypt_blowfish, 6174106Smarkm "$2" 6274106Smarkm }, 6374106Smarkm#endif 6474106Smarkm { 6564918Sgreen NULL, 6664918Sgreen NULL 6764918Sgreen } 6864918Sgreen}; 6964918Sgreen 7070419Speterstatic int crypt_type = -1; 7164918Sgreen 7270419Speterstatic void 7370419Spetercrypt_setdefault(void) 7470419Speter{ 7570419Speter char *def; 7670419Speter int i; 7770419Speter 7870419Speter if (crypt_type != -1) 7970419Speter return; 8070419Speter def = auth_getval("crypt_default"); 8170419Speter if (def == NULL) { 8270419Speter crypt_type = 0; 8370419Speter return; 8470419Speter } 8570419Speter for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 8670419Speter if (strcmp(def, crypt_types[i].name) == 0) { 8770419Speter crypt_type = i; 8870419Speter return; 8970419Speter } 9070419Speter } 9170419Speter crypt_type = 0; 9270419Speter} 9370419Speter 9464918Sgreenconst char * 9570419Spetercrypt_get_format(void) 9670419Speter{ 9764918Sgreen 9870419Speter crypt_setdefault(); 9964918Sgreen return (crypt_types[crypt_type].name); 10064918Sgreen} 10164918Sgreen 10264918Sgreenint 10370419Spetercrypt_set_format(char *type) 10470419Speter{ 10564918Sgreen int i; 10664918Sgreen 10770419Speter crypt_setdefault(); 10864918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 10964918Sgreen if (strcmp(type, crypt_types[i].name) == 0) { 11064918Sgreen crypt_type = i; 11164918Sgreen return (1); 11264918Sgreen } 11364918Sgreen } 11464918Sgreen return (0); 11564918Sgreen} 11664918Sgreen 11743092Smarkmchar * 11851462Smarkmcrypt(char *passwd, char *salt) 1191984Scsgr{ 12064918Sgreen int i; 12164918Sgreen 12270419Speter crypt_setdefault(); 12364918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 12464918Sgreen if (crypt_types[i].magic != NULL && strncmp(salt, 12564918Sgreen crypt_types[i].magic, strlen(crypt_types[i].magic)) == 0) 12664918Sgreen return (crypt_types[i].func(passwd, salt)); 12764918Sgreen } 12864918Sgreen return (crypt_types[crypt_type].func(passwd, salt)); 1291984Scsgr} 130