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