crypt.c revision 220497
1193326Sed/*
2193326Sed * Copyright (c) 1999
3193326Sed *      Mark Murray.  All rights reserved.
4193326Sed *
5193326Sed * Redistribution and use in source and binary forms, with or without
6193326Sed * modification, are permitted provided that the following conditions
7193326Sed * are met:
8193326Sed * 1. Redistributions of source code must retain the above copyright
9193326Sed *    notice, this list of conditions and the following disclaimer.
10193326Sed * 2. Redistributions in binary form must reproduce the above copyright
11193326Sed *    notice, this list of conditions and the following disclaimer in the
12193326Sed *    documentation and/or other materials provided with the distribution.
13193326Sed *
14280031Sdim * THIS SOFTWARE IS PROVIDED BY MARK MURRAY AND CONTRIBUTORS ``AS IS'' AND
15280031Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17193326Sed * ARE DISCLAIMED.  IN NO EVENT SHALL MARK MURRAY OR CONTRIBUTORS BE LIABLE
18193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23193326Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24193326Sed * SUCH DAMAGE.
25193326Sed */
26193326Sed
27193326Sed#include <sys/cdefs.h>
28193326Sed__FBSDID("$FreeBSD: head/lib/libcrypt/crypt.c 220497 2011-04-09 14:02:04Z markm $");
29193326Sed
30193326Sed#include <sys/types.h>
31193326Sed#include <string.h>
32198092Srdivacky#include <libutil.h>
33193326Sed#include <unistd.h>
34193326Sed#include "crypt.h"
35193326Sed
36193326Sedstatic const struct {
37193326Sed	const char *const name;
38198092Srdivacky	char *(*const func)(const char *, const char *);
39193326Sed	const char *const magic;
40193326Sed} crypt_types[] = {
41193326Sed#ifdef HAS_DES
42193326Sed	{
43193326Sed		"des",
44193326Sed		crypt_des,
45193326Sed		NULL
46	},
47#endif
48	{
49		"md5",
50		crypt_md5,
51		"$1$"
52	},
53#ifdef HAS_BLOWFISH
54	{
55		"blf",
56		crypt_blowfish,
57		"$2"
58	},
59#endif
60	{
61		"nth",
62		crypt_nthash,
63		"$3$"
64	},
65	{
66		"sha256",
67		sha256_crypt,
68		"$5$"
69	},
70	{
71		"sha512",
72		sha512_crypt,
73		"$6$"
74	},
75	{
76		NULL,
77		NULL,
78		NULL
79	}
80};
81
82static int crypt_type = -1;
83
84static void
85crypt_setdefault(void)
86{
87	char *def;
88	size_t i;
89
90	if (crypt_type != -1)
91		return;
92	def = auth_getval("crypt_default");
93	if (def == NULL) {
94		crypt_type = 0;
95		return;
96	}
97	for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) {
98		if (strcmp(def, crypt_types[i].name) == 0) {
99			crypt_type = (int)i;
100			return;
101		}
102	}
103	crypt_type = 0;
104}
105
106const char *
107crypt_get_format(void)
108{
109
110	crypt_setdefault();
111	return (crypt_types[crypt_type].name);
112}
113
114int
115crypt_set_format(const char *type)
116{
117	size_t i;
118
119	crypt_setdefault();
120	for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) {
121		if (strcmp(type, crypt_types[i].name) == 0) {
122			crypt_type = (int)i;
123			return (1);
124		}
125	}
126	return (0);
127}
128
129char *
130crypt(const char *passwd, const char *salt)
131{
132	size_t i;
133
134	crypt_setdefault();
135	for (i = 0; i < sizeof(crypt_types) / sizeof(crypt_types[0]) - 1; i++) {
136		if (crypt_types[i].magic != NULL && strncmp(salt,
137		    crypt_types[i].magic, strlen(crypt_types[i].magic)) == 0)
138			return (crypt_types[i].func(passwd, salt));
139	}
140	return (crypt_types[crypt_type].func(passwd, salt));
141}
142