File: ldapid

package info (click to toggle)
ldapscripts 2.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 496 kB
  • sloc: sh: 707; makefile: 167
file content (129 lines) | stat: -rwxr-xr-x 4,245 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh

#  ldapid : displays a user's list of IDs

#  Copyright (C) 2008-2017 Ganal LAPLANCHE
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
  echo "Usage : $0 [-P] [username | uid]"
  exit 1
fi

# Source runtime file
_RUNTIMEFILE="/usr/lib/ldapscripts/runtime"
. "$_RUNTIMEFILE"

# Parse options
case "$1" in
  "-P")
    # Passwd-like display requested
    if [ -z "$2" ]
    then
      # Current user
      _USER="$USER"
    else
      _USER="$2"
    fi
    ;;
  "")
    # Standard display requested (current user)
    _USER="$USER"
    ;;
  *)
    # Standard display requested (with arg)
    _USER="$1"
    ;;
esac

# Check username
_findentry "$USUFFIX,$SUFFIX" "(&(objectClass=posixAccount)(|(uid=$_USER)(uidNumber=$_USER)))"
[ -z "$_ENTRY" ] && end_die "User $_USER not found in LDAP"

# Get each (common) attribute 
# uidNumber
_getattribute "$_ENTRY" "uidNumber"
[ -z "$_ATTRIBUTE" ] && end_die "Error getting user attribute from LDAP (uidNumber)"
_UIDNUMBER="$_ATTRIBUTE"
# uid (login)
_getattribute "$_ENTRY" "uid"
[ -z "$_ATTRIBUTE" ] && end_die "Error getting user attribute from LDAP (uid)"
_UID="$_ATTRIBUTE"
# gidNumber
_getattribute "$_ENTRY" "gidNumber"
[ -z "$_ATTRIBUTE" ] && end_die "Error getting user attribute from LDAP (gidNumber)"
_PRIMARYGID="$_ATTRIBUTE"

case "$1" in
  "-P")
    # Passwd-like display requested
    # Fetch additional attributes
    _getattribute "$_ENTRY" "gecos"
    _GECOS=${_ATTRIBUTE:-""}
    _getattribute "$_ENTRY" "homeDirectory"
    _HOMEDIR=${_ATTRIBUTE:-""}
    _getattribute "$_ENTRY" "loginShell"
    _SHELL=${_ATTRIBUTE:-""}
    _getattribute "$_ENTRY" "userPassword"
    _PASSWD=${_ATTRIBUTE:-""}
    is_yes "$_B64" && _PASSWD=$(echo -n $_PASSWD | _b64decode | _utf8decode)
    # Remove leading encoding scheme
    _PASSWD=$(echo $_PASSWD | sed "s|{.*}||")
    # Prepare output
    _OUTPUT="$_UID:$_PASSWD:$_UIDNUMBER:$_PRIMARYGID::0:0"
    _OUTPUT="$_OUTPUT:$_GECOS:$_HOMEDIR:$_SHELL"
    ;;
  *)
    # Standard display requested (current user)
    # Compute additional attribute : primary group name
    _PRIMARYGROUP=$(_gidtogroup "$_PRIMARYGID")
    [ -z "$_PRIMARYGROUP" ] && end_die "Cannot resolve gid $_PRIMARYGID to group : not found"
    # Prepare output
    _OUTPUT="uid=$_UIDNUMBER($_UID) gid=$_PRIMARYGID($_PRIMARYGROUP)"
    _OUTPUT="$_OUTPUT groups=$_PRIMARYGID($_PRIMARYGROUP)"
    # Get secondary groups (posixGroup)
    _SECONDARYGIDS=$(_ldapsearch "$GSUFFIX,$SUFFIX" "(&(objectClass=posixGroup)(memberUid=$_UID))" gidNumber | grep "gidNumber: " | sed "s|gidNumber: ||")
    for _SECONDARYGID in $_SECONDARYGIDS
    do
      _GID=$(_gidtogroup "$_SECONDARYGID")
      [ -z "$_GID" ] && end_die "Cannot resolve gid $_SECONDARYGID to group : not found"
      _OUTPUT="$_OUTPUT,$_SECONDARYGID($_GID)"
    done
    # Get member groups (groupOfNames, groupOfUniqueNames)
    if [ "$GCLASS" != "posixGroup" ]
    then
      _MEMBERGIDS=$(_ldapsearch "$GSUFFIX,$SUFFIX" "(&(objectClass=$GCLASS)($_GMEMBERATTR=$_ENTRY))" gidNumber | grep "gidNumber: " | sed "s|gidNumber: ||")
      _FIRSTPASS=""
      for _MEMBERGID in $_MEMBERGIDS
      do
        _GID=$(_gidtogroup "$_MEMBERGID")
        [ -z "$_GID" ] && end_die "Cannot resolve gid $_MEMBERGID to group : not found"
        if [ -z "$_FIRSTPASS" ]
        then
          _OUTPUT="$_OUTPUT groups(member)=$_MEMBERGID($_GID)"
          _FIRSTPASS="done"
        else
          _OUTPUT="$_OUTPUT,$_MEMBERGID($_GID)"
        fi
      done
    fi
    ;;
esac

# Display result
echo $_OUTPUT && end_ok