75 lines
3.1 KiB
Bash
75 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
# ------------------------------------------------------------------------------
|
|
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# Protected by the BSD3 license. Please read bellow for details.
|
|
#
|
|
# * Redistribution and use in source and binary forms,
|
|
# * with or without modification, are permitted provided
|
|
# * that the following conditions are met:
|
|
# *
|
|
# * Redistributions of source code must retain the above
|
|
# * copyright notice, this list of conditions and the
|
|
# * following disclaimer.
|
|
# *
|
|
# * Redistributions in binary form must reproduce the above
|
|
# * copyright notice, this list of conditions and the following
|
|
# * disclaimer in the documentation and/or other materials
|
|
# * provided with the distribution.
|
|
# *
|
|
# * Neither the name of the copyright holder nor the names
|
|
# * of any other contributors may be used to endorse or
|
|
# * promote products derived from this software without
|
|
# * specific prior written permission.
|
|
# *
|
|
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
# * OF SUCH DAMAGE.
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers of profile.d/pwd.sh, loaded by the module itself.
|
|
|
|
# Pool of random words read from /dev/urandom, refilled by _pwd_rand.
|
|
_PWD_POOL=()
|
|
|
|
# Internal helper: put in _PWD_RAND an integer uniformly distributed in
|
|
# [0, $1), for 1 <= $1 <= 2^32. $RANDOM is NOT usable for passwords: it only
|
|
# has 15 bits, a state seeded from the time and the PID, and is predictable.
|
|
# The source is $SRANDOM (bash >= 5.1, from the kernel's random generator), or
|
|
# else /dev/urandom read through od, 256 words at a time to limit the number
|
|
# of processes. Values above the largest multiple of $1 are rejected, so that
|
|
# "% $1" introduces no bias.
|
|
# Usage: _pwd_rand <max>
|
|
_pwd_rand()
|
|
{
|
|
local max="$1" limit r
|
|
limit=$(( 4294967296 - 4294967296 % max ))
|
|
while :; do
|
|
if [[ -n "${SRANDOM+x}" ]]; then
|
|
r="$SRANDOM"
|
|
else
|
|
if (( ${#_PWD_POOL[@]} == 0 )); then
|
|
# shellcheck disable=SC2207 # whitespace-separated numbers
|
|
_PWD_POOL=( $(od -An -v -N1024 -tu4 /dev/urandom 2>/dev/null) )
|
|
if (( ${#_PWD_POOL[@]} == 0 )); then
|
|
disp E "No source of random numbers available (/dev/urandom)."
|
|
return 1
|
|
fi
|
|
fi
|
|
r="${_PWD_POOL[-1]}"
|
|
unset '_PWD_POOL[-1]'
|
|
fi
|
|
if (( r < limit )); then
|
|
_PWD_RAND=$(( r % max ))
|
|
return 0
|
|
fi
|
|
done
|
|
}
|