]> rtime.felk.cvut.cz Git - novaboot.git/blob - server/adduser-novaboot
adduser-novaboot: Reload systemd after adding override units
[novaboot.git] / server / adduser-novaboot
1 #!/bin/sh
2
3 set -e
4
5 die() {
6     echo >&2 "$@"
7     exit 1
8 }
9
10 print_help() {
11     cat <<EOF
12 Usage: adduser-novaboot --key KEY [--admin-id=NAME] [adduser options] user
13 EOF
14 }
15
16 TEMP=$(getopt -o 'h' --long 'admin-id:,key:,help,home:,uid:,firstuid:,lastuid:,gecos:,ingroup:,gid:' -n "${0##*/}" -- "$@")
17 [ $? -ne 0 ] && die "getopt error"
18 eval set -- "$TEMP"
19 unset TEMP
20
21 admin=admin
22 key=
23 while true; do
24     case "$1" in
25         '--admin-id')
26             admin=$2
27             shift 2;;
28         '--key')
29             keysrc=$2
30             shift 2;;
31         '-h' | '--help')
32             print_help; exit;;
33         '--')
34             shift; break;;
35         *)
36             adduser_opts="$adduser_opts $1 $2"
37             shift 2;;
38     esac
39 done
40
41 [ -z "$keysrc" ] && die "Missing --key option"
42
43 if [ "$keysrc" = "-" ]; then
44     key=$(cat)
45 else
46     key=$(cat "$keysrc")
47 fi
48
49 [ -z "$key" -o "$(echo "$key" | wc -l)" -ne 1 ] && die "--key needs to be just one line"
50 echo "$key" | grep -q ssh || die "--key does not look like an SSH public key"
51
52 adduser --disabled-password --shell $(which novaboot-shell) $adduser_opts "$@"
53
54 user="$1"
55 home=$(getent passwd "$user"|awk -F: '{print $6;}')
56 uid=$(id -u "$user")
57
58 echo "Creating $home/.ssh/authorized_keys"
59 mkdir -p -m 700 "$home/.ssh"
60 echo "command=\"user $admin admin\" $key" >> $home/.ssh/authorized_keys
61 chown $user: "$home/.ssh" "$home/.ssh/authorized_keys"
62
63 if [ -d /srv/tftp -a ! -e /srv/tftp/$user  ]; then
64     echo "Creating /srv/tftp/$user and symlink to it from $home/tftproot."
65     mkdir -p /srv/tftp/$user
66     chown $user /srv/tftp/$user
67     ln -s /srv/tftp/$user $home/tftproot
68 else
69     echo "NOT creating /srv/tftp/$user and symlink to it from $home/tftproot."
70 fi
71
72 if [ -d /run/systemd/system ]; then
73     echo "Installing systemd services and timers in /etc/systemd/system/user@$uid.service.d"
74     mkdir -p /etc/systemd/system/user@$uid.service.d
75     cat <<EOF > /etc/systemd/system/user@$uid.service.d/novaboot-server.conf
76 [Unit]
77 Requires=novaboot-server-session@$user.service
78 After=novaboot-server-session@$user.service
79 EOF
80     systemctl daemon-reload
81 fi
82
83 echo "Creating configuration template in $home/.novaboot-shell"
84 cat <<'CONFIG_EOF' > $home/.novaboot-shell
85 #!/bin/sh
86 #
87 # Configuration for novaboot-shell
88 #
89
90 #console_cmd='sterm -s 115200 /dev/ttyUSB0'
91
92 #reset_cmd='/bin/sh -c "(usbrelay LY03X_2=1; sleep 0.1; usbrelay LY03X_2=0) 2>/dev/null"'
93
94 #on_cmd='/bin/sh -c "(usbrelay LY03X_1=1; sleep 0.1; usbrelay LY03X_1=0) 2>/dev/null"';
95 #off_cmd='/bin/sh -c "(usbrelay LY03X_1=1; sleep 7.0; usbrelay LY03X_1=0) 2>/dev/null"';
96
97 # target_config="\
98 # --prefix=/prefix/
99 # --uboot==>
100 # --uboot-init=setenv serverip 192.168.1.1
101 # --uboot-addr=kernel=0x81000000
102 # --uboot-addr=fdt=0x83000000
103 # --uboot-addr=ramdisk=0x83100000
104 # "
105 CONFIG_EOF
106 chown $user: $home/.novaboot-shell
107
108 echo "Done"
109 exit 0
110
111 : <<EOF
112 =encoding utf8
113
114 =head1 NAME
115
116 adduser-novaboot - create user account for use with novaboot's --ssh option
117
118 =head1 SYNOPSIS
119
120 B<adduser-novaboot> --key KEY [--admin-id NAME] [adduser options] user
121
122 =head1 DESCRIPTION
123
124 B<adduser-novaboot> is a wrapper of L<adduser(8)> command that
125 simplifies creation of user accounts for I<novaboot>'s --ssh option.
126 The created account has its shell set to L<novaboot-shell(1)>. The
127 command also creates a template of the configuration file, sets up
128 administrator's SSH key in L<authorized_keys(5)> prepares directories
129 and symlinks that for integration with TFTP server and, when run under
130 L<systemd(1)>, configures systemd service files to automatically
131 power-off the target after timeout.
132
133 =head1 OPTIONS
134
135 =over 8
136
137 =item --key KEY
138
139 Mandatory argument specifying administrator's public SSH key (e.g.
140 F<~/.ssh/id_rsa.pub>). The key will be copied to the created account's
141 F<~/.ssh/authorized_keys> and marked with administrator flag.
142
143 =item --admin-id NAME
144
145 User name associated with the key. This user name is shown to
146 connecting users when the target is occupied by the administrator.
147 When omitted, I<admin> is used as the user name.
148
149 =back
150
151 =head1 AUTHORS
152
153 Michal Sojka <sojkam1@fel.cvut.cz>
154
155 Latest version can be found at
156 L<https://github.com/wentasah/novaboot>.
157
158 =cut
159 EOF