creds_msdos revision 1.4.4.2 1 1.4.4.2 martin #!/bin/sh
2 1.4.4.2 martin #
3 1.4.4.2 martin # $NetBSD: creds_msdos,v 1.4.4.2 2020/04/13 07:45:36 martin Exp $
4 1.4.4.2 martin #
5 1.4.4.2 martin # Copyright (c) 2019 Matthew R. Green
6 1.4.4.2 martin # All rights reserved.
7 1.4.4.2 martin #
8 1.4.4.2 martin # Redistribution and use in source and binary forms, with or without
9 1.4.4.2 martin # modification, are permitted provided that the following conditions
10 1.4.4.2 martin # are met:
11 1.4.4.2 martin # 1. Redistributions of source code must retain the above copyright
12 1.4.4.2 martin # notice, this list of conditions and the following disclaimer.
13 1.4.4.2 martin # 2. Redistributions in binary form must reproduce the above copyright
14 1.4.4.2 martin # notice, this list of conditions and the following disclaimer in the
15 1.4.4.2 martin # documentation and/or other materials provided with the distribution.
16 1.4.4.2 martin # 3. The name of the author may not be used to endorse or promote products
17 1.4.4.2 martin # derived from this software without specific prior written permission.
18 1.4.4.2 martin #
19 1.4.4.2 martin # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.4.4.2 martin # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.4.4.2 martin # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.4.4.2 martin # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.4.4.2 martin # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.4.4.2 martin # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.4.4.2 martin # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.4.4.2 martin # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.4.4.2 martin # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.4.4.2 martin # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.4.4.2 martin # SUCH DAMAGE.
30 1.4.4.2 martin
31 1.4.4.2 martin #
32 1.4.4.2 martin # If "creds_msdos_partition" is an msdos partition and has a creds.txt
33 1.4.4.2 martin # in it, perform these commands:
34 1.4.4.2 martin # "sshkeyfile <user> <path on msdos>"
35 1.4.4.2 martin # "sshkey <user> <entry>"
36 1.4.4.2 martin # "useraddhash <user> <passwd hash>"
37 1.4.4.2 martin # "useradd <user> <passwd>"
38 1.4.4.2 martin # If the "useradd" method is used, this the creds.txt file will be
39 1.4.4.2 martin # shredded and deleted with rm -P.
40 1.4.4.2 martin
41 1.4.4.2 martin # PROVIDE: creds_msdos
42 1.4.4.2 martin # REQUIRE: mountall
43 1.4.4.2 martin
44 1.4.4.2 martin $_rc_subr_loaded . /etc/rc.subr
45 1.4.4.2 martin
46 1.4.4.2 martin name="creds_msdos"
47 1.4.4.2 martin start_cmd="creds_msdos_start"
48 1.4.4.2 martin stop_cmd=":"
49 1.4.4.2 martin
50 1.4.4.2 martin fail() {
51 1.4.4.2 martin echo "$@" 1>&2
52 1.4.4.2 martin exit 1
53 1.4.4.2 martin }
54 1.4.4.2 martin
55 1.4.4.2 martin # This uses $ssh_userkeys global
56 1.4.4.2 martin sshkey_setup() {
57 1.4.4.2 martin local user="$1"
58 1.4.4.2 martin local group="wheel"
59 1.4.4.2 martin
60 1.4.4.2 martin # don't create existing users
61 1.4.4.2 martin if ! id -u "${user}" > /dev/null 2>&1; then
62 1.4.4.2 martin useradd -m -G "${group}" "${user}" || fail "Useradd failed."
63 1.4.4.2 martin fi
64 1.4.4.2 martin
65 1.4.4.2 martin eval ssh_userdir=~"${user}/.ssh"
66 1.4.4.2 martin mkdir -p -m 755 "${ssh_userdir}" || fail "mkdir ~/.ssh failed."
67 1.4.4.2 martin chmod 755 "${ssh_userdir}"
68 1.4.4.2 martin chown "${user}" "${ssh_userdir}"
69 1.4.4.2 martin
70 1.4.4.2 martin ssh_userkeys="${ssh_userdir}/authorized_keys"
71 1.4.4.2 martin }
72 1.4.4.2 martin
73 1.4.4.2 martin sshkey_finish() {
74 1.4.4.2 martin local user="$1"
75 1.4.4.2 martin
76 1.4.4.2 martin chmod 644 "${ssh_userkeys}"
77 1.4.4.2 martin chown "${user}" "${ssh_userkeys}"
78 1.4.4.2 martin }
79 1.4.4.2 martin
80 1.4.4.2 martin do_sshkeyfile() {
81 1.4.4.2 martin local user="$1"
82 1.4.4.2 martin local newkeys="${creds_msdos_partition}/$2"
83 1.4.4.2 martin
84 1.4.4.2 martin if [ ! -f "${newkeys}" ]; then
85 1.4.4.2 martin return
86 1.4.4.2 martin fi
87 1.4.4.2 martin
88 1.4.4.2 martin sshkey_setup "${user}"
89 1.4.4.2 martin
90 1.4.4.2 martin # check entry is not present
91 1.4.4.2 martin while read type keydata name; do
92 1.4.4.2 martin if fgrep -q "${keydata}" "${ssh_userkeys}" 2>/dev/null; then
93 1.4.4.2 martin continue
94 1.4.4.2 martin fi
95 1.4.4.2 martin echo "${type} ${keydata} ${name}" >> "${ssh_userkeys}"
96 1.4.4.2 martin done < "${newkeys}"
97 1.4.4.2 martin
98 1.4.4.2 martin sshkey_finish "${user}"
99 1.4.4.2 martin }
100 1.4.4.2 martin
101 1.4.4.2 martin do_sshkey() {
102 1.4.4.2 martin local user="$1"
103 1.4.4.2 martin local newkey="$2"
104 1.4.4.2 martin
105 1.4.4.2 martin sshkey_setup "${user}"
106 1.4.4.2 martin
107 1.4.4.2 martin echo "${newkey}" >> "${ssh_userkeys}"
108 1.4.4.2 martin
109 1.4.4.2 martin sshkey_finish "${user}"
110 1.4.4.2 martin }
111 1.4.4.2 martin
112 1.4.4.2 martin do_useraddpwhash() {
113 1.4.4.2 martin local user="$1"
114 1.4.4.2 martin local pwhash="$2"
115 1.4.4.2 martin local group="wheel"
116 1.4.4.2 martin
117 1.4.4.2 martin # don't add to existing users
118 1.4.4.2 martin if id -u "${user}" > /dev/null 2>&1; then
119 1.4.4.2 martin return
120 1.4.4.2 martin fi
121 1.4.4.2 martin
122 1.4.4.2 martin useradd -m -p "${pwhash}" -G "${group}" "${user}" || fail "Useradd failed."
123 1.4.4.2 martin }
124 1.4.4.2 martin
125 1.4.4.2 martin do_useradd() {
126 1.4.4.2 martin local user="$1"
127 1.4.4.2 martin local password="$2"
128 1.4.4.2 martin
129 1.4.4.2 martin local pwhash=$(pwhash "$password")
130 1.4.4.2 martin do_useraddpwhash "${user}" "${pwhash}"
131 1.4.4.2 martin }
132 1.4.4.2 martin
133 1.4.4.2 martin creds_msdos_start()
134 1.4.4.2 martin {
135 1.4.4.2 martin local fstab_file=/etc/fstab
136 1.4.4.2 martin
137 1.4.4.2 martin if [ -z "${creds_msdos_partition}" ]; then
138 1.4.4.2 martin echo "Not looking for credentials on msdos"
139 1.4.4.2 martin return
140 1.4.4.2 martin fi
141 1.4.4.2 martin while read junk1 mp fstype junk2; do
142 1.4.4.2 martin if [ "${mp}" != "${creds_msdos_partition}" ]; then
143 1.4.4.2 martin continue
144 1.4.4.2 martin fi
145 1.4.4.2 martin if [ "${fstype}" != "msdos" ]; then
146 1.4.4.2 martin echo "Not checking for creds on ${creds_msdos_partition}: not an msdos file system"
147 1.4.4.2 martin return
148 1.4.4.2 martin fi
149 1.4.4.2 martin break
150 1.4.4.2 martin done < "${fstab_file}"
151 1.4.4.2 martin
152 1.4.4.2 martin local delete_creds=no
153 1.4.4.2 martin local creds_file="${creds_msdos_partition}/creds.txt"
154 1.4.4.2 martin
155 1.4.4.2 martin if [ -f "${creds_file}" ]; then
156 1.4.4.2 martin while read type user args; do
157 1.4.4.2 martin # strip cr
158 1.4.4.2 martin local clean_args=$(echo "$args" | tr -d '\015')
159 1.4.4.2 martin case "$type" in
160 1.4.4.2 martin \#*|'')
161 1.4.4.2 martin continue
162 1.4.4.2 martin ;;
163 1.4.4.2 martin sshkeyfile)
164 1.4.4.2 martin echo "Added user ${user} via ssh key file method."
165 1.4.4.2 martin do_sshkeyfile "${user}" "${clean_args}"
166 1.4.4.2 martin ;;
167 1.4.4.2 martin sshkey)
168 1.4.4.2 martin echo "Added user ${user} via ssh key string method."
169 1.4.4.2 martin do_sshkey "${user}" "${clean_args}"
170 1.4.4.2 martin ;;
171 1.4.4.2 martin useraddpwhash)
172 1.4.4.2 martin echo "Added user ${user} via password hash method."
173 1.4.4.2 martin do_useraddpwhash "${user}" "${clean_args}"
174 1.4.4.2 martin ;;
175 1.4.4.2 martin useradd)
176 1.4.4.2 martin echo "Added user ${user} via password method, shredding credentials file."
177 1.4.4.2 martin do_useradd "${user}" "${clean_args}"
178 1.4.4.2 martin delete_creds=yes
179 1.4.4.2 martin ;;
180 1.4.4.2 martin *)
181 1.4.4.2 martin echo "Do not understand '$type' creds" 1>&2
182 1.4.4.2 martin exit 1
183 1.4.4.2 martin ;;
184 1.4.4.2 martin esac
185 1.4.4.2 martin done < "${creds_file}"
186 1.4.4.2 martin fi
187 1.4.4.2 martin
188 1.4.4.2 martin if [ $delete_creds = yes ]; then
189 1.4.4.2 martin rm -P -f "${creds_file}"
190 1.4.4.2 martin fi
191 1.4.4.2 martin }
192 1.4.4.2 martin
193 1.4.4.2 martin load_rc_config $name
194 1.4.4.2 martin run_rc_command "$1"
195