From f822bfede803e7ed457fe14962e952ee23c7a3ae Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 8 Dec 2017 05:24:32 +0000 Subject: [PATCH] etc/profile.d/ldap.sh --- etc/profile.d/functions | 444 ++++++++++++++++++++-------------------- etc/profile.d/ldap.sh | 181 ++++++++-------- 2 files changed, 315 insertions(+), 310 deletions(-) diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 2085b4b2..c18b0d45 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -6,26 +6,26 @@ nb_repos() { [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox ( - # Always ! - echo "$NB_ROOT" + # Always ! + echo "$NB_ROOT" - # Conf file - local conf="$NB_ROOT/repos.conf" - [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" + # Conf file + local conf="$NB_ROOT/repos.conf" + [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf" - # Realpath - #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null - local IFS=" + # Realpath + #ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null + local IFS=" " - for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do + for dir in $(eval ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do - # Accept directories or files (ex: use to tag a directory: .nb-install) - [ -f "$dir" ] && dir=$(dirname "$dir") + # Accept directories or files (ex: use to tag a directory: .nb-install) + [ -f "$dir" ] && dir=$(dirname "$dir") - dir=$(realpath "$dir") - echo "$dir" + dir=$(realpath "$dir") + echo "$dir" - done + done # no doubles # strong if awk failed @@ -41,14 +41,14 @@ nb_repos_ls() { " for d in `nb_repos`; do - for f in "$@"; do + for f in "$@"; do - #nb_debug "====$d/$f" - for repo in "$d"/$f; do - [ -e "$repo" ] && echo "$repo" - done + #nb_debug "====$d/$f" + for repo in "$d"/$f; do + [ -e "$repo" ] && echo "$repo" + done - done + done done } @@ -115,9 +115,9 @@ nb_repos_grep() { timeout() { if which timeout 1>/dev/null; then - timeout -- $@ + timeout -- $@ else - perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@ + perl -e 'BEGIN{$|=1}; $SIG{ALRM}=sub{exit 1}; alarm(shift @ARGV); system join(" ",@ARGV)' -- $@ fi } @@ -127,24 +127,24 @@ shell_help() { # local msg="" if [ "$#" -gt 0 ]; then - msg="$1" - shift + msg="$1" + shift fi local i #while [ $# -gt 0 ]; do for i in $@; do - case "$i" in - -h|-help|--help) - printf "$msg\n" + case "$i" in + -h|-help|--help) + printf "$msg\n" # NB 25.02.17 case $OSTYPE in # NB 25.02.17 darwin*) printf "$msg\n" ;; # NB 25.02.17 *) echo -e "$msg" ;; # NB 25.02.17 esac - return 0 - ;; - esac - shift 2>/dev/null || break + return 0 + ;; + esac + shift 2>/dev/null || break done return 1 @@ -156,8 +156,8 @@ shell_help_noarg() { # local msg="" if [ "$#" -gt 0 ]; then - msg="$1" - shift + msg="$1" + shift fi local args="$*" @@ -178,23 +178,23 @@ shell_ssh() { local server; server="$1"; shift local tmp; tmp="/tmp/$FUNCNAME.$USER" ( - alias | sed -e 's/^alias //' -e 's/^/alias /' - eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v ' function$' - echo "[ -r /etc/profile ] && source /etc/profile" + alias | sed -e 's/^alias //' -e 's/^/alias /' + eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v ' function$' + echo "[ -r /etc/profile ] && source /etc/profile" ) | ssh "$server" "cat > $tmp || rm -vf $tmp" || return local args if [ -z "$@" ]; then - [ -z "$ssh_opt" ] && ssh_opt='-t' + [ -z "$ssh_opt" ] && ssh_opt='-t' - case "$ZSH_NAME" in - *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;; - *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;; - esac + case "$ZSH_NAME" in + *zsh) ssh $ssh_opt "$server" "source $tmp && zsh -f -d; rm -f $tmp" ;; + *) ssh $ssh_opt "$server" "bash --rcfile $tmp; rm -f $tmp" ;; + esac else # NB 31.08.16 ssh $ssh_opt "$server" "bash && rm -f $tmp" <<< ". $tmp; $@" - echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp" + echo ". $tmp; $@" | ssh $ssh_opt "$server" "bash && rm -f $tmp" fi } @@ -211,12 +211,12 @@ env_add_path() { [ -z "$env_value" ] || env_value=$(printf %s "$env_value" | awk -v RS=: '{ if (!arr[$0]++) {printf("%s%s",!ln++?"":":",$0)}}') for p in $@; do - [ -e "$p" ] || continue - case "${env_value}" in - *:$p|*:$p:*|$p:*|$p) continue;; - esac - [ -z "$env_value" ] || env_value=":${env_value}" - env_value="${p}${env_value}" + [ -e "$p" ] || continue + case "${env_value}" in + *:$p|*:$p:*|$p:*|$p) continue;; + esac + [ -z "$env_value" ] || env_value=":${env_value}" + env_value="${p}${env_value}" done # Clean up doubles @@ -226,12 +226,12 @@ env_add_path() { if ! which realpath >/dev/null; then realpath() { if which perl 1>/dev/null; then - perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" + perl -MFile::Spec -MCwd -e 'print File::Spec->rel2abs( Cwd::abs_path($ARGV[0]) )."\n" if -e $ARGV[0]' "$1" else - case "$OSTYPE" in - darwin*) readlink "$1" ;; - *) readlink -f "$1" ;; - esac + case "$OSTYPE" in + darwin*) readlink "$1" ;; + *) readlink -f "$1" ;; + esac fi } fi @@ -239,11 +239,11 @@ fi ascii() { if [ ! -t 0 ]; then - cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' + cat | perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' elif [ -n "$*" ]; then - perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@ + perl -ne 'for (split("",$_)) { print sprintf("%3d",ord($_)).":$_\n"; }' $@ else - perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }' + perl -e 'binmode(STDOUT, ":utf8"); for(32..255){ print "$_:".chr($_)."\n"; }' fi } @@ -278,7 +278,7 @@ txt2ascii() { $in | perl -e ' while (<>) { for (split("",$_)) { - print sprintf("\%3d",ord($_)).":$_\n"; + print sprintf("\%3d",ord($_)).":$_\n"; } } ' @@ -293,9 +293,9 @@ secs2h () { if [[ ${D} != 0 ]] then - printf '%d days %02d:%02d:%02d\n' $D $H $M $S + printf '%d days %02d:%02d:%02d\n' $D $H $M $S else - printf '%02d:%02d:%02d\n' $H $M $S + printf '%02d:%02d:%02d\n' $H $M $S fi } @@ -303,9 +303,9 @@ bytes2h() { local in args if [ "$1" = "-regex" ]; then - args="regex=$2" - shift - shift + args="regex=$2" + shift + shift fi in="cat" [ -z "$@" ] || in="echo $@" @@ -317,8 +317,8 @@ BEGIN{ use NB::Functions qw/bytes2h/; $exp = "([\\d\\.]+)"; if (@ARGV and $ARGV[0] =~ /regex=(\S+)/) { - shift @ARGV; - $exp = $1; + shift @ARGV; + $exp = $1; } }; s/$exp/bytes2h($1)/ge;' $args } @@ -369,14 +369,14 @@ getexp() { exp=$1; shift if [ -z "$_getexp_pcregrep" ]; then - _getexp_pcregrep=0 - which pcregrep > /dev/null && _getexp_pcregrep=1 + _getexp_pcregrep=0 + which pcregrep > /dev/null && _getexp_pcregrep=1 fi if [ $_getexp_pcregrep = 1 ]; then - pcregrep -o${num} "$exp" $@ + pcregrep -o${num} "$exp" $@ else - perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@ + perl -ne 'BEGIN{$e=shift @ARGV; $n=shift(@ARGV) or 0; $e="($e)" and $n=1 if $n==0} (@_=m{$e}) and ($n<=scalar(@_)) and print ($n==0 ? $_[0] : $_[$n-1].chr(10))' "$exp" "$num" $@ fi return } @@ -392,17 +392,17 @@ ls_img_black() { while [ $# -gt 0 ]; do - if [ "$1" = "-threshold" ]; then - threshold=$2; shift 2; continue - fi + if [ "$1" = "-threshold" ]; then + threshold=$2; shift 2; continue + fi - #colors=$(convert $1 -format "%[mean]" info:-) + #colors=$(convert $1 -format "%[mean]" info:-) - if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then - echo "$1" - ret=0 - fi - shift + if ! convert "$1" -threshold ${threshold}% -format %c histogram:info:- | grep -qm1 white; then + echo "$1" + ret=0 + fi + shift done @@ -424,14 +424,14 @@ jpg2avi() { local count; count=0 while [ $# -gt 0 ]; do - count=$(($count+1)) + count=$(($count+1)) - if [ "$count" = "$tot" ]; then - avi="$1"; shift; break - fi - echo "$1" >> "$still" + if [ "$count" = "$tot" ]; then + avi="$1"; shift; break + fi + echo "$1" >> "$still" - shift + shift done #echo $(cat $still)" -> $avi" @@ -448,32 +448,32 @@ cron_d_install() { #shell_help "$usage" "$@" && return 1 [ -z "$*" ] && echo "$usage" && return 1 if ! perl -e '' 2>/dev/null; then - echo "cron_d_install: please install perl" 1>&2 - return 1 + echo "cron_d_install: please install perl" 1>&2 + return 1 fi if [ "$#" -gt 0 ]; then - case "$1" in -t|--test) test=1; shift ;; esac + case "$1" in -t|--test) test=1; shift ;; esac fi #echo "$#"; return if [ $# -gt 3 ]; then - shell_help_noarg "$usage" "--help" && return 1 + shell_help_noarg "$usage" "--help" && return 1 #elif [ $# -eq 3 ]; then elif [ ! -e "$1" ]; then - fname=$1; shift - tmp="/tmp/$FUNCNAME.$$" - trap "rm -f $tmp*" EXIT - file="$tmp.cron" + fname=$1; shift + tmp="/tmp/$FUNCNAME.$$" + trap "rm -f $tmp*" EXIT + file="$tmp.cron" # NB 31.08.16 cat <<< "$1" > "$file"; shift - echo "$1" > "$file"; shift - exp=$1; shift + echo "$1" > "$file"; shift + exp=$1; shift else - file=$1; shift - exp=$1; shift - fname=$(basename "$file") + file=$1; shift + exp=$1; shift + fname=$(basename "$file") fi @@ -482,15 +482,15 @@ cron_d_install() { # if [ -d /etc/cron.d ]; then - if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: /etc/cron.d: $fname" - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return - echo ': Not changed' - return 1 - fi + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: /etc/cron.d: $fname" + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return + echo ': Not changed' + return 1 + fi - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" - return + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && cp "$file" "/etc/cron.d/$fname" + return fi # @@ -535,10 +535,10 @@ exit $diff ? 0 : 1; ) if [ "$test" = "1" ]; then - printf '%s' "$FUNCNAME: crontab: $fname" - #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" - [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return - echo ": Not changed; return 1" + printf '%s' "$FUNCNAME: crontab: $fname" + #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" + [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return + echo ": Not changed; return 1" fi # NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" @@ -547,42 +547,42 @@ exit $diff ? 0 : 1; } csv2human() { - shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; - local sep noheader; - sep='\t'; - noheader=0; - local files='' - - while [ $# -gt 0 ]; do - - if [ -e "$1" ]; then - files="$files $1" - shift - continue - fi - - case "$1" in - --sep|-s) - sep=$2; - shift - ;; - --noheader|-nh) - noheader=1 - ;; - *) - echo "Unknow option: $1 at $0!"; - return 1 - ;; - - esac - - shift - - done - - [ -z "$@" ] || sep="$@"; - - cat $files | perl -MEncode -F"$sep" -ane 'BEGIN { + shell_help "Usage: $FUNCNAME [PERL_REGEXP --sep|-s (default: tab)] [--noheader|-nh]" "$@" && return; + local sep noheader; + sep='\t'; + noheader=0; + local files='' + + while [ $# -gt 0 ]; do + + if [ -e "$1" ]; then + files="$files $1" + shift + continue + fi + + case "$1" in + --sep|-s) + sep=$2; + shift + ;; + --noheader|-nh) + noheader=1 + ;; + *) + echo "Unknow option: $1 at $0!"; + return 1 + ;; + + esac + + shift + + done + + [ -z "$@" ] || sep="$@"; + + cat $files | perl -MEncode -F"$sep" -ane 'BEGIN { $noheader = shift @ARGV; @len = (); @lines = (); binmode( STDOUT, "utf8:" ); @@ -603,8 +603,8 @@ END { $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10); print $sep_line; while ($_ = shift @lines) { - printf $format,@$_; - print $sep_line if !$noheader and !$i++; + printf $format,@$_; + print $sep_line if !$noheader and !$i++; } print $sep_line; print "$tot Records\n"; @@ -624,8 +624,8 @@ EOF ) echo "$out" case "$out" in - *"Relay access denied"*) return 0 ;; - *) return 1 ;; + *"Relay access denied"*) return 0 ;; + *) return 1 ;; esac } @@ -633,8 +633,8 @@ shell_push_arg() { local ret="$1" local to_var="$2" case "$1" in - #) echo "Escape: $i" ;; - *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;; + #) echo "Escape: $i" ;; + *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;; esac [ -z "$to_var" ] && echo "$ret" && return eval "$to_var=\"\$$to_var \$ret\"; $to_var=\${$to_var## };" @@ -644,11 +644,11 @@ urlencode() { local length="${#1}" local i for i in `seq 0 $(($length-1))`; do - eval 'local c="${1:i:1}"' # TODO compatible with busybox - case $c in - [a-zA-Z0-9.~_-]) printf "$c" ;; - *) printf '%%%02X' "'$c" - esac + eval 'local c="${1:i:1}"' # TODO compatible with busybox + case $c in + [a-zA-Z0-9.~_-]) printf "$c" ;; + *) printf '%%%02X' "'$c" + esac done } @@ -661,9 +661,9 @@ boot_loader() { local mbr=`dd if=/dev/sda bs=446 count=1 2> /dev/null| strings -a` case "$mbr" in - *GRUB*) echo "GRUB" ;; - *LILO*) echo "LILO" ;; - *) return 1 ;; + *GRUB*) echo "GRUB" ;; + *LILO*) echo "LILO" ;; + *) return 1 ;; esac return 0 @@ -674,8 +674,8 @@ is_empty() { [[ -z $(ls -A "/$1" | head -1) ]]; } is_prompt() { #[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive" case "$-" in - *i*) return 0 ;; - *) return 1 ;; + *i*) return 0 ;; + *) return 1 ;; esac } @@ -687,23 +687,23 @@ url2nc() { ls_users() { case "$OSTYPE" in - darwin*) - dscacheutil -q user \ - | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \ - | sort -u \ - | sort -t : -k3,4 -n \ - ; - ;; - *) - if [ -n "$(which getent 2>/dev/null)" ]; then - getent passwd - elif [ -e /etc/passwd ]; then - grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n - else - echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2 - return 1 - fi - ;; + darwin*) + dscacheutil -q user \ + | perl -ne '/^\w+:\s+(.*)?$/; push @_,$1; print join(":",$_[0],"*",@_[2,3,6,4,5])."\n" and @_=() if /^\s*$/' \ + | sort -u \ + | sort -t : -k3,4 -n \ + ; + ;; + *) + if [ -n "$(which getent 2>/dev/null)" ]; then + getent passwd + elif [ -e /etc/passwd ]; then + grep '^[^#:]\+:' /etc/passwd | sort -u | sort -t : -k3,4 -n + else + echo "$FUNCNAME: can not find any users on this system: #OSTYPE" 1>&2 + return 1 + fi + ;; esac } @@ -713,17 +713,17 @@ epoch2date() { perl -M'POSIX strftime' -e 'e2d($ARGV[0]) and print "\n" and exit find_sort_mtime() { ( - local find_args="$@" - [ -z "$find_args" ] && args="." - - case "$OSTYPE" in - darwin*) - find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \; - ;; - *) - find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/' - ;; - esac + local find_args="$@" + [ -z "$find_args" ] && args="." + + case "$OSTYPE" in + darwin*) + find $find_args -type f -exec stat -f '%m'$'\t''%N' {} \; + ;; + *) + find $@ -type f -printf "%T@\t%p\n" | sed 's/^\([0-9]\+\)\.[0-9]\+/\1/' + ;; + esac ) | sort -k1 -n $sort_args | epoch2date # strftime "%a %b %e %H:%M:%S %Y" @@ -733,14 +733,14 @@ shell_replace() { local usage="Usage: shell_replace [-i[.BACKUP_EXTENSION]] [-find PAHT]" if [ -z "$*" -o "$1" = "--help" -o "$1" = "-h" ]; then - echo "$usage" - return + echo "$usage" + return fi if [ "$1" = "-find" ]; then - shift - grep -ErlI '^.{1,3}/dev/null; then - curl -sk $* + curl -sk $* elif which wget 1>/dev/null; then - wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* + wget -O - --quiet --no-verbose --no-check-certificate --timeout 5 --tries 1 $* elif which lynx 1>/dev/null; then - lynx --source -dump $* + lynx --source -dump $* else - echo "Can't find web client" 1>&2 + echo "Can't find web client" 1>&2 fi } # NB 07.01.17 alias Get=http_get http_head() { if which curl 1>/dev/null; then - curl -sk --head $* + curl -sk --head $* elif which wget 1>/dev/null; then - http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print' + http_get --save-headers - $* | perl -ne '/^\s*$/ and last;print' elif which lynx 1>/dev/null; then - lynx --head -dump $* + lynx --head -dump $* else - echo "Can't find web client" 1>&2 + echo "Can't find web client" 1>&2 fi } @@ -818,23 +818,23 @@ which_cache() { # Cache case "$_which_cache|" in - *"|$1=1|"*) - nb_debug "which_cache() $1=1" - return 0 - ;; - *"|$1=0|"*) - nb_debug "which_cache() $1=0" - return 1 - ;; + *"|$1=1|"*) + nb_debug "which_cache() $1=1" + return 0 + ;; + *"|$1=0|"*) + nb_debug "which_cache() $1=0" + return 1 + ;; esac #nb_debug "which_cache() $1=?" for p in $PATH; do - test -x "$p/$1" || continue + test -x "$p/$1" || continue - _which_cache="$_which_cache|$1=1" - echo "$1" - return 0 + _which_cache="$_which_cache|$1=1" + echo "$1" + return 0 done _which_cache="$_which_cache|$1=0" @@ -865,9 +865,9 @@ lscolors() { local color=16 while [ $color -lt 245 ]; do - echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" - printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" - color=$(($color+1)) + echo -ne "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" + printf '\t%s\n' "\\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m" + color=$(($color+1)) done } @@ -875,14 +875,14 @@ tab2space() { local size=2 [ "$1" = "-size" ] && shift && size=$1 && shift local space=$(eval "printf ' %.0s' {1..$size}") - perl -pe "1 while s/^\t/$space/" $@ +# NB 08.12.17 perl -pe "1 while s/^\t/$space/" $@ + perl -pe "s/\G[\t]/$space/g" $@ } space2tab() { local size=2 [ "$1" = "-size" ] && shift && size=$1 && shift local space=$(eval "printf ' %.0s' {1..$size}") - #perl -pe "1 while s/^(\t*)?$space/$1\t/" $@ - #perl -pe "1 while s/^$space/\t/; 1 while s/(\t)+$space/$1\t/" $@ - perl -pe "1 while s/^$space/\t/" $@ +# NB 08.12.17 perl -pe "1 while s/^$space/\t/" $@ + perl -pe "s/^\t/$space/; s/\G[ ]{$size}/\t/g" $@ } diff --git a/etc/profile.d/ldap.sh b/etc/profile.d/ldap.sh index b5f957d6..661a9a30 100644 --- a/etc/profile.d/ldap.sh +++ b/etc/profile.d/ldap.sh @@ -1,18 +1,18 @@ which slapcat > /dev/null || return ldap_new_id() { - local attr=${1:-uidNumber} - local max=${2:-65000} - max=$( - slapcat -o ldif-wrap=999 -a "(&($attr=*))" | grep "^$attr:" | sed 's/^[^:]\+: //' | awk '$1 < '$max' {print $0}' | sort -n | tail -1 - ) - [ -z "$max" ] && max=0 - echo $(($max+1)) + local attr=${1:-uidNumber} + local max=${2:-65000} + max=$( + slapcat -o ldif-wrap=999 -a "(&($attr=*))" | grep "^$attr:" | sed 's/^[^:]\+: //' | awk '$1 < '$max' {print $0}' | sort -n | tail -1 + ) + [ -z "$max" ] && max=0 + echo $(($max+1)) } ldap_user_password() { - local pass=$(for i in 0 1 3 4;do slappasswd -gn; done; echo) - cat <&1 | true ldap_config2file() { - [ ! -r slapd.d ] && echo slapd.d si missing 1>&2 && return 1 - slapcat -n 0 -F slapd.d -l config.ldif - echo config.ldif created + [ ! -r slapd.d ] && echo slapd.d si missing 1>&2 && return 1 + slapcat -n 0 -F slapd.d -l config.ldif + echo config.ldif created } ldap_config2dir() { - [ ! -r config.ldif ] && echo config.ldif si missing 1>&2 && return 1 - slapadd -n 0 -F slapd.d -l config.ldif - echo slapd.d created + [ ! -r config.ldif ] && echo config.ldif si missing 1>&2 && return 1 + slapadd -n 0 -F slapd.d -l config.ldif + echo slapd.d created } ldap_config() { # NB 03.11.17 ldapsearch -Q -Y EXTERNAL -H ldapi:/// -o ldif-wrap=999 -b cn=config '(!(objectClass=olcSchemaConfig))' - slapcat -o ldif-wrap=no -b cn=config -a '(!(objectClass=olcSchemaConfig))' + slapcat -o ldif-wrap=no -b cn=config -a '(!(objectClass=olcSchemaConfig))' } ldap_databases() { - slapcat -o ldif-wrap=no -b cn=config | grep "^dn: olcDatabase=" + slapcat -o ldif-wrap=no -b cn=config | grep "^dn: olcDatabase=" } ldap_schema_config() { # NB 03.11.17 ldapsearch -x -s base -b "cn=subschema" objectclasses - slapcat -o ldif-wrap=no -b cn=config -a '(&(objectClass=olcSchemaConfig))' + slapcat -o ldif-wrap=no -b cn=config -a '(&(objectClass=olcSchemaConfig))' } ldap_cn() { - #ldapsearch -x '(cn=*)' dn | grep ^dn | sed 's/dn: //' - slapcat -a 'cn=*' $@ | grep ^dn | sed 's/dn: //' + #ldapsearch -x '(cn=*)' dn | grep ^dn | sed 's/dn: //' + slapcat -a 'cn=*' $@ | grep ^dn | sed 's/dn: //' } slapcat_csv() { - local aawk human=no human_opt='--noheader' - local aldap="$1"; shift - while [ $# -gt 0 ]; do - case "$1" in - -head) aawk+=" $1"; human_opt="" ;; - -human) human=yes; human_opt=""; aawk+=" -head" ;; - -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;; - *) aawk+=" $1" ;; - esac - shift - done - [ $human = yes ] && slapcat_csv "$aldap" $aawk | csv2human $human_opt && return - slapcat -a "$aldap" -o ldif-wrap=no | grep -v '^entryCSN' | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk + local aawk human=no human_opt='--noheader' + local aldap="$1"; shift + while [ $# -gt 0 ]; do + case "$1" in + -head) aawk+=" $1"; human_opt="" ;; + -human) human=yes; human_opt=""; aawk+=" -head" ;; + -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;; + *) aawk+=" $1" ;; + esac + shift + done + [ $human = yes ] && slapcat_csv "$aldap" $aawk | csv2human $human_opt && return + slapcat -a "$aldap" -o ldif-wrap=no | grep -v '^entryCSN' | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk } ldapsearch_csv() { - local aldap aawk human=no human_opt='--noheader' - - while [ $# -gt 0 ]; do - case "$1" in - -head) aawk+=" $1"; human_opt="" ;; - -human) human=yes; human_opt=""; aawk+=" -head" ;; - -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;; - *) aldap+=" $1" ;; - esac - shift - done - - aldap=${aldap/ /}; aawk=${aawk/ /} - [ $human = yes ] && ldapsearch_csv $aldap $aawk | csv2human $human_opt && return - ldapsearch -LLLx -o ldif-wrap=no $aldap | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk $aldap + local aldap aawk human=no human_opt='--noheader' + + while [ $# -gt 0 ]; do + case "$1" in + -head) aawk+=" $1"; human_opt="" ;; + -human) human=yes; human_opt=""; aawk+=" -head" ;; + -nohead) human_opt="--noheader"; aawk=${aawk// -head/} ;; + *) aldap+=" $1" ;; + esac + shift + done + + aldap=${aldap/ /}; aawk=${aawk/ /} + [ $human = yes ] && ldapsearch_csv $aldap $aawk | csv2human $human_opt && return + ldapsearch -LLLx -o ldif-wrap=no $aldap | awk -f $NB_ROOT/lib/awk/ldap2csv.awk -- $aawk $aldap } ldap_gup_dir() { - local dir="$1" && shift - cd "$dir" || return - local perm=$(ls -dlaF --color=auto . 2>/dev/null | awk '{print $3"."$4}') - local tmp=`mktemp -d` - eval ' - ldap_gup $@ | while read -r -a rec; do - file=${rec[0]} - rec=("${rec[@]:1}") # shift - echo "${rec[@]}" >> "$tmp/$file" - done - ' - [ -n "$perm" ] && chown "$perm" "$tmp"/* - chmod 640 "$tmp"/* - mv "$tmp"/* ./ - rm -rf "$tmp" + local dir="$1" && shift + cd "$dir" || return + local perm=$(ls -dlaF --color=auto . 2>/dev/null | awk '{print $3"."$4}') + local tmp=`mktemp -d` + eval ' + ldap_gup $@ | while IFS=: read -r -a rec; do + IFS=: + file=${rec[0]} + rec=("${rec[@]:1}") # shift + #echo "$file ${rec[*]}" && continue + echo "${rec[*]}" >> "$tmp/$file" + done + ' + ( + [ -n "$perm" ] && chown "$perm" "$tmp"/* + chmod 640 "$tmp"/* + mv "$tmp"/* ./ + ) 2>&1 | true + rm -rf "$tmp" } ldap_gup() { - local usage="Usage: ldap_gup [filter attrs ...]" - case "$*" in - -h|-help) echo $usage; return;; - esac - - eval ' - local args - slapcat_csv "(&(objectClass=posixGroup)(memberUid=*))" cn memberUid | while read -r -a rec; do - - group=${rec[0]} - rec=("${rec[@]:1}") # shift - - for uid in ${rec[@]}; do - #echo slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword - rec=( $(slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword uid mail) ) - pass=${rec[0]} - rec=("${rec[@]:1}") # shift - for field in ${rec[@]}; do - echo $group $field:$pass - done - done - - done - ' - return + local usage="Usage: ldap_gup [filter attrs ...]" + case "$*" in + -h|-help) echo $usage; return;; + esac + local filer=$1; shift; [ -n "$filter" ] && filter="($filter)" + local attrs=${*:-"cn"}; shift + + eval ' + local args + slapcat_csv "(&(objectClass=posixGroup)(memberUid=*)$filter)" cn memberUid | while read -r -a rec; do + + group=${rec[0]} + rec=("${rec[@]:1}") # shift + + for uid in ${rec[@]}; do + rec=( $(slapcat_csv "(&(uid=$uid)(userPassword=*))" userPassword $attrs) ) + pass=${rec[0]} + rec=("${rec[@]:1}") # shift + for field in ${rec[@]}; do + echo $group:$field:$pass + done + done + + done + ' + return } -- 2.47.3