crypt.c revision 70421
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 70421 2000-12-28 11:23:01Z peter $ 2743092Smarkm * 2842981Sbrandon */ 2942981Sbrandon 3043092Smarkm#if defined(LIBC_SCCS) && !defined(lint) 3170419Speterstatic const char rcsid[] = 3270419Speter"$FreeBSD: head/lib/libcrypt/crypt.c 70421 2000-12-28 11:23:01Z peter $"; 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 }, 5770421Speter { 5864918Sgreen NULL, 5964918Sgreen NULL 6064918Sgreen } 6164918Sgreen}; 6264918Sgreen 6370419Speterstatic int crypt_type = -1; 6464918Sgreen 6570419Speterstatic void 6670419Spetercrypt_setdefault(void) 6770419Speter{ 6870419Speter char *def; 6970419Speter int i; 7070419Speter 7170419Speter if (crypt_type != -1) 7270419Speter return; 7370419Speter def = auth_getval("crypt_default"); 7470419Speter if (def == NULL) { 7570419Speter crypt_type = 0; 7670419Speter return; 7770419Speter } 7870419Speter for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 7970419Speter if (strcmp(def, crypt_types[i].name) == 0) { 8070419Speter crypt_type = i; 8170419Speter return; 8270419Speter } 8370419Speter } 8470419Speter crypt_type = 0; 8570419Speter} 8670419Speter 8764918Sgreenconst char * 8870419Spetercrypt_get_format(void) 8970419Speter{ 9064918Sgreen 9170419Speter crypt_setdefault(); 9264918Sgreen return (crypt_types[crypt_type].name); 9364918Sgreen} 9464918Sgreen 9564918Sgreenint 9670419Spetercrypt_set_format(char *type) 9770419Speter{ 9864918Sgreen int i; 9964918Sgreen 10070419Speter crypt_setdefault(); 10164918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 10264918Sgreen if (strcmp(type, crypt_types[i].name) == 0) { 10364918Sgreen crypt_type = i; 10464918Sgreen return (1); 10564918Sgreen } 10664918Sgreen } 10764918Sgreen return (0); 10864918Sgreen} 10964918Sgreen 11043092Smarkmchar * 11151462Smarkmcrypt(char *passwd, char *salt) 1121984Scsgr{ 11364918Sgreen int i; 11464918Sgreen 11570419Speter crypt_setdefault(); 11664918Sgreen for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) { 11764918Sgreen if (crypt_types[i].magic != NULL && strncmp(salt, 11864918Sgreen crypt_types[i].magic, strlen(crypt_types[i].magic)) == 0) 11964918Sgreen return (crypt_types[i].func(passwd, salt)); 12064918Sgreen } 12164918Sgreen return (crypt_types[crypt_type].func(passwd, salt)); 1221984Scsgr} 123