K5D

              cut'n'paste
              ° √ ∇ Ξ Σ Π Δ ß § №
              Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω
              α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω

PASSWD GROUP SHADOW
===================

# FORMATS
---------

/etc/passwd

  username:password:UID:GID:user_id_info:home_directory:command/shell

/etc/group

  groupname:password:GID:member,member,..member

/etc/shadow

  username:password:lastchanged:minimum:maximum:warn:inactive:expire


# TASK
------

Create 3 bash scripts to explain in **human readable** format contents of the /etc/group , /etc/passwd and /etc/shadow files in your Alpine Linux machine. Use following patterns:

days since 1970/1/1 into human readable date

  date --utc -d @$(($1*3600*24))

username from UID

  id -nu $UID

groupname from GID (1000 as an example)

  getent group 1000 | cut -d: -f1

loop through all lines in file.txt

  while read -r line; do echo "$line"; done < file.txt
  
  -r do not allow backslashes to escape any characters

loop through all lines in file.txt and set field separator as $IFS

  IFS=':'; while read f1 f2 frest; do echo "$f1 $f2"; done < file.txt

use return in bash function and special variable $? to return number

  foo () { local x=123; return $x; }; foo; echo $?

use echo in bash function to return string

  f () { echo $2 and $1 are alright tonight; }; echo $(f Mars Venus)

Also use links below:


# LINKS
-------

[1]. Understanding /etc/group File

  https://www.cyberciti.biz/faq/understanding-etcgroup-file/

[2]. Understanding /etc/passwd File Format

  https://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/

[3]. Understanding /etc/shadow file

  https://www.cyberciti.biz/faq/understanding-etcshadow-file/

[4]. Calculate Days Since 1/1/1970

  https://serverfault.com/questions/220633
    /calculate-days-since-1-1-1970

[5]. Bash Scripting Tutorial - 7. Functions

  https://ryanstutorials.net
    /bash-scripting-tutorial/bash-functions.php


# SCRIPTS
---------

foo:~/bar# cat pass.sh
#!/bin/bash

getgid() {
  echo $(getent group $1 | cut -d: -f1)
}

IFS=':'
while read f1 f2 f3 f4 f5 f6 f7
do
  echo "------------------"
  echo "user name:     $f1"
  echo "password:      $f2"
  echo "UID:           $f3"
  echo -n "group name: $(getgid $f3) "; echo "(GID: $f4)"
  echo "user info:     $f5"
  echo "home folder:   $f6"
  echo "command/shell: $f7"
done < /etc/passwd

foo:~/bar# cat grou.sh
#!/bin/bash

IFS=':'
while read f1 f2 f3 f4
do
  echo "------------------"
  echo "group name:    $f1"
  echo "password:      $f2"
  echo "GID:           $f3"
  echo "group list:    $f4"
done < /etc/group

foo:~/bar# cat shad.sh
#!/bin/bash

IFS=':'

while read f1 f2 f3 f4 f5 f6 f7 f8
do
  if [ "$f3" = "" ]; then f3='0'; fi
  if [ "$f4" = "" ]; then f4='-'; fi
  if [ "$f5" = "" ]; then f5='-'; fi
  if [ "$f6" = "" ]; then f6='-'; fi
  if [ "$f7" = "" ]; then f7='-'; fi
  if [ "$f8" = "" ]; then f8='-'; fi

  x=`date --utc -d @$(($f3*3600*24))`

  echo "------------------"
  echo "Username:    $f1"
  echo "Password:    $f2"
  echo "LastChanged: $x"
  echo "Minimum:     $f4 days"
  echo "Maximum:     $f5 days"
  echo "Warning in:  $f6 days"
  echo "Inactive:    $f7 days"
  echo "Expire:      $f8 days"
done < /etc/shadow

foo:~/bar#


# FILES TO RETURN
-----------------

1. alpino (with bash installed) linux scripts sources.

2. /etc/passwd, /etc/group, /etc/shadow that scripts were tested with.


# OUTPUT

foo:~/bar# bash pass.sh
------------------
user name:     root
password:      x
UID:           0
group name: root (GID: 0)
user info:     root
home folder:   /root
command/shell: /bin/ash
------------------
user name:     bin
password:      x
UID:           1
group name: bin (GID: 1)
user info:     bin
home folder:   /bin
command/shell: /sbin/nologin
------------------
user name:     daemon
password:      x
UID:           2
group name: daemon (GID: 2)
user info:     daemon
home folder:   /sbin
command/shell: /sbin/nologin
------------------
user name:     adm
password:      x
UID:           3
group name: sys (GID: 4)
user info:     adm
home folder:   /var/adm
command/shell: /sbin/nologin
------------------
user name:     lp
password:      x
UID:           4
group name: adm (GID: 7)
user info:     lp
home folder:   /var/spool/lpd
command/shell: /sbin/nologin
------------------
user name:     sync
password:      x
UID:           5
group name: tty (GID: 0)
user info:     sync
home folder:   /sbin
command/shell: /bin/sync
------------------
user name:     shutdown
password:      x
UID:           6
group name: disk (GID: 0)
user info:     shutdown
home folder:   /sbin
command/shell: /sbin/shutdown
------------------
user name:     halt
password:      x
UID:           7
group name: lp (GID: 0)
user info:     halt
home folder:   /sbin
command/shell: /sbin/halt
------------------
user name:     mail
password:      x
UID:           8
group name: mem (GID: 12)
user info:     mail
home folder:   /var/mail
command/shell: /sbin/nologin
------------------
user name:     news
password:      x
UID:           9
group name: kmem (GID: 13)
user info:     news
home folder:   /usr/lib/news
command/shell: /sbin/nologin
------------------
user name:     uucp
password:      x
UID:           10
group name: wheel (GID: 14)
user info:     uucp
home folder:   /var/spool/uucppublic
command/shell: /sbin/nologin
------------------
user name:     operator
password:      x
UID:           11
group name: floppy (GID: 0)
user info:     operator
home folder:   /root
command/shell: /sbin/nologin
------------------
user name:     man
password:      x
UID:           13
group name: news (GID: 15)
user info:     man
home folder:   /usr/man
command/shell: /sbin/nologin
------------------
user name:     postmaster
password:      x
UID:           14
group name: uucp (GID: 12)
user info:     postmaster
home folder:   /var/mail
command/shell: /sbin/nologin
------------------
user name:     cron
password:      x
UID:           16
group name: cron (GID: 16)
user info:     cron
home folder:   /var/spool/cron
command/shell: /sbin/nologin
------------------
user name:     ftp
password:      x
UID:           21
group name: ftp (GID: 21)
user info:
home folder:   /var/lib/ftp
command/shell: /sbin/nologin
------------------
user name:     sshd
password:      x
UID:           22
group name: sshd (GID: 22)
user info:     sshd
home folder:   /dev/null
command/shell: /sbin/nologin
------------------
user name:     at
password:      x
UID:           25
group name: at (GID: 25)
user info:     at
home folder:   /var/spool/cron/atjobs
command/shell: /sbin/nologin
------------------
user name:     squid
password:      x
UID:           31
group name: squid (GID: 31)
user info:     Squid
home folder:   /var/cache/squid
command/shell: /sbin/nologin
------------------
user name:     xfs
password:      x
UID:           33
group name: xfs (GID: 33)
user info:     X Font Server
home folder:   /etc/X11/fs
command/shell: /sbin/nologin
------------------
user name:     games
password:      x
UID:           35
group name: games (GID: 35)
user info:     games
home folder:   /usr/games
command/shell: /sbin/nologin
------------------
user name:     cyrus
password:      x
UID:           85
group name: usb (GID: 12)
user info:
home folder:   /usr/cyrus
command/shell: /sbin/nologin
------------------
user name:     vpopmail
password:      x
UID:           89
group name: vpopmail (GID: 89)
user info:
home folder:   /var/vpopmail
command/shell: /sbin/nologin
------------------
user name:     ntp
password:      x
UID:           123
group name: ntp (GID: 123)
user info:     NTP
home folder:   /var/empty
command/shell: /sbin/nologin
------------------
user name:     smmsp
password:      x
UID:           209
group name: smmsp (GID: 209)
user info:     smmsp
home folder:   /var/spool/mqueue
command/shell: /sbin/nologin
------------------
user name:     guest
password:      x
UID:           405
group name:  (GID: 100)
user info:     guest
home folder:   /dev/null
command/shell: /sbin/nologin
------------------
user name:     nobody
password:      x
UID:           65534
group name: nobody (GID: 65534)
user info:     nobody
home folder:   /
command/shell: /sbin/nologin
------------------
user name:     chrony
password:      x
UID:           100
group name: users (GID: 101)
user info:     chrony
home folder:   /var/log/chrony
command/shell: /sbin/nologin
------------------
user name:     opilane
password:      x
UID:           1000
group name: opilane (GID: 1000)
user info:     Linux User,,,
home folder:   /home/opilane
command/shell: /bin/ash
------------------
user name:     vsftp
password:      x
UID:           101
group name: chrony (GID: 21)
user info:     vsftp
home folder:   /var/lib/ftp
command/shell: /sbin/nologin
------------------
user name:     apache
password:      x
UID:           102
group name: apache (GID: 102)
user info:     apache
home folder:   /var/www
command/shell: /sbin/nologin
------------------
user name:     svn
password:      x
UID:           103
group name: svnusers (GID: 103)
user info:     svn
home folder:   /var/svn
command/shell: /sbin/nologin
------------------
user name:     messagebus
password:      x
UID:           104
group name: messagebus (GID: 104)
user info:     messagebus
home folder:   /dev/null
command/shell: /sbin/nologin
------------------
user name:     polkitd
password:      x
UID:           105
group name: polkitd (GID: 105)
user info:     polkitd
home folder:   /var/empty
command/shell: /sbin/nologin


foo:~/bar# bash grou.sh
------------------
group name:    root
password:      x
GID:           0
group list:    root
------------------
group name:    bin
password:      x
GID:           1
group list:    root,bin,daemon
------------------
group name:    daemon
password:      x
GID:           2
group list:    root,bin,daemon
------------------
group name:    sys
password:      x
GID:           3
group list:    root,bin,adm
------------------
group name:    adm
password:      x
GID:           4
group list:    root,adm,daemon
------------------
group name:    tty
password:      x
GID:           5
group list:
------------------
group name:    disk
password:      x
GID:           6
group list:    root,adm
------------------
group name:    lp
password:      x
GID:           7
group list:    lp
------------------
group name:    mem
password:      x
GID:           8
group list:
------------------
group name:    kmem
password:      x
GID:           9
group list:
------------------
group name:    wheel
password:      x
GID:           10
group list:    root
------------------
group name:    floppy
password:      x
GID:           11
group list:    root
------------------
group name:    mail
password:      x
GID:           12
group list:    mail
------------------
group name:    news
password:      x
GID:           13
group list:    news
------------------
group name:    uucp
password:      x
GID:           14
group list:    uucp
------------------
group name:    man
password:      x
GID:           15
group list:    man
------------------
group name:    cron
password:      x
GID:           16
group list:    cron
------------------
group name:    console
password:      x
GID:           17
group list:
------------------
group name:    audio
password:      x
GID:           18
group list:
------------------
group name:    cdrom
password:      x
GID:           19
group list:
------------------
group name:    dialout
password:      x
GID:           20
group list:    root
------------------
group name:    ftp
password:      x
GID:           21
group list:    vsftp
------------------
group name:    sshd
password:      x
GID:           22
group list:
------------------
group name:    input
password:      x
GID:           23
group list:
------------------
group name:    at
password:      x
GID:           25
group list:    at
------------------
group name:    tape
password:      x
GID:           26
group list:    root
------------------
group name:    video
password:      x
GID:           27
group list:    root
------------------
group name:    netdev
password:      x
GID:           28
group list:
------------------
group name:    readproc
password:      x
GID:           30
group list:
------------------
group name:    squid
password:      x
GID:           31
group list:    squid
------------------
group name:    xfs
password:      x
GID:           33
group list:    xfs
------------------
group name:    kvm
password:      x
GID:           34
group list:    kvm
------------------
group name:    games
password:      x
GID:           35
group list:
------------------
group name:    shadow
password:      x
GID:           42
group list:
------------------
group name:    cdrw
password:      x
GID:           80
group list:
------------------
group name:    usb
password:      x
GID:           85
group list:
------------------
group name:    vpopmail
password:      x
GID:           89
group list:
------------------
group name:    users
password:      x
GID:           100
group list:    games
------------------
group name:    ntp
password:      x
GID:           123
group list:
------------------
group name:    nofiles
password:      x
GID:           200
group list:
------------------
group name:    smmsp
password:      x
GID:           209
group list:    smmsp
------------------
group name:    locate
password:      x
GID:           245
group list:
------------------
group name:    abuild
password:      x
GID:           300
group list:    opilane
------------------
group name:    utmp
password:      x
GID:           406
group list:
------------------
group name:    ping
password:      x
GID:           999
group list:
------------------
group name:    nogroup
password:      x
GID:           65533
group list:
------------------
group name:    nobody
password:      x
GID:           65534
group list:
------------------
group name:    chrony
password:      x
GID:           101
group list:    chrony
------------------
group name:    opilane
password:      x
GID:           1000
group list:
------------------
group name:    www-data
password:      x
GID:           82
group list:    apache
------------------
group name:    apache
password:      x
GID:           102
group list:    apache
------------------
group name:    svnusers
password:      x
GID:           103
group list:    svn
------------------
group name:    messagebus
password:      x
GID:           104
group list:    messagebus
------------------
group name:    polkitd
password:      x
GID:           105
group list:    polkitd
------------------
group name:    vboxsf
password:      x
GID:           106
group list:


foo:~/bar# bash shad.sh
------------------
Username:    root
Password:    $6$3GxLbGNqQpjjDuJ2$wQXIUirnvrBQu3wClV2tOBRfVFx9i0TPZwTj61zzHTLLHfnnuw5rjLDB58wlvA5PNujwExpfnemyDybu2QBC7.
LastChanged: Sat Apr 25 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    bin
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    daemon
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    adm
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    lp
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    sync
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    shutdown
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    halt
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    mail
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    news
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    uucp
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    operator
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    man
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    postmaster
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    cron
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    ftp
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    sshd
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    at
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    squid
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    xfs
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    games
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    cyrus
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    vpopmail
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    ntp
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    smmsp
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    guest
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    nobody
Password:    !
LastChanged: Thu Jan  1 00:00:00 UTC 1970
Minimum:     0 days
Maximum:     - days
Warning in:  - days
Inactive:    - days
Expire:      - days
------------------
Username:    chrony
Password:    !
LastChanged: Sat Apr 25 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    opilane
Password:    $6$JRogbr8UWakkwavR$I1OOW1gN63MEkhzBuAMkRnWcbQLAru8K4RfD0Qaq/BFI30tBb97qoNSonnePOLsFRl8twzrHG41jWyRpVMXzZ0
LastChanged: Sat Apr 25 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    vsftp
Password:    !
LastChanged: Tue Apr 28 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    apache
Password:    !
LastChanged: Tue Apr 28 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    svn
Password:    !
LastChanged: Tue Apr 28 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    messagebus
Password:    !
LastChanged: Thu Apr 30 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
------------------
Username:    polkitd
Password:    !
LastChanged: Thu Apr 30 00:00:00 UTC 2020
Minimum:     0 days
Maximum:     99999 days
Warning in:  7 days
Inactive:    - days
Expire:      - days
foo:~/bar#
