blob: 29073f0eb2a7e6e7ad12ba8d2b7d0d27bccb855a [file] [log] [blame]
Jean-Marie Verdunf2f4f122020-10-26 11:17:06 -07001####
2# Copyright 2020 Hewlett Packard Enterprise Development LP.
Jonathan Doman570ebbb2021-10-18 14:38:45 -07003# Copyright 2021 Intel Corporation
Jean-Marie Verdunf2f4f122020-10-26 11:17:06 -07004#
5# Add a basic class to add a privileged user from an ssh
6# standpoint and a public key passed as an input parameter
7# from the local.conf file
8# Example:
9# INHERIT += "phosphor-deploy-ssh-keys"
Jonathan Doman570ebbb2021-10-18 14:38:45 -070010#
11# SSH_KEYS = "vejmarie:/home/openbmc/openbmc/meta-hpe/keys/test.pub"
12# or
13# SSH_KEYS = "vejmarie:/home/openbmc/openbmc/meta-hpe/keys/test.pub;root:/path/to/id_rsa.pub"
Jean-Marie Verdunf2f4f122020-10-26 11:17:06 -070014####
15
16inherit useradd_base
17
18IMAGE_PREPROCESS_COMMAND += "deploy_local_user;"
19
20deploy_local_user () {
Patrick Williamsaf48f632023-03-20 10:13:55 -050021 if [ "${SSH_KEYS}" == "" ]; then
22 bbwarn "Trying to deploy SSH keys but input variable is empty (SSH_KEYS)"
23 return
24 fi
Jonathan Doman570ebbb2021-10-18 14:38:45 -070025
Patrick Williamsaf48f632023-03-20 10:13:55 -050026 ssh_keys="${SSH_KEYS}"
27 while [ "${ssh_keys}" != "" ]; do
28 current_key=`echo "$ssh_keys" | cut -d ';' -f1`
29 ssh_keys=`echo "$ssh_keys" | cut -s -d ';' -f2-`
Jonathan Doman570ebbb2021-10-18 14:38:45 -070030
Patrick Williamsaf48f632023-03-20 10:13:55 -050031 username=`echo "$current_key" | awk -F":" '{ print $1}'`
32 key_path=`echo "$current_key" | awk -F":" '{ print $2}'`
Jonathan Doman570ebbb2021-10-18 14:38:45 -070033
Patrick Williamsaf48f632023-03-20 10:13:55 -050034 if [ ! -d ${IMAGE_ROOTFS}/home/${username} ]; then
35 perform_useradd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} -p '' ${username}"
36 fi
Jonathan Doman570ebbb2021-10-18 14:38:45 -070037
Patrick Williamsaf48f632023-03-20 10:13:55 -050038 if [ ! -d ${IMAGE_ROOTFS}/home/${username}.ssh/ ]; then
39 install -d ${IMAGE_ROOTFS}/home/${username}/.ssh/
40 fi
Jonathan Doman570ebbb2021-10-18 14:38:45 -070041
Patrick Williamsaf48f632023-03-20 10:13:55 -050042 if [ ! -f ${IMAGE_ROOTFS}/home/${username}/.ssh/authorized_keys ]; then
43 install -m 0600 ${key_path} ${IMAGE_ROOTFS}/home/${username}/.ssh/authorized_keys
44 else
45 cat ${key_path} >> ${IMAGE_ROOTFS}/home/${username}/.ssh/authorized_keys
46 fi
Jonathan Doman570ebbb2021-10-18 14:38:45 -070047
Patrick Williamsaf48f632023-03-20 10:13:55 -050048 uid=`cat ${IMAGE_ROOTFS}/etc/passwd | grep "${username}:" | awk -F ":" '{print $3}'`
49 guid=`cat ${IMAGE_ROOTFS}/etc/passwd | grep "${username}:" | awk -F ":" '{print $4}'`
Jonathan Doman570ebbb2021-10-18 14:38:45 -070050
Patrick Williamsaf48f632023-03-20 10:13:55 -050051 chown -R ${uid}:${guid} ${IMAGE_ROOTFS}/home/${username}/.ssh
52 chmod 600 ${IMAGE_ROOTFS}/home/${username}/.ssh/authorized_keys
53 chmod 700 ${IMAGE_ROOTFS}/home/${username}/.ssh
Jonathan Doman570ebbb2021-10-18 14:38:45 -070054
Patrick Williamsaf48f632023-03-20 10:13:55 -050055 is_group=`grep "priv-admin" ${IMAGE_ROOTFS}/etc/group || true`
Jonathan Doman570ebbb2021-10-18 14:38:45 -070056
Patrick Williamsaf48f632023-03-20 10:13:55 -050057 if [ -z "${is_group}" ]; then
58 perform_groupadd "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} priv-admin"
59 fi
Jonathan Doman570ebbb2021-10-18 14:38:45 -070060
Patrick Williamsaf48f632023-03-20 10:13:55 -050061 perform_usermod "${IMAGE_ROOTFS}" "-R ${IMAGE_ROOTFS} -a -G priv-admin ${username}"
62 done
Jean-Marie Verdunf2f4f122020-10-26 11:17:06 -070063}