Check in create-user changes from megacz
[hcoop/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
22
23#
24# Kerberos principals
25# (creat kerberos principals: fred, fred/cgi, fred/mailfilter)
26#
27
28# We use -randkey for user's main principal as well, to make sure that
29# the creation process does not continue without having a main
30# principal. (But you who want to set password for a user, don't
31# worry - we'll invoke cpw later, so that it has the same effect
32# as setting password right now - while it is more error tolerant).
33
52e2a5b3 34sudo kadmin.local -p root/admin -q "ank -policy user -randkey +requires_preauth $USER@HCOOP.NET"
cd0db70e 35sudo kadmin.local -p root/admin -q "modprinc -maxlife 1day $USER@HCOOP.NET"
36sudo kadmin.local -p root/admin -q "ank -policy daemon -randkey +requires_preauth $USER/daemon@HCOOP.NET"
d2462e94 37
38#
39# Create AFS users corresponding to krb5 principals.
40# (fred/cgi principal == fred.cgi AFS user)
41#
42
43pts cu $USER || true
44ID=`pts examine $USER | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
cd0db70e 45pts cu $USER.daemon || true
46ID_DAEMON=`pts examine $USER.daemon | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
d2462e94 47
48
49#
50# Construct various paths for later perusal.
51#
52
53# (If it's not clear, for user fred, PATHBITS = f/fr/fred)
54PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
55HOMEPATH=/afs/hcoop.net/user/$PATHBITS
56MAILPATH=/afs/hcoop.net/common/email/$PATHBITS
64f69c08 57DBPATH=/afs/hcoop.net/common/.databases/$PATHBITS
d2462e94 58PGDIR=$DBPATH/postgres
59MYSQLDIR=$DBPATH/mysql
60
61
62#
63# Create LDAP entries. (With the whole libnss-ptdb, I kind of
64# lost the idea of what I want to do with LDAP, but we'll
65# see with time how well it integrates...)
66# The ID returned from AFS is important here, we want to make
67# sure those IDs match.
68#
69
70# USER entry
71echo "
72dn: uid=$USER,ou=People,dc=hcoop,dc=net
73objectClass: top
74objectClass: person
75objectClass: posixAccount
76cn: $USER
77uid: $USER
78gidNumber: $ID
d2462e94 79sn: $USER
80host: abulafia
81host: mire
82
83dn: cn=$USER,ou=Group,dc=hcoop,dc=net
84objectClass: top
85objectClass: posixGroup
86cn: $USER
87gidNumber: $ID
88memberUid: $USER
0963ebc5 89" | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
d2462e94 90
cd0db70e 91# USER.daemon entry
d2462e94 92echo "
cd0db70e 93dn: uid=$USER.daemon,ou=People,dc=hcoop,dc=net
d2462e94 94objectClass: top
95objectClass: person
96objectClass: posixAccount
cd0db70e 97cn: $USER.daemon
98uid: $USER.daemon
99gidNumber: $ID_DAEMON
100sn: $USER.daemon
d2462e94 101
cd0db70e 102dn: cn=$USER.daemon,ou=Group,dc=hcoop,dc=net
d2462e94 103objectClass: top
104objectClass: posixGroup
cd0db70e 105cn: $USER.daemon
106gidNumber: $ID_DAEMON
107memberUid: $USER.daemon
0963ebc5 108" | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
d2462e94 109
110
111#
112# Export .mailfilter and .cgi keys to a keytab file
113#
114
cd0db70e 115# create a daemon keytab (used by /etc/exim4/get-token)
116# *only* if it does not exist!
117test -e /etc/keytabs/user.daemon/$USER || \
118 sudo kadmin.local -p root/admin -q "ktadd -k /etc/keytabs/user.daemon/$USER $USER/daemon@HCOOP.NET"
d2462e94 119
cd0db70e 120# Properly chown/mod keytab files (must be $USER:www-data)
121sudo chown $USER:www-data /etc/keytabs/user.daemon/$USER
122sudo chmod 440 /etc/keytabs/user.daemon/$USER
d2462e94 123
7a7e31c9 124# rsync keytabs to mire
cd0db70e 125(cd /etc/keytabs
126 sudo tar clpf - user.daemon/$USER | \
127 ssh mire.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
d2462e94 128
129#
130# Create/mount/set-perms on user's volumes (home, mail, databases, logs)
131#
132
133# HOME VOLUME
0963ebc5 134vos examine user.$USER 2>/dev/null || \
135 vos create deleuze.hcoop.net /vicepa user.$USER -maxquota 400000
d2462e94 136mkdir -p `dirname $HOMEPATH`
ef64fd5f 137fs ls $HOMEPATH || fs mkm $HOMEPATH user.$USER
d2462e94 138chown $USER $HOMEPATH
139fs sa $HOMEPATH $USER all
cd0db70e 140fs sa $HOMEPATH system:anyuser l
d2462e94 141
142# Apache logs
143mkdir -p $HOMEPATH/logs/apache
cd0db70e 144fsr sa $HOMEPATH/logs/apache $USER.daemon rlwidk
d2462e94 145
7a7e31c9 146# public_html
05033b52 147mkdir -p $HOMEPATH/public_html/
7a7e31c9 148fs sa $HOMEPATH/public_html system:anyuser rl
05033b52 149mkdir -p $HOMEPATH/.procmail.d/
150fs sa $HOMEPATH/.procmail.d/ system:anyuser rl
cd0db70e 151mkdir -p $HOMEPATH/.public/
152fs sa $HOMEPATH/.public/ system:anyuser rl
153test -e $HOMEPATH/.forward || \
154 test -L $HOMEPATH/.forward || \
155 ln -s $HOMEPATH/.public/.forward $HOMEPATH/.forward
156mkdir -p $HOMEPATH/.public/.domtool/
157test -e $HOMEPATH/.domtool || \
158 test -L $HOMEPATH/.domtool || \
159 ln -s $HOMEPATH/.public/.domtool $HOMEPATH/.domtool
7a7e31c9 160
d2462e94 161# MAIL VOLUME
0963ebc5 162vos examine mail.$USER 2>/dev/null || \
163 vos create deleuze.hcoop.net /vicepa mail.$USER -maxquota 400000
d2462e94 164mkdir -p `dirname $MAILPATH`
ef64fd5f 165fs ls $MAILPATH || fs mkm $MAILPATH mail.$USER
166fs ls $HOMEPATH/Maildir || fs mkm $HOMEPATH/Maildir mail.$USER
cd0db70e 167fs sa $MAILPATH $USER all
168fsr sa $MAILPATH $USER.daemon all
d2462e94 169
170# DATABASE VOLUME
171if ! vos examine db.$USER >/dev/null 2>/dev/null; then
172 mkdir -p `dirname /afs/.hcoop.net/common/.databases/$PATHBITS`
173 vos create -server afs -partition a -name db.$USER -maxquota 400000
174 fs mkmount -dir /afs/.hcoop.net/common/.databases/$PATHBITS -vol db.$USER -rw
175 vos release common.databases
176 fs sa -dir $DBPATH -acl system:postgres l
177 fs sa -dir $DBPATH -acl system:mysql l
178 fs sa -dir $DBPATH -acl system:backup rl
179fi
180
181# Create postgres user and tablespace placeholder within volume
182if ! [ -d $PGDIR ]; then
183 mkdir -p $PGDIR
184 chown postgres:postgres $PGDIR
185 fs sa -dir $PGDIR -acl system:postgres write
186
187 sudo -u postgres psql -c "CREATE TABLESPACE user_$USER OWNER postgres LOCATION '$PGDIR'" template1
188fi
189
190# Create mysql user and databases placeholder within volume
191mkdir -p $MYSQLDIR
192chown mysql:mysql $MYSQLDIR
193fs sa -dir $MYSQLDIR -acl system:mysql write
194
195
196#
197# Mount points for backup volumes
198#
199
cd0db70e 200mkdir -p `dirname /afs/hcoop.net/.old/user/$PATHBITS`
201mkdir -p `dirname /afs/hcoop.net/.old/mail/$PATHBITS`
202fs ls /afs/hcoop.net/.old/user/$PATHBITS || \
203 fs mkm /afs/hcoop.net/.old/user/$PATHBITS user.$USER.backup
204fs ls /afs/hcoop.net/.old/mail/$PATHBITS || \
205 fs mkm /afs/hcoop.net/.old/mail/$PATHBITS mail.$USER.backup
206vos release old
d2462e94 207
7fe272af 208# technically this might not be necessary, but for good measure...
d2462e94 209vos syncserv deleuze
210vos syncvldb deleuze
7fe272af 211
212# refresh volume location cache (takes ~2hrs otherwise)
d2462e94 213fs checkvolumes
7fe272af 214ssh mire.hcoop.net fs checkvolumes
d2462e94 215
e8f64a1b 216# Technically this is not idempotent. This is not *too* bad because
217# of the fact that in AFS non-system:administrators users can't change
218# the group/owner of a file anyways. However, users still might want
219# to know which other users created certain files (in, say, a dropbox
220# or something like that). FIMXE.
221chown -R $USER:nogroup $HOMEPATH
222chown -R $USER:nogroup $MAILPATH