3ffa5f9613d5b488e1369327f94ca77a493d5ef4
[hcoop/scripts.git] / create-user
1 #!/bin/bash -ex
2
3 # MUST be executed:
4 # - on fritz
5 # - as a user with an /etc/sudoers line
6 # - member of "wheel" unix group on deleuze (FIXME: TRUE?)
7 # - while holding tickets for a user who can 'ssh -K' to mire
8 # - and is a member of "wheel" on mire
9 # - while holding tokens for a user who is:
10 # - a member of system:administrator
11 # - listed in 'bos listusers fritz'
12 # - and who has been set up with Domtool admin privileges by:
13 # - running 'domtool-adduser $USER' while holding AFS admin tokens as
14 # someone who is already a Domtool admin
15 # - running 'domtool-admin grant $USER priv all' as someone who is already a
16 # Domtool admin
17 # (To bootstrap yourself into admindom:
18 # 1. Run '/etc/init.d/domtool-server stop' on deleuze.
19 # 2. Run '/etc/init.d/domtool-slave stop' on all Domtool slave machines
20 # (e.g., mire).
21 # 3. Edit ~domtool/acl, following the example of adamc_admin to grant
22 # yourself 'priv all'.
23 # 4. Run '/etc/init.d/domtool-server start' on deleuze.
24 # 5. Run '/etc/init.d/domtool-slave start' on all Domtool slave
25 # machines.
26 # 6. Run 'domtool-adduser' as above.)
27
28 USER=$1
29
30 export PATH=$PATH:/afs/hcoop.net/common/bin/
31
32 if test -z "$USER"; then
33 echo "Invoke as create-user <USERNAME>"
34 exit 1
35 fi
36
37 #
38 # Helper functions
39 #
40
41 # Run a command on both mire and deleuze; assumes that no escaping is
42 # needed.
43 function mire_and_deleuze() {
44 execute_on_deleuze $*
45 execute_on_mire $*
46 }
47
48 function execute_on_deleuze () {
49 ssh -K deleuze.hcoop.net $*
50 }
51
52 function execute_on_mire () {
53 ssh -K mire.hcoop.net $*
54 }
55
56 function execute_on_all_machines () {
57 $*
58 ssh -K mire.hcoop.net $*
59 ssh -K hopper.hcoop.net $*
60 ssh -K deleuze.hcoop.net $*
61 }
62
63 #
64 # Kerberos principals
65 # (creat kerberos principals: fred, fred/cgi, fred/mailfilter)
66 #
67
68 # We use -randkey for user's main principal as well, to make sure that
69 # the creation process does not continue without having a main
70 # principal. (But you who want to set password for a user, don't
71 # worry - we'll invoke cpw later, so that it has the same effect
72 # as setting password right now - while it is more error tolerant).
73
74 sudo kadmin.local -p root/admin -q "ank -policy user -randkey +requires_preauth $USER@HCOOP.NET"
75 sudo kadmin.local -p root/admin -q "modprinc -maxlife 1day $USER@HCOOP.NET"
76 sudo kadmin.local -p root/admin -q "ank -policy daemon -randkey +requires_preauth $USER/daemon@HCOOP.NET"
77
78 #
79 # Create AFS users corresponding to krb5 principals.
80 # (fred/cgi principal == fred.cgi AFS user)
81 #
82
83 pts cu $USER || true
84 ID=`pts examine $USER | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
85 pts cu $USER.daemon || true
86 ID_DAEMON=`pts examine $USER.daemon | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
87
88
89 #
90 # Construct various paths for later perusal.
91 #
92
93 # (If it's not clear, for user fred, PATHBITS = f/fr/fred)
94 PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
95 HOMEPATH=/afs/hcoop.net/user/$PATHBITS
96 MAILPATH=/afs/hcoop.net/common/email/$PATHBITS
97
98
99 # LDAP bit excised (see git history...)
100
101 #
102 # Export .mailfilter and .cgi keys to a keytab file
103 #
104
105 # create a daemon keytab (used by /etc/exim4/get-token)
106 # *only* if it does not exist!
107 test -e /etc/keytabs/user.daemon/$USER || \
108 sudo kadmin.local -p root/admin -q "ktadd -k /etc/keytabs/user.daemon/$USER $USER/daemon@HCOOP.NET"
109
110 # Properly chown/mod keytab files (must be $USER:www-data)
111 sudo chown $USER:www-data /etc/keytabs/user.daemon/$USER
112 sudo chmod 440 /etc/keytabs/user.daemon/$USER
113
114 # rsync keytabs
115 (cd /etc/keytabs
116 sudo tar clpf - user.daemon/$USER | \
117 ssh mire.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
118 (cd /etc/keytabs
119 sudo tar clpf - user.daemon/$USER | \
120 ssh hopper.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
121 (cd /etc/keytabs
122 sudo tar clpf - user.daemon/$USER | \
123 ssh deleuze.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
124 (cd /etc/keytabs
125 sudo tar clpf - user.daemon/$USER | \
126 ssh navajos.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
127
128 #
129 # Create/mount/set-perms on user's volumes (home, mail, databases, logs)
130 #
131
132 # HOME VOLUME
133 if vos examine user.$USER.d 2>/dev/null; then
134 echo "Reactivating old volume (user.$USER.d)"
135 vos rename user.$USER.d user.$USER
136 fi
137 vos examine user.$USER 2>/dev/null || \
138 vos create fritz.hcoop.net /vicepa user.$USER -maxquota 400000
139
140 mkdir -p `dirname $HOMEPATH`
141 fs ls $HOMEPATH || test -L $HOMEPATH || fs mkm $HOMEPATH user.$USER
142 chown $USER:nogroup $HOMEPATH
143 fs sa $HOMEPATH $USER all
144 fs sa $HOMEPATH system:anyuser l
145
146 # Apache logs
147 mkdir -p $HOMEPATH/.logs
148 chown $USER:nogroup $HOMEPATH/.logs
149 mkdir -p $HOMEPATH/.logs/apache
150 chown $USER:nogroup $HOMEPATH/.logs/apache
151 fs sa $HOMEPATH/.logs/apache $USER.daemon rlwidk
152 mkdir -p $HOMEPATH/.logs/mail
153 fs sa $HOMEPATH/.logs/mail $USER.daemon rlwidk
154 chown $USER:nogroup $HOMEPATH/.logs/mail
155
156 # public_html
157 test -e $HOMEPATH/public_html || \
158 (mkdir -p $HOMEPATH/public_html; \
159 chown $USER:nogroup $HOMEPATH/public_html; \
160 fs sa $HOMEPATH/public_html system:anyuser none; \
161 fs sa $HOMEPATH/public_html $USER.daemon rl)
162
163 # .procmail.d
164 mkdir -p $HOMEPATH/.procmail.d
165 chown $USER:nogroup $HOMEPATH/.procmail.d
166 fs sa $HOMEPATH/.procmail.d system:anyuser rl
167
168 # .public
169 mkdir -p $HOMEPATH/.public/
170 chown $USER:nogroup $HOMEPATH/.public
171 fs sa $HOMEPATH/.public system:anyuser rl
172
173 # .domtool
174 mkdir -p $HOMEPATH/.public/.domtool
175 chown $USER:nogroup $HOMEPATH/.public/.domtool
176 test -e $HOMEPATH/.domtool || \
177 test -L $HOMEPATH/.domtool || \
178 execute_on_deleuze sudo -u $USER ln -s $HOMEPATH/.public/.domtool $HOMEPATH/.domtool
179 # ^^ work around sudo env_reset crap without having to
180 # actually figure out how to make it work cleanly -- clinton,
181 # 2011-11-30
182
183 # Gitweb hosting
184 test -L /var/cache/git/$USER || \
185 sudo ln -s $HOMEPATH/.hcoop-git /var/cache/git/$USER
186
187 # MAIL VOLUME
188 if vos examine mail.$USER.d 2>/dev/null; then
189 echo "Reactivating old volume (mail.$USER.d)"
190 vos rename mail.$USER.d mail.$USER
191 fi
192 vos examine mail.$USER 2>/dev/null || \
193 vos create fritz.hcoop.net /vicepa mail.$USER -maxquota 400000
194
195 mkdir -p `dirname $MAILPATH`
196 fs ls $MAILPATH || fs mkm $MAILPATH mail.$USER
197 fs ls $HOMEPATH/Maildir || fs mkm $HOMEPATH/Maildir mail.$USER
198 chown $USER:nogroup $MAILPATH
199 chown $USER:nogroup $HOMEPATH/Maildir
200 fs sa $MAILPATH $USER all
201 fs sa $MAILPATH $USER.daemon all
202 if test ! -e $MAILPATH/new; then
203 mkdir -p $MAILPATH/cur $MAILPATH/new $MAILPATH/tmp
204 echo -e "This email account is provided as a service for HCoop members." \
205 "\n\nTo learn how to use it, please visit the page" \
206 "\n<http://wiki.hcoop.net/MemberManual/Email> on our website."| \
207 mail -s "Welcome to your HCoop email store" \
208 -e -a "From: postmaster@hcoop.net" \
209 real-$USER
210 fi
211 chown $USER:nogroup $MAILPATH/cur $MAILPATH/new $MAILPATH/tmp
212
213 # Set up shared SpamAssassin folder
214 if test -f $HOMEPATH/Maildir/shared-maildirs; then
215 # Deal with case where user rsync'd their Maildir from fyodor
216 pattern='^SpamAssassin /home/spamd'
217 file=$HOMEPATH/Maildir/shared-maildirs
218 if grep $pattern $file; then
219 sed -i -r -e \
220 's!^(SpamAssassin )/home/spamd!\1/var/local/lib/spamd!1' \
221 $file
222 fi
223 else
224 maildirmake --add SpamAssassin=/var/local/lib/spamd/Maildir \
225 $HOMEPATH/Maildir
226 fi
227
228 # Create database tablespaces
229 sudo /afs/hcoop.net/common/etc/scripts/create-user-database $USER
230
231 #
232 # Mount points for backup volumes
233 #
234
235 mkdir -p `dirname /afs/hcoop.net/.old/user/$PATHBITS`
236 mkdir -p `dirname /afs/hcoop.net/.old/mail/$PATHBITS`
237 fs ls /afs/hcoop.net/.old/user/$PATHBITS || \
238 fs mkm /afs/hcoop.net/.old/user/$PATHBITS user.$USER.backup
239 fs ls /afs/hcoop.net/.old/mail/$PATHBITS || \
240 fs mkm /afs/hcoop.net/.old/mail/$PATHBITS mail.$USER.backup
241 vos release old
242
243 # technically this might not be necessary, but for good measure...
244 vos syncserv fritz
245 vos syncvldb fritz
246
247 # refresh volume location cache (takes ~2hrs otherwise)
248 execute_on_all_machines fs checkvolumes
249
250 #
251 # Non-AFS files and directories
252 #
253
254 # Make per-user apache DAV lock directory -- the directory must be
255 # both user and group-writable, which is silly.
256 mire_and_deleuze sudo mkdir -p /var/lock/apache2/dav/$USER
257 mire_and_deleuze sudo chown $USER:www-data /var/lock/apache2/dav/$USER
258 mire_and_deleuze sudo chmod ug=rwx,o= /var/lock/apache2/dav/$USER
259
260 #
261 # Domtool integration
262 #
263
264 execute_on_deleuze domtool-adduser $USER
265
266 #
267 # Subscribe user to our mailing lists.
268 #
269 echo $USER@hcoop.net | execute_on_deleuze sudo -u list \
270 /var/lib/mailman/bin/add_members -r - hcoop-announce