create-user: comment-out unnecessary and trouble-causing shared-folders 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'
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
5debfe01 205
206# This does not yet seem to be needed, and it triggers an AFS issue,
207# so I've commented it out --mwolson.
208#
209# NOTIFY=no
210# for dir in $HOMEPATH/Maildir/shared-folders/SpamAssassin/*; do
211# if ! test -d $dir; then
212# NOTIFY=yes
213# else
214# dest=/var/local/lib/spamd/Maildir/.$(basename $dir)
215# if test "$(readlink $dir/shared)" != "$dest"; then
216# ln -sf $dest $dir/shared
217# fi
218# fi
219# done
220# if test $NOTIFY = yes; then
221# # This is probably going overboard, but oh well
222# echo "$USER needs assistance on their shared spam dir" | \
223# mail -s "[create-user] $USER needs assistance" \
224# -e -a "From: admins@deleuze.hcoop.net" mwolson_admin
225# fi
226
64a96d98 227else
228 maildirmake --add SpamAssassin=/var/local/lib/spamd/Maildir \
229 $HOMEPATH/Maildir
230fi
231
d2462e94 232# DATABASE VOLUME
233if ! vos examine db.$USER >/dev/null 2>/dev/null; then
234 mkdir -p `dirname /afs/.hcoop.net/common/.databases/$PATHBITS`
235 vos create -server afs -partition a -name db.$USER -maxquota 400000
236 fs mkmount -dir /afs/.hcoop.net/common/.databases/$PATHBITS -vol db.$USER -rw
237 vos release common.databases
238 fs sa -dir $DBPATH -acl system:postgres l
239 fs sa -dir $DBPATH -acl system:mysql l
240 fs sa -dir $DBPATH -acl system:backup rl
241fi
242
243# Create postgres user and tablespace placeholder within volume
e576ecef 244if ! test -d $PGDIR; then
d2462e94 245 mkdir -p $PGDIR
246 chown postgres:postgres $PGDIR
247 fs sa -dir $PGDIR -acl system:postgres write
248
249 sudo -u postgres psql -c "CREATE TABLESPACE user_$USER OWNER postgres LOCATION '$PGDIR'" template1
250fi
251
252# Create mysql user and databases placeholder within volume
253mkdir -p $MYSQLDIR
254chown mysql:mysql $MYSQLDIR
255fs sa -dir $MYSQLDIR -acl system:mysql write
256
257
258#
259# Mount points for backup volumes
260#
261
cd0db70e 262mkdir -p `dirname /afs/hcoop.net/.old/user/$PATHBITS`
263mkdir -p `dirname /afs/hcoop.net/.old/mail/$PATHBITS`
264fs ls /afs/hcoop.net/.old/user/$PATHBITS || \
265 fs mkm /afs/hcoop.net/.old/user/$PATHBITS user.$USER.backup
266fs ls /afs/hcoop.net/.old/mail/$PATHBITS || \
267 fs mkm /afs/hcoop.net/.old/mail/$PATHBITS mail.$USER.backup
268vos release old
d2462e94 269
7fe272af 270# technically this might not be necessary, but for good measure...
d2462e94 271vos syncserv deleuze
272vos syncvldb deleuze
7fe272af 273
274# refresh volume location cache (takes ~2hrs otherwise)
7e1859cd 275mire_and_deleuze fs checkvolumes
2c556c0c 276
277#
7e1859cd 278# Non-AFS files and directories
2c556c0c 279#
280
da0bc057 281# Make per-user apache DAV lock directory -- the directory must be
282# both user and group-writable, which is silly.
7e1859cd 283mire_and_deleuze sudo mkdir -p /var/lock/apache2/dav/$USER
284mire_and_deleuze sudo chown $USER:www-data /var/lock/apache2/dav/$USER
285mire_and_deleuze sudo chmod ug=rwx,o= /var/lock/apache2/dav/$USER
db51e5b7 286
287#
288# Domtool integration
289#
290
291domtool-adduser $USER