New stuff.
[clinton/scripts.git] / create-user
CommitLineData
d2462e94 1#!/bin/bash -ex
2
3# MUST be executed:
4# - on deleuze
5# - as a user with an /etc/sudoers line
64f69c08 6# - member of "wheel" unix group on deleuze
78725790 7# - while holding tickets for a user who can 'ssh -K' to mire
64f69c08 8# - and is a member of "wheel" on mire
d2462e94 9# - while holding tokens for a user who is:
10# - a member of system:administrator
11# - listed in 'bos listusers deleuze'
241a0263 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.)
d2462e94 27
28USER=$1
29
cd0db70e 30export PATH=$PATH:/afs/hcoop.net/common/bin/
31
d2462e94 32if test -z "$USER"; then
33 echo "Invoke as create-user <USERNAME>"
34 exit 1
35fi
36
7e1859cd 37#
38# Helper functions
39#
40
41# Run a command on both mire and deleuze; assumes that no escaping is
42# needed.
43function mire_and_deleuze() {
44 $*
45 ssh mire.hcoop.net $*
46}
d2462e94 47
48#
49# Kerberos principals
50# (creat kerberos principals: fred, fred/cgi, fred/mailfilter)
51#
52
53# We use -randkey for user's main principal as well, to make sure that
54# the creation process does not continue without having a main
55# principal. (But you who want to set password for a user, don't
56# worry - we'll invoke cpw later, so that it has the same effect
57# as setting password right now - while it is more error tolerant).
58
52e2a5b3 59sudo kadmin.local -p root/admin -q "ank -policy user -randkey +requires_preauth $USER@HCOOP.NET"
cd0db70e 60sudo kadmin.local -p root/admin -q "modprinc -maxlife 1day $USER@HCOOP.NET"
61sudo kadmin.local -p root/admin -q "ank -policy daemon -randkey +requires_preauth $USER/daemon@HCOOP.NET"
d2462e94 62
63#
64# Create AFS users corresponding to krb5 principals.
65# (fred/cgi principal == fred.cgi AFS user)
66#
67
68pts cu $USER || true
69ID=`pts examine $USER | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
cd0db70e 70pts cu $USER.daemon || true
71ID_DAEMON=`pts examine $USER.daemon | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
d2462e94 72
73
74#
75# Construct various paths for later perusal.
76#
77
78# (If it's not clear, for user fred, PATHBITS = f/fr/fred)
79PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
80HOMEPATH=/afs/hcoop.net/user/$PATHBITS
81MAILPATH=/afs/hcoop.net/common/email/$PATHBITS
64f69c08 82DBPATH=/afs/hcoop.net/common/.databases/$PATHBITS
d2462e94 83PGDIR=$DBPATH/postgres
84MYSQLDIR=$DBPATH/mysql
85
86
87#
88# Create LDAP entries. (With the whole libnss-ptdb, I kind of
89# lost the idea of what I want to do with LDAP, but we'll
90# see with time how well it integrates...)
91# The ID returned from AFS is important here, we want to make
92# sure those IDs match.
93#
94
95# USER entry
96echo "
97dn: uid=$USER,ou=People,dc=hcoop,dc=net
98objectClass: top
99objectClass: person
100objectClass: posixAccount
101cn: $USER
102uid: $USER
103gidNumber: $ID
d2462e94 104sn: $USER
105host: abulafia
106host: mire
107
108dn: cn=$USER,ou=Group,dc=hcoop,dc=net
109objectClass: top
110objectClass: posixGroup
111cn: $USER
112gidNumber: $ID
113memberUid: $USER
0963ebc5 114" | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
d2462e94 115
cd0db70e 116# USER.daemon entry
d2462e94 117echo "
cd0db70e 118dn: uid=$USER.daemon,ou=People,dc=hcoop,dc=net
d2462e94 119objectClass: top
120objectClass: person
121objectClass: posixAccount
cd0db70e 122cn: $USER.daemon
123uid: $USER.daemon
124gidNumber: $ID_DAEMON
125sn: $USER.daemon
d2462e94 126
cd0db70e 127dn: cn=$USER.daemon,ou=Group,dc=hcoop,dc=net
d2462e94 128objectClass: top
129objectClass: posixGroup
cd0db70e 130cn: $USER.daemon
131gidNumber: $ID_DAEMON
132memberUid: $USER.daemon
0963ebc5 133" | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
d2462e94 134
135
136#
137# Export .mailfilter and .cgi keys to a keytab file
138#
139
cd0db70e 140# create a daemon keytab (used by /etc/exim4/get-token)
141# *only* if it does not exist!
142test -e /etc/keytabs/user.daemon/$USER || \
143 sudo kadmin.local -p root/admin -q "ktadd -k /etc/keytabs/user.daemon/$USER $USER/daemon@HCOOP.NET"
d2462e94 144
cd0db70e 145# Properly chown/mod keytab files (must be $USER:www-data)
146sudo chown $USER:www-data /etc/keytabs/user.daemon/$USER
147sudo chmod 440 /etc/keytabs/user.daemon/$USER
d2462e94 148
7a7e31c9 149# rsync keytabs to mire
cd0db70e 150(cd /etc/keytabs
151 sudo tar clpf - user.daemon/$USER | \
152 ssh mire.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
d2462e94 153
154#
155# Create/mount/set-perms on user's volumes (home, mail, databases, logs)
156#
157
158# HOME VOLUME
0963ebc5 159vos examine user.$USER 2>/dev/null || \
160 vos create deleuze.hcoop.net /vicepa user.$USER -maxquota 400000
d2462e94 161mkdir -p `dirname $HOMEPATH`
3304c1fa 162fs ls $HOMEPATH || test -L $HOMEPATH || fs mkm $HOMEPATH user.$USER
b7b1cdff 163chown $USER:nogroup $HOMEPATH
d2462e94 164fs sa $HOMEPATH $USER all
cd0db70e 165fs sa $HOMEPATH system:anyuser l
d2462e94 166
167# Apache logs
655b4c82 168mkdir -p $HOMEPATH/.logs
169chown $USER:nogroup $HOMEPATH/.logs
170mkdir -p $HOMEPATH/.logs/apache
171chown $USER:nogroup $HOMEPATH/.logs/apache
172fs sa $HOMEPATH/.logs/apache $USER.daemon rlwidk
173mkdir -p $HOMEPATH/.logs/mail
174fs sa $HOMEPATH/.logs/mail $USER.daemon rlwidk
175chown $USER:nogroup $HOMEPATH/.logs/mail
d2462e94 176
7a7e31c9 177# public_html
42d22a10 178test -e $HOMEPATH/public_html || \
179 (mkdir -p $HOMEPATH/public_html; \
180 chown $USER:nogroup $HOMEPATH/public_html; \
181 fs sa $HOMEPATH/public_html system:anyuser none; \
182 fs sa $HOMEPATH/public_html $USER.daemon rl)
b7b1cdff 183
184# .procmail.d
185mkdir -p $HOMEPATH/.procmail.d
186chown $USER:nogroup $HOMEPATH/.procmail.d
187fs sa $HOMEPATH/.procmail.d system:anyuser rl
188
189# .public
cd0db70e 190mkdir -p $HOMEPATH/.public/
b7b1cdff 191chown $USER:nogroup $HOMEPATH/.public
192fs sa $HOMEPATH/.public system:anyuser rl
193
194# .domtool
195mkdir -p $HOMEPATH/.public/.domtool
196chown $USER:nogroup $HOMEPATH/.public/.domtool
cd0db70e 197test -e $HOMEPATH/.domtool || \
198 test -L $HOMEPATH/.domtool || \
74e97319 199 sudo -u $USER ln -s $HOMEPATH/.public/.domtool $HOMEPATH/.domtool
7a7e31c9 200
0008b2c9 201# Gitweb hosting
d890ae7b 202test -L /var/cache/git/$USER || \
203 sudo ln -s $HOMEPATH/.hcoop-git /var/cache/git/$USER
0008b2c9 204
d2462e94 205# MAIL VOLUME
0963ebc5 206vos examine mail.$USER 2>/dev/null || \
207 vos create deleuze.hcoop.net /vicepa mail.$USER -maxquota 400000
d2462e94 208mkdir -p `dirname $MAILPATH`
ef64fd5f 209fs ls $MAILPATH || fs mkm $MAILPATH mail.$USER
210fs ls $HOMEPATH/Maildir || fs mkm $HOMEPATH/Maildir mail.$USER
894794d2 211chown $USER:nogroup $MAILPATH
212chown $USER:nogroup $HOMEPATH/Maildir
cd0db70e 213fs sa $MAILPATH $USER all
ea773cea 214fs sa $MAILPATH $USER.daemon all
739364a2 215if test ! -e $MAILPATH/new; then
216 mkdir -p $MAILPATH/cur $MAILPATH/new $MAILPATH/tmp
217 echo -e "This email account is provided as a service for HCoop members." \
218 "\n\nTo learn how to use it, please visit the page" \
ffff3a6f 219 "\n<http://wiki.hcoop.net/MemberManual/Email> on our website."| \
739364a2 220 mail -s "Welcome to your HCoop email store" \
221 -e -a "From: postmaster@hcoop.net" \
222 real-$USER
223fi
3d7737ab 224chown $USER:nogroup $MAILPATH/cur $MAILPATH/new $MAILPATH/tmp
d2462e94 225
64a96d98 226# Set up shared SpamAssassin folder
227if test -f $HOMEPATH/Maildir/shared-maildirs; then
228 # Deal with case where user rsync'd their Maildir from fyodor
229 pattern='^SpamAssassin /home/spamd'
230 file=$HOMEPATH/Maildir/shared-maildirs
231 if grep $pattern $file; then
232 sed -i -r -e \
233 's!^(SpamAssassin )/home/spamd!\1/var/local/lib/spamd!1' \
234 $file
235 fi
64a96d98 236else
237 maildirmake --add SpamAssassin=/var/local/lib/spamd/Maildir \
238 $HOMEPATH/Maildir
239fi
240
d2462e94 241# DATABASE VOLUME
242if ! vos examine db.$USER >/dev/null 2>/dev/null; then
243 mkdir -p `dirname /afs/.hcoop.net/common/.databases/$PATHBITS`
244 vos create -server afs -partition a -name db.$USER -maxquota 400000
245 fs mkmount -dir /afs/.hcoop.net/common/.databases/$PATHBITS -vol db.$USER -rw
d2462e94 246 fs sa -dir $DBPATH -acl system:postgres l
247 fs sa -dir $DBPATH -acl system:mysql l
248 fs sa -dir $DBPATH -acl system:backup rl
9c29302d 249 fs sa -dir $DBPATH -acl $USER rl
d2462e94 250fi
251
252# Create postgres user and tablespace placeholder within volume
e576ecef 253if ! test -d $PGDIR; then
d2462e94 254 mkdir -p $PGDIR
255 chown postgres:postgres $PGDIR
256 fs sa -dir $PGDIR -acl system:postgres write
9c29302d 257 fs sa -dir $PGDIR -acl $USER none
d2462e94 258
259 sudo -u postgres psql -c "CREATE TABLESPACE user_$USER OWNER postgres LOCATION '$PGDIR'" template1
260fi
261
262# Create mysql user and databases placeholder within volume
263mkdir -p $MYSQLDIR
264chown mysql:mysql $MYSQLDIR
265fs sa -dir $MYSQLDIR -acl system:mysql write
9c29302d 266fs sa -dir $MYSQLDIR -acl $USER none
d2462e94 267
7fff2fea 268vos release common.databases
d2462e94 269
270#
271# Mount points for backup volumes
272#
273
cd0db70e 274mkdir -p `dirname /afs/hcoop.net/.old/user/$PATHBITS`
275mkdir -p `dirname /afs/hcoop.net/.old/mail/$PATHBITS`
276fs ls /afs/hcoop.net/.old/user/$PATHBITS || \
277 fs mkm /afs/hcoop.net/.old/user/$PATHBITS user.$USER.backup
278fs ls /afs/hcoop.net/.old/mail/$PATHBITS || \
279 fs mkm /afs/hcoop.net/.old/mail/$PATHBITS mail.$USER.backup
280vos release old
d2462e94 281
7fe272af 282# technically this might not be necessary, but for good measure...
d2462e94 283vos syncserv deleuze
284vos syncvldb deleuze
7fe272af 285
286# refresh volume location cache (takes ~2hrs otherwise)
7e1859cd 287mire_and_deleuze fs checkvolumes
2c556c0c 288
289#
7e1859cd 290# Non-AFS files and directories
2c556c0c 291#
292
da0bc057 293# Make per-user apache DAV lock directory -- the directory must be
294# both user and group-writable, which is silly.
7e1859cd 295mire_and_deleuze sudo mkdir -p /var/lock/apache2/dav/$USER
296mire_and_deleuze sudo chown $USER:www-data /var/lock/apache2/dav/$USER
297mire_and_deleuze sudo chmod ug=rwx,o= /var/lock/apache2/dav/$USER
db51e5b7 298
299#
300# Domtool integration
301#
302
303domtool-adduser $USER
2a2fcc8e 304
305#
306# Subscribe user to our mailing lists.
307#
308echo $USER@hcoop.net | sudo -u list \
309 /var/lib/mailman/bin/add_members -r - hcoop-announce