2021-12-05 23:35:27 +08:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>
|
|
|
|
* SPDX-FileCopyrightText: J.T. Conklin <jtc@netbsd.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense
|
|
|
|
*/
|
|
|
|
|
2007-10-07 19:44:02 +08:00
|
|
|
/*
|
|
|
|
* salt.c - generate a random salt string for crypt()
|
|
|
|
*
|
|
|
|
* Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
|
2008-04-27 08:40:09 +08:00
|
|
|
* it is in the public domain.
|
2007-10-07 19:44:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 07:46:11 +08:00
|
|
|
#ident "$Id$"
|
2007-10-07 19:47:01 +08:00
|
|
|
|
2007-11-24 04:51:43 +08:00
|
|
|
#include <assert.h>
|
2021-06-23 22:06:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
Use bzero(3) instead of its pattern
It was blessed by POSIX.1-2001, and GCC says that it won't go away,
possibly ever.
memset(3) is dangerous, as the 2nd and 3rd arguments can be accidentally
swapped --who remembers what's the order of the 2nd and 3rd parameters
to memset(3) without checking the manual page or some code that uses
it?--. Some recent compilers may be able to catch that via some
warnings, but those are not infalible. And even if compiler warnings
could always catch that, the time lost in fixing or checking the docs is
lost for no clear gain. Having a sane API that is unambiguous is the
Right Thing (tm); and that API is bzero(3).
If someone doesn't believe memset(3) is error-prone, please read the
book "Unix Network Programming", Volume 1, 3rd Edition by Stevens, et
al., Section 1.2. See a stackoverflow reference in the link below[1].
bzero(3) had a bad fame in the bad old days, because some ancient
systems (I'm talking of many decades ago) shipped a broken version of
bzero(3). We can assume that all systems in which current shadow utils
can be built, have a working version of bzero(3) --if not, please fix
your broken system; don't blame the programmer--.
One reason that some use today to avoid bzero(3) in favor of memset(3)
is that memset(3) is more often used; but that's a circular reasoning.
Even if bzero(3) wasn't supported by the system, it would need to be
invented. It's the right API.
Another reason that some argue is that POSIX.1-2008 removed the
specification of bzero(3). That's not a problem, because GCC will
probably support it forever, and even if it didn't, we can redefine it
like we do with memzero(). bzero(3) is just a one-liner wrapper around
memset(3).
Link: [1] <https://stackoverflow.com/a/17097978>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-02 00:27:50 +08:00
|
|
|
#include <strings.h>
|
|
|
|
|
2007-10-07 19:44:02 +08:00
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "getdef.h"
|
2021-11-29 07:37:53 +08:00
|
|
|
#include "shadowlog.h"
|
2007-11-16 20:36:21 +08:00
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
#if (defined CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY && \
|
|
|
|
CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY)
|
|
|
|
#define USE_XCRYPT_GENSALT 1
|
|
|
|
#else
|
|
|
|
#define USE_XCRYPT_GENSALT 0
|
|
|
|
#endif
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/* Add the salt prefix. */
|
|
|
|
#define MAGNUM(array,ch) (array)[0]=(array)[2]='$',(array)[1]=(ch),(array)[3]='\0'
|
|
|
|
|
|
|
|
#ifdef USE_BCRYPT
|
|
|
|
/* Use $2b$ as prefix for compatibility with OpenBSD's bcrypt. */
|
|
|
|
#define BCRYPTMAGNUM(array) (array)[0]=(array)[3]='$',(array)[1]='2',(array)[2]='b',(array)[4]='\0'
|
|
|
|
#define BCRYPT_SALT_SIZE 22
|
|
|
|
/* Default number of rounds if not explicitly specified. */
|
|
|
|
#define B_ROUNDS_DEFAULT 13
|
|
|
|
/* Minimum number of rounds. */
|
|
|
|
#define B_ROUNDS_MIN 4
|
|
|
|
/* Maximum number of rounds. */
|
|
|
|
#define B_ROUNDS_MAX 31
|
|
|
|
#endif /* USE_BCRYPT */
|
|
|
|
|
|
|
|
#ifdef USE_SHA_CRYPT
|
|
|
|
/* Fixed salt len for sha{256,512}crypt. */
|
|
|
|
#define SHA_CRYPT_SALT_SIZE 16
|
|
|
|
/* Default number of rounds if not explicitly specified. */
|
|
|
|
#define SHA_ROUNDS_DEFAULT 5000
|
|
|
|
/* Minimum number of rounds. */
|
|
|
|
#define SHA_ROUNDS_MIN 1000
|
|
|
|
/* Maximum number of rounds. */
|
|
|
|
#define SHA_ROUNDS_MAX 999999999
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_YESCRYPT
|
|
|
|
/*
|
|
|
|
* Default number of base64 characters used for the salt.
|
|
|
|
* 24 characters gives a 144 bits (18 bytes) salt. Unlike the more
|
|
|
|
* traditional 128 bits (16 bytes) salt, this 144 bits salt is always
|
|
|
|
* represented by the same number of base64 characters without padding
|
|
|
|
* issue, even with a non-standard base64 encoding scheme.
|
|
|
|
*/
|
|
|
|
#define YESCRYPT_SALT_SIZE 24
|
|
|
|
/* Default cost if not explicitly specified. */
|
|
|
|
#define Y_COST_DEFAULT 5
|
|
|
|
/* Minimum cost. */
|
|
|
|
#define Y_COST_MIN 1
|
|
|
|
/* Maximum cost. */
|
|
|
|
#define Y_COST_MAX 11
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Fixed salt len for md5crypt. */
|
|
|
|
#define MD5_CRYPT_SALT_SIZE 8
|
|
|
|
|
|
|
|
/* Generate salt of size salt_size. */
|
|
|
|
#define MAX_SALT_SIZE 44
|
|
|
|
#define MIN_SALT_SIZE 8
|
|
|
|
|
|
|
|
/* Maximum size of the generated salt string. */
|
|
|
|
#define GENSALT_SETTING_SIZE 100
|
|
|
|
|
2008-01-06 22:50:26 +08:00
|
|
|
/* local function prototypes */
|
2021-06-15 20:23:42 +08:00
|
|
|
#if !USE_XCRYPT_GENSALT
|
* libmisc/xgetXXbyYY.c, libmisc/myname.c, libmisc/getgr_nam_gid.c,
libmisc/salt.c, libmisc/list.c, libmisc/cleanup.c, src/login.c,
lib/getdef.h, lib/groupio.c, lib/getlong.c, lib/gshadow_.h,
lib/sgroupio.c, lib/shadowio.c, lib/pwio.c, lib/commonio.h,
lib/fputsx.c, lib/prototypes.h: Added splint annotations.
* lib/groupio.c: Avoid implicit conversion of pointers to
booleans.
* lib/groupio.c: Free allocated buffers in case of failure.
2009-04-23 17:57:03 +08:00
|
|
|
static /*@observer@*/const char *gensalt (size_t salt_size);
|
2021-06-15 20:23:42 +08:00
|
|
|
#endif /* !USE_XCRYPT_GENSALT */
|
2019-09-17 02:54:56 +08:00
|
|
|
#ifdef USE_SHA_CRYPT
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long SHA_get_salt_rounds (/*@null@*/const int *prefered_rounds);
|
2021-06-15 20:23:42 +08:00
|
|
|
static /*@observer@*/void SHA_salt_rounds_to_buf (char *buf, unsigned long rounds);
|
2008-05-20 04:56:48 +08:00
|
|
|
#endif /* USE_SHA_CRYPT */
|
2019-09-17 02:54:56 +08:00
|
|
|
#ifdef USE_BCRYPT
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long BCRYPT_get_salt_rounds (/*@null@*/const int *prefered_rounds);
|
2021-06-15 20:23:42 +08:00
|
|
|
static /*@observer@*/void BCRYPT_salt_rounds_to_buf (char *buf, unsigned long rounds);
|
2019-09-17 02:54:56 +08:00
|
|
|
#endif /* USE_BCRYPT */
|
2020-12-28 04:09:25 +08:00
|
|
|
#ifdef USE_YESCRYPT
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long YESCRYPT_get_salt_cost (/*@null@*/const int *prefered_cost);
|
2021-06-15 20:23:42 +08:00
|
|
|
static /*@observer@*/void YESCRYPT_salt_cost_to_buf (char *buf, unsigned long cost);
|
2020-12-28 04:09:25 +08:00
|
|
|
#endif /* USE_YESCRYPT */
|
2008-01-06 22:50:26 +08:00
|
|
|
|
2008-02-04 01:23:58 +08:00
|
|
|
|
2019-09-17 02:54:56 +08:00
|
|
|
#ifdef USE_SHA_CRYPT
|
2021-06-15 20:23:42 +08:00
|
|
|
/* Return the the rounds number for the SHA crypt methods. */
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long SHA_get_salt_rounds (/*@null@*/const int *prefered_rounds)
|
2007-11-20 08:05:54 +08:00
|
|
|
{
|
2021-06-15 05:28:28 +08:00
|
|
|
unsigned long rounds;
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2007-11-20 17:33:52 +08:00
|
|
|
if (NULL == prefered_rounds) {
|
|
|
|
long min_rounds = getdef_long ("SHA_CRYPT_MIN_ROUNDS", -1);
|
|
|
|
long max_rounds = getdef_long ("SHA_CRYPT_MAX_ROUNDS", -1);
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2008-06-14 03:37:15 +08:00
|
|
|
if ((-1 == min_rounds) && (-1 == max_rounds)) {
|
2021-06-15 05:28:28 +08:00
|
|
|
rounds = SHA_ROUNDS_DEFAULT;
|
2008-06-14 03:37:15 +08:00
|
|
|
}
|
2021-08-15 01:24:34 +08:00
|
|
|
else {
|
|
|
|
if (-1 == min_rounds) {
|
|
|
|
min_rounds = max_rounds;
|
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-08-15 01:24:34 +08:00
|
|
|
if (-1 == max_rounds) {
|
|
|
|
max_rounds = min_rounds;
|
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-08-15 01:24:34 +08:00
|
|
|
if (min_rounds > max_rounds) {
|
|
|
|
max_rounds = min_rounds;
|
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2022-12-31 01:50:21 +08:00
|
|
|
rounds = csrand_interval (min_rounds, max_rounds);
|
2008-06-14 03:37:15 +08:00
|
|
|
}
|
|
|
|
} else if (0 == *prefered_rounds) {
|
2021-06-15 05:28:28 +08:00
|
|
|
rounds = SHA_ROUNDS_DEFAULT;
|
2008-06-14 03:37:15 +08:00
|
|
|
} else {
|
2021-06-15 05:28:28 +08:00
|
|
|
rounds = (unsigned long) *prefered_rounds;
|
2008-06-14 03:37:15 +08:00
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
|
|
|
/* Sanity checks. The libc should also check this, but this
|
|
|
|
* protects against a rounds_prefix overflow. */
|
2021-06-15 05:28:28 +08:00
|
|
|
if (rounds < SHA_ROUNDS_MIN) {
|
|
|
|
rounds = SHA_ROUNDS_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rounds > SHA_ROUNDS_MAX) {
|
|
|
|
rounds = SHA_ROUNDS_MAX;
|
2008-06-14 03:37:15 +08:00
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
return rounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill a salt prefix specifying the rounds number for the SHA crypt methods
|
|
|
|
* to a buffer.
|
|
|
|
*/
|
|
|
|
static /*@observer@*/void SHA_salt_rounds_to_buf (char *buf, unsigned long rounds)
|
|
|
|
{
|
|
|
|
const size_t buf_begin = strlen (buf);
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/* Nothing to do here if SHA_ROUNDS_DEFAULT is used. */
|
|
|
|
if (rounds == SHA_ROUNDS_DEFAULT) {
|
|
|
|
return;
|
2008-06-14 03:37:15 +08:00
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/*
|
|
|
|
* Check if the result buffer is long enough.
|
|
|
|
* We are going to write a maximum of 17 bytes,
|
|
|
|
* plus one byte for the terminator.
|
|
|
|
* rounds=XXXXXXXXX$
|
|
|
|
* 00000000011111111
|
|
|
|
* 12345678901234567
|
|
|
|
*/
|
2021-06-15 05:28:28 +08:00
|
|
|
assert (GENSALT_SETTING_SIZE > buf_begin + 17);
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
(void) snprintf (buf + buf_begin, 18, "rounds=%lu$", rounds);
|
2007-11-20 08:05:54 +08:00
|
|
|
}
|
2008-05-20 04:56:48 +08:00
|
|
|
#endif /* USE_SHA_CRYPT */
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2019-09-17 02:54:56 +08:00
|
|
|
#ifdef USE_BCRYPT
|
2021-06-15 20:23:42 +08:00
|
|
|
/* Return the the rounds number for the BCRYPT method. */
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long BCRYPT_get_salt_rounds (/*@null@*/const int *prefered_rounds)
|
2019-09-17 02:54:56 +08:00
|
|
|
{
|
2021-06-15 05:28:28 +08:00
|
|
|
unsigned long rounds;
|
2019-09-17 02:54:56 +08:00
|
|
|
|
|
|
|
if (NULL == prefered_rounds) {
|
|
|
|
long min_rounds = getdef_long ("BCRYPT_MIN_ROUNDS", -1);
|
|
|
|
long max_rounds = getdef_long ("BCRYPT_MAX_ROUNDS", -1);
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
if ((-1 == min_rounds) && (-1 == max_rounds)) {
|
2019-09-17 02:54:56 +08:00
|
|
|
rounds = B_ROUNDS_DEFAULT;
|
2021-06-15 05:28:28 +08:00
|
|
|
} else {
|
2019-09-17 02:54:56 +08:00
|
|
|
if (-1 == min_rounds) {
|
|
|
|
min_rounds = max_rounds;
|
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
|
2019-09-17 02:54:56 +08:00
|
|
|
if (-1 == max_rounds) {
|
|
|
|
max_rounds = min_rounds;
|
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
|
2019-09-17 02:54:56 +08:00
|
|
|
if (min_rounds > max_rounds) {
|
|
|
|
max_rounds = min_rounds;
|
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
|
2022-12-31 01:50:21 +08:00
|
|
|
rounds = csrand_interval (min_rounds, max_rounds);
|
2019-09-17 02:54:56 +08:00
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
} else if (0 == *prefered_rounds) {
|
|
|
|
rounds = B_ROUNDS_DEFAULT;
|
2019-09-17 02:54:56 +08:00
|
|
|
} else {
|
2021-06-15 05:28:28 +08:00
|
|
|
rounds = (unsigned long) *prefered_rounds;
|
2019-09-17 02:54:56 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/* Sanity checks. */
|
2019-09-17 02:54:56 +08:00
|
|
|
if (rounds < B_ROUNDS_MIN) {
|
|
|
|
rounds = B_ROUNDS_MIN;
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
#if USE_XCRYPT_GENSALT
|
|
|
|
if (rounds > B_ROUNDS_MAX) {
|
|
|
|
rounds = B_ROUNDS_MAX;
|
|
|
|
}
|
|
|
|
#else /* USE_XCRYPT_GENSALT */
|
2021-06-15 05:28:28 +08:00
|
|
|
/*
|
|
|
|
* Use 19 as an upper bound for now,
|
|
|
|
* because musl doesn't allow rounds >= 20.
|
2022-05-10 21:01:21 +08:00
|
|
|
* If musl ever supports > 20 rounds,
|
|
|
|
* rounds should be set to B_ROUNDS_MAX.
|
2021-06-15 05:28:28 +08:00
|
|
|
*/
|
2019-09-17 02:54:56 +08:00
|
|
|
if (rounds > 19) {
|
|
|
|
rounds = 19;
|
|
|
|
}
|
2021-06-15 20:23:42 +08:00
|
|
|
#endif /* USE_XCRYPT_GENSALT */
|
|
|
|
|
|
|
|
return rounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill a salt prefix specifying the rounds number for the BCRYPT method
|
|
|
|
* to a buffer.
|
|
|
|
*/
|
|
|
|
static /*@observer@*/void BCRYPT_salt_rounds_to_buf (char *buf, unsigned long rounds)
|
|
|
|
{
|
|
|
|
const size_t buf_begin = strlen (buf);
|
2019-09-17 02:54:56 +08:00
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/*
|
|
|
|
* Check if the result buffer is long enough.
|
|
|
|
* We are going to write three bytes,
|
|
|
|
* plus one byte for the terminator.
|
|
|
|
* XX$
|
|
|
|
* 000
|
|
|
|
* 123
|
|
|
|
*/
|
2021-06-15 05:28:28 +08:00
|
|
|
assert (GENSALT_SETTING_SIZE > buf_begin + 3);
|
2019-09-17 02:54:56 +08:00
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
(void) snprintf (buf + buf_begin, 4, "%2.2lu$", rounds);
|
2019-09-17 02:54:56 +08:00
|
|
|
}
|
|
|
|
#endif /* USE_BCRYPT */
|
|
|
|
|
2020-12-28 04:09:25 +08:00
|
|
|
#ifdef USE_YESCRYPT
|
2021-06-15 20:23:42 +08:00
|
|
|
/* Return the the cost number for the YESCRYPT method. */
|
2022-08-05 23:40:36 +08:00
|
|
|
static /*@observer@*/unsigned long YESCRYPT_get_salt_cost (/*@null@*/const int *prefered_cost)
|
2020-12-28 04:09:25 +08:00
|
|
|
{
|
2021-06-15 05:28:28 +08:00
|
|
|
unsigned long cost;
|
2020-12-28 04:09:25 +08:00
|
|
|
|
|
|
|
if (NULL == prefered_cost) {
|
|
|
|
cost = getdef_num ("YESCRYPT_COST_FACTOR", Y_COST_DEFAULT);
|
2021-06-15 05:28:28 +08:00
|
|
|
} else if (0 == *prefered_cost) {
|
|
|
|
cost = Y_COST_DEFAULT;
|
2020-12-28 04:09:25 +08:00
|
|
|
} else {
|
2021-06-15 05:28:28 +08:00
|
|
|
cost = (unsigned long) *prefered_cost;
|
2020-12-28 04:09:25 +08:00
|
|
|
}
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/* Sanity checks. */
|
2020-12-28 04:09:25 +08:00
|
|
|
if (cost < Y_COST_MIN) {
|
|
|
|
cost = Y_COST_MIN;
|
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
|
2020-12-28 04:09:25 +08:00
|
|
|
if (cost > Y_COST_MAX) {
|
|
|
|
cost = Y_COST_MAX;
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill a salt prefix specifying the cost for the YESCRYPT method
|
|
|
|
* to a buffer.
|
|
|
|
*/
|
|
|
|
static /*@observer@*/void YESCRYPT_salt_cost_to_buf (char *buf, unsigned long cost)
|
|
|
|
{
|
|
|
|
const size_t buf_begin = strlen (buf);
|
|
|
|
|
2021-06-15 05:28:28 +08:00
|
|
|
/*
|
|
|
|
* Check if the result buffer is long enough.
|
|
|
|
* We are going to write four bytes,
|
|
|
|
* plus one byte for the terminator.
|
|
|
|
* jXX$
|
|
|
|
* 0000
|
|
|
|
* 1234
|
|
|
|
*/
|
|
|
|
assert (GENSALT_SETTING_SIZE > buf_begin + 4);
|
2021-06-15 05:28:28 +08:00
|
|
|
|
|
|
|
buf[buf_begin + 0] = 'j';
|
2020-12-28 04:09:25 +08:00
|
|
|
if (cost < 3) {
|
2021-06-15 05:28:28 +08:00
|
|
|
buf[buf_begin + 1] = 0x36 + cost;
|
2020-12-28 04:09:25 +08:00
|
|
|
} else if (cost < 6) {
|
2021-06-15 05:28:28 +08:00
|
|
|
buf[buf_begin + 1] = 0x34 + cost;
|
2020-12-28 04:09:25 +08:00
|
|
|
} else {
|
2021-06-15 05:28:28 +08:00
|
|
|
buf[buf_begin + 1] = 0x3b + cost;
|
2020-12-28 04:09:25 +08:00
|
|
|
}
|
2021-06-15 05:28:28 +08:00
|
|
|
buf[buf_begin + 2] = cost >= 3 ? 'T' : '5';
|
|
|
|
buf[buf_begin + 3] = '$';
|
|
|
|
buf[buf_begin + 4] = '\0';
|
2020-12-28 04:09:25 +08:00
|
|
|
}
|
|
|
|
#endif /* USE_YESCRYPT */
|
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
#if !USE_XCRYPT_GENSALT
|
* libmisc/xgetXXbyYY.c, libmisc/myname.c, libmisc/getgr_nam_gid.c,
libmisc/salt.c, libmisc/list.c, libmisc/cleanup.c, src/login.c,
lib/getdef.h, lib/groupio.c, lib/getlong.c, lib/gshadow_.h,
lib/sgroupio.c, lib/shadowio.c, lib/pwio.c, lib/commonio.h,
lib/fputsx.c, lib/prototypes.h: Added splint annotations.
* lib/groupio.c: Avoid implicit conversion of pointers to
booleans.
* lib/groupio.c: Free allocated buffers in case of failure.
2009-04-23 17:57:03 +08:00
|
|
|
static /*@observer@*/const char *gensalt (size_t salt_size)
|
2008-01-06 22:50:26 +08:00
|
|
|
{
|
2021-06-15 05:28:28 +08:00
|
|
|
static char salt[MAX_SALT_SIZE + 6];
|
2007-11-24 08:00:12 +08:00
|
|
|
|
Use bzero(3) instead of its pattern
It was blessed by POSIX.1-2001, and GCC says that it won't go away,
possibly ever.
memset(3) is dangerous, as the 2nd and 3rd arguments can be accidentally
swapped --who remembers what's the order of the 2nd and 3rd parameters
to memset(3) without checking the manual page or some code that uses
it?--. Some recent compilers may be able to catch that via some
warnings, but those are not infalible. And even if compiler warnings
could always catch that, the time lost in fixing or checking the docs is
lost for no clear gain. Having a sane API that is unambiguous is the
Right Thing (tm); and that API is bzero(3).
If someone doesn't believe memset(3) is error-prone, please read the
book "Unix Network Programming", Volume 1, 3rd Edition by Stevens, et
al., Section 1.2. See a stackoverflow reference in the link below[1].
bzero(3) had a bad fame in the bad old days, because some ancient
systems (I'm talking of many decades ago) shipped a broken version of
bzero(3). We can assume that all systems in which current shadow utils
can be built, have a working version of bzero(3) --if not, please fix
your broken system; don't blame the programmer--.
One reason that some use today to avoid bzero(3) in favor of memset(3)
is that memset(3) is more often used; but that's a circular reasoning.
Even if bzero(3) wasn't supported by the system, it would need to be
invented. It's the right API.
Another reason that some argue is that POSIX.1-2008 removed the
specification of bzero(3). That's not a problem, because GCC will
probably support it forever, and even if it didn't, we can redefine it
like we do with memzero(). bzero(3) is just a one-liner wrapper around
memset(3).
Link: [1] <https://stackoverflow.com/a/17097978>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-02 00:27:50 +08:00
|
|
|
bzero(salt, MAX_SALT_SIZE + 6);
|
2007-11-24 08:00:12 +08:00
|
|
|
|
2007-11-24 05:04:43 +08:00
|
|
|
assert (salt_size >= MIN_SALT_SIZE &&
|
|
|
|
salt_size <= MAX_SALT_SIZE);
|
2022-12-31 02:42:17 +08:00
|
|
|
strcat (salt, l64a (csrand ()));
|
2007-11-24 08:00:12 +08:00
|
|
|
do {
|
2022-12-31 02:42:17 +08:00
|
|
|
strcat (salt, l64a (csrand ()));
|
2007-11-24 08:00:12 +08:00
|
|
|
} while (strlen (salt) < salt_size);
|
2008-06-14 03:37:15 +08:00
|
|
|
|
2007-11-24 08:00:12 +08:00
|
|
|
salt[salt_size] = '\0';
|
|
|
|
|
|
|
|
return salt;
|
2007-11-24 04:51:43 +08:00
|
|
|
}
|
2021-06-15 20:23:42 +08:00
|
|
|
#endif /* !USE_XCRYPT_GENSALT */
|
2007-11-24 04:51:43 +08:00
|
|
|
|
2007-11-20 08:05:54 +08:00
|
|
|
/*
|
|
|
|
* Generate 8 base64 ASCII characters of random salt. If MD5_CRYPT_ENAB
|
|
|
|
* in /etc/login.defs is "yes", the salt string will be prefixed by "$1$"
|
|
|
|
* (magic) and pw_encrypt() will execute the MD5-based FreeBSD-compatible
|
|
|
|
* version of crypt() instead of the standard one.
|
|
|
|
* Other methods can be set with ENCRYPT_METHOD
|
2007-11-20 17:33:52 +08:00
|
|
|
*
|
|
|
|
* The method can be forced with the meth parameter.
|
2021-06-15 05:28:28 +08:00
|
|
|
* If NULL, the method will be defined according to the ENCRYPT_METHOD
|
|
|
|
* variable, and if not set according to the MD5_CRYPT_ENAB variable,
|
|
|
|
* which can both be set inside the login.defs file.
|
2007-11-20 17:33:52 +08:00
|
|
|
*
|
|
|
|
* If meth is specified, an additional parameter can be provided.
|
|
|
|
* * For the SHA256 and SHA512 method, this specifies the number of rounds
|
|
|
|
* (if not NULL).
|
2020-12-28 04:09:25 +08:00
|
|
|
* * For the YESCRYPT method, this specifies the cost factor (if not NULL).
|
2007-11-20 08:05:54 +08:00
|
|
|
*/
|
2011-08-14 22:37:17 +08:00
|
|
|
/*@observer@*/const char *crypt_make_salt (/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg)
|
2007-10-07 19:44:02 +08:00
|
|
|
{
|
2021-06-15 05:28:28 +08:00
|
|
|
static char result[GENSALT_SETTING_SIZE];
|
|
|
|
size_t salt_len = MAX_SALT_SIZE;
|
2008-01-06 22:50:26 +08:00
|
|
|
const char *method;
|
2021-06-15 20:23:42 +08:00
|
|
|
unsigned long rounds = 0;
|
2007-10-07 19:44:02 +08:00
|
|
|
|
Use bzero(3) instead of its pattern
It was blessed by POSIX.1-2001, and GCC says that it won't go away,
possibly ever.
memset(3) is dangerous, as the 2nd and 3rd arguments can be accidentally
swapped --who remembers what's the order of the 2nd and 3rd parameters
to memset(3) without checking the manual page or some code that uses
it?--. Some recent compilers may be able to catch that via some
warnings, but those are not infalible. And even if compiler warnings
could always catch that, the time lost in fixing or checking the docs is
lost for no clear gain. Having a sane API that is unambiguous is the
Right Thing (tm); and that API is bzero(3).
If someone doesn't believe memset(3) is error-prone, please read the
book "Unix Network Programming", Volume 1, 3rd Edition by Stevens, et
al., Section 1.2. See a stackoverflow reference in the link below[1].
bzero(3) had a bad fame in the bad old days, because some ancient
systems (I'm talking of many decades ago) shipped a broken version of
bzero(3). We can assume that all systems in which current shadow utils
can be built, have a working version of bzero(3) --if not, please fix
your broken system; don't blame the programmer--.
One reason that some use today to avoid bzero(3) in favor of memset(3)
is that memset(3) is more often used; but that's a circular reasoning.
Even if bzero(3) wasn't supported by the system, it would need to be
invented. It's the right API.
Another reason that some argue is that POSIX.1-2008 removed the
specification of bzero(3). That's not a problem, because GCC will
probably support it forever, and even if it didn't, we can redefine it
like we do with memzero(). bzero(3) is just a one-liner wrapper around
memset(3).
Link: [1] <https://stackoverflow.com/a/17097978>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-02 00:27:50 +08:00
|
|
|
bzero(result, GENSALT_SETTING_SIZE);
|
2007-11-20 06:34:48 +08:00
|
|
|
|
2007-11-20 17:33:52 +08:00
|
|
|
if (NULL != meth)
|
|
|
|
method = meth;
|
2007-11-24 07:57:47 +08:00
|
|
|
else {
|
2008-06-14 03:37:15 +08:00
|
|
|
method = getdef_str ("ENCRYPT_METHOD");
|
|
|
|
if (NULL == method) {
|
|
|
|
method = getdef_bool ("MD5_CRYPT_ENAB") ? "MD5" : "DES";
|
|
|
|
}
|
2007-11-24 07:57:47 +08:00
|
|
|
}
|
2007-11-20 17:33:52 +08:00
|
|
|
|
2008-05-24 21:08:58 +08:00
|
|
|
if (0 == strcmp (method, "MD5")) {
|
2007-11-20 17:33:52 +08:00
|
|
|
MAGNUM(result, '1');
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = MD5_CRYPT_SALT_SIZE;
|
2021-06-15 20:23:42 +08:00
|
|
|
rounds = 0;
|
2019-09-17 02:54:56 +08:00
|
|
|
#ifdef USE_BCRYPT
|
|
|
|
} else if (0 == strcmp (method, "BCRYPT")) {
|
|
|
|
BCRYPTMAGNUM(result);
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = BCRYPT_SALT_SIZE;
|
2023-02-01 20:50:48 +08:00
|
|
|
rounds = BCRYPT_get_salt_rounds (arg);
|
2021-06-15 20:23:42 +08:00
|
|
|
BCRYPT_salt_rounds_to_buf (result, rounds);
|
2019-09-17 02:54:56 +08:00
|
|
|
#endif /* USE_BCRYPT */
|
2020-12-28 04:09:25 +08:00
|
|
|
#ifdef USE_YESCRYPT
|
|
|
|
} else if (0 == strcmp (method, "YESCRYPT")) {
|
|
|
|
MAGNUM(result, 'y');
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = YESCRYPT_SALT_SIZE;
|
2023-02-01 20:50:48 +08:00
|
|
|
rounds = YESCRYPT_get_salt_cost (arg);
|
2021-06-15 20:23:42 +08:00
|
|
|
YESCRYPT_salt_cost_to_buf (result, rounds);
|
2020-12-28 04:09:25 +08:00
|
|
|
#endif /* USE_YESCRYPT */
|
* configure.in: New configure option: --with-sha-crypt enabled by
default. Keeping the feature enabled is safe. Disabling it permits
to disable the references to the SHA256 and SHA512 password
encryption algorithms from the usage help and manuals (in addition
to the support for these algorithms in the code).
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: ENCRYPT_METHOD is
always supported in login.defs. Remove the ENCRYPTMETHOD_SELECT
preprocessor condition.
* libmisc/obscure.c, libmisc/salt.c, src/newusers.c,
src/chpasswd.c, src/chgpasswd.c, src/passwd.c: Disable SHA256 and
SHA512 if USE_SHA_CRYPT is not defined (this corresponds to a
subset of the ENCRYPTMETHOD_SELECT sections).
2007-11-24 21:08:08 +08:00
|
|
|
#ifdef USE_SHA_CRYPT
|
2008-05-24 21:08:58 +08:00
|
|
|
} else if (0 == strcmp (method, "SHA256")) {
|
2007-11-20 17:33:52 +08:00
|
|
|
MAGNUM(result, '5');
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = SHA_CRYPT_SALT_SIZE;
|
2023-02-01 20:50:48 +08:00
|
|
|
rounds = SHA_get_salt_rounds (arg);
|
2021-06-15 20:23:42 +08:00
|
|
|
SHA_salt_rounds_to_buf (result, rounds);
|
2008-05-24 21:08:58 +08:00
|
|
|
} else if (0 == strcmp (method, "SHA512")) {
|
2007-11-20 17:33:52 +08:00
|
|
|
MAGNUM(result, '6');
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = SHA_CRYPT_SALT_SIZE;
|
2023-02-01 20:50:48 +08:00
|
|
|
rounds = SHA_get_salt_rounds (arg);
|
2021-06-15 20:23:42 +08:00
|
|
|
SHA_salt_rounds_to_buf (result, rounds);
|
2008-05-20 04:56:48 +08:00
|
|
|
#endif /* USE_SHA_CRYPT */
|
2007-11-21 04:00:16 +08:00
|
|
|
} else if (0 != strcmp (method, "DES")) {
|
2021-11-29 07:37:53 +08:00
|
|
|
fprintf (log_get_logfd(),
|
2007-11-20 17:33:52 +08:00
|
|
|
_("Invalid ENCRYPT_METHOD value: '%s'.\n"
|
|
|
|
"Defaulting to DES.\n"),
|
|
|
|
method);
|
2021-06-15 05:28:28 +08:00
|
|
|
salt_len = MAX_SALT_SIZE;
|
2021-06-15 20:23:42 +08:00
|
|
|
rounds = 0;
|
Use bzero(3) instead of its pattern
It was blessed by POSIX.1-2001, and GCC says that it won't go away,
possibly ever.
memset(3) is dangerous, as the 2nd and 3rd arguments can be accidentally
swapped --who remembers what's the order of the 2nd and 3rd parameters
to memset(3) without checking the manual page or some code that uses
it?--. Some recent compilers may be able to catch that via some
warnings, but those are not infalible. And even if compiler warnings
could always catch that, the time lost in fixing or checking the docs is
lost for no clear gain. Having a sane API that is unambiguous is the
Right Thing (tm); and that API is bzero(3).
If someone doesn't believe memset(3) is error-prone, please read the
book "Unix Network Programming", Volume 1, 3rd Edition by Stevens, et
al., Section 1.2. See a stackoverflow reference in the link below[1].
bzero(3) had a bad fame in the bad old days, because some ancient
systems (I'm talking of many decades ago) shipped a broken version of
bzero(3). We can assume that all systems in which current shadow utils
can be built, have a working version of bzero(3) --if not, please fix
your broken system; don't blame the programmer--.
One reason that some use today to avoid bzero(3) in favor of memset(3)
is that memset(3) is more often used; but that's a circular reasoning.
Even if bzero(3) wasn't supported by the system, it would need to be
invented. It's the right API.
Another reason that some argue is that POSIX.1-2008 removed the
specification of bzero(3). That's not a problem, because GCC will
probably support it forever, and even if it didn't, we can redefine it
like we do with memzero(). bzero(3) is just a one-liner wrapper around
memset(3).
Link: [1] <https://stackoverflow.com/a/17097978>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-02 00:27:50 +08:00
|
|
|
bzero(result, GENSALT_SETTING_SIZE);
|
2007-11-20 06:14:19 +08:00
|
|
|
}
|
2007-11-20 08:05:54 +08:00
|
|
|
|
2021-06-15 20:23:42 +08:00
|
|
|
#if USE_XCRYPT_GENSALT
|
|
|
|
/*
|
|
|
|
* Prepare DES setting for crypt_gensalt(), if result
|
|
|
|
* has not been filled with anything previously.
|
|
|
|
*/
|
|
|
|
if ('\0' == result[0]) {
|
|
|
|
/* Avoid -Wunused-but-set-variable. */
|
|
|
|
salt_len = GENSALT_SETTING_SIZE - 1;
|
|
|
|
rounds = 0;
|
Use bzero(3) instead of its pattern
It was blessed by POSIX.1-2001, and GCC says that it won't go away,
possibly ever.
memset(3) is dangerous, as the 2nd and 3rd arguments can be accidentally
swapped --who remembers what's the order of the 2nd and 3rd parameters
to memset(3) without checking the manual page or some code that uses
it?--. Some recent compilers may be able to catch that via some
warnings, but those are not infalible. And even if compiler warnings
could always catch that, the time lost in fixing or checking the docs is
lost for no clear gain. Having a sane API that is unambiguous is the
Right Thing (tm); and that API is bzero(3).
If someone doesn't believe memset(3) is error-prone, please read the
book "Unix Network Programming", Volume 1, 3rd Edition by Stevens, et
al., Section 1.2. See a stackoverflow reference in the link below[1].
bzero(3) had a bad fame in the bad old days, because some ancient
systems (I'm talking of many decades ago) shipped a broken version of
bzero(3). We can assume that all systems in which current shadow utils
can be built, have a working version of bzero(3) --if not, please fix
your broken system; don't blame the programmer--.
One reason that some use today to avoid bzero(3) in favor of memset(3)
is that memset(3) is more often used; but that's a circular reasoning.
Even if bzero(3) wasn't supported by the system, it would need to be
invented. It's the right API.
Another reason that some argue is that POSIX.1-2008 removed the
specification of bzero(3). That's not a problem, because GCC will
probably support it forever, and even if it didn't, we can redefine it
like we do with memzero(). bzero(3) is just a one-liner wrapper around
memset(3).
Link: [1] <https://stackoverflow.com/a/17097978>
Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-02 00:27:50 +08:00
|
|
|
memset(result, '.', salt_len);
|
2021-06-15 20:23:42 +08:00
|
|
|
result[salt_len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
char *retval = crypt_gensalt (result, rounds, NULL, 0);
|
|
|
|
|
|
|
|
/* Should not happen, but... */
|
|
|
|
if (NULL == retval) {
|
2021-11-29 07:37:53 +08:00
|
|
|
fprintf (log_get_logfd(),
|
2021-06-15 20:23:42 +08:00
|
|
|
_("Unable to generate a salt from setting "
|
|
|
|
"\"%s\", check your settings in "
|
|
|
|
"ENCRYPT_METHOD and the corresponding "
|
|
|
|
"configuration for your selected hash "
|
|
|
|
"method.\n"), result);
|
|
|
|
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
#else /* USE_XCRYPT_GENSALT */
|
2021-06-15 05:28:28 +08:00
|
|
|
/* Check if the result buffer is long enough. */
|
|
|
|
assert (GENSALT_SETTING_SIZE > strlen (result) + salt_len);
|
|
|
|
|
|
|
|
/* Concatenate a pseudo random salt. */
|
|
|
|
strncat (result, gensalt (salt_len),
|
|
|
|
GENSALT_SETTING_SIZE - strlen (result) - 1);
|
2007-10-07 19:44:02 +08:00
|
|
|
|
|
|
|
return result;
|
2021-06-15 20:23:42 +08:00
|
|
|
#endif /* USE_XCRYPT_GENSALT */
|
2007-10-07 19:44:02 +08:00
|
|
|
}
|