create-user: rsync cgi-keytabs to mire, create ~/public_html
[hcoop/scripts.git] / create-user
1 #!/bin/bash -ex
2
3 # MUST be executed:
4 # - on deleuze
5 # - as a user with an /etc/sudoers line
6 # - member of wheel unix group
7 # - while holding tokens for a user who is:
8 # - a member of system:administrator
9 # - listed in 'bos listusers deleuze'
10
11 USER=$1
12
13 if test -z "$USER"; then
14 echo "Invoke as create-user <USERNAME>"
15 exit 1
16 fi
17
18
19 #
20 # Kerberos principals
21 # (creat kerberos principals: fred, fred/cgi, fred/mailfilter)
22 #
23
24 # We use -randkey for user's main principal as well, to make sure that
25 # the creation process does not continue without having a main
26 # principal. (But you who want to set password for a user, don't
27 # worry - we'll invoke cpw later, so that it has the same effect
28 # as setting password right now - while it is more error tolerant).
29
30 sudo kadmin.local -p root/admin -q "ank -policy user -randkey $USER@HCOOP.NET"
31 sudo kadmin.local -p root/admin -q "ank -policy mailfilter -randkey $USER/mailfilter@HCOOP.NET"
32 sudo kadmin.local -p root/admin -q "ank -policy cgi -randkey $USER/cgi@HCOOP.NET"
33
34
35 #
36 # Create AFS users corresponding to krb5 principals.
37 # (fred/cgi principal == fred.cgi AFS user)
38 #
39
40 pts cu $USER || true
41 ID=`pts examine $USER | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
42 pts cu $USER.mailfilter $ID_MF || true
43 ID_MF=`pts examine $USER.mailfilter | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
44 pts cu $USER.cgi || true
45 ID_CGI=`pts examine $USER.cgi | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
46
47
48 #
49 # Construct various paths for later perusal.
50 #
51
52 # (If it's not clear, for user fred, PATHBITS = f/fr/fred)
53 PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
54 HOMEPATH=/afs/hcoop.net/user/$PATHBITS
55 MAILPATH=/afs/hcoop.net/common/email/$PATHBITS
56 DBPATH=/afs/hcoop.net/common/databases/$PATHBITS
57 PGDIR=$DBPATH/postgres
58 MYSQLDIR=$DBPATH/mysql
59
60
61 #
62 # Create LDAP entries. (With the whole libnss-ptdb, I kind of
63 # lost the idea of what I want to do with LDAP, but we'll
64 # see with time how well it integrates...)
65 # The ID returned from AFS is important here, we want to make
66 # sure those IDs match.
67 #
68
69 # USER entry
70 echo "
71 dn: uid=$USER,ou=People,dc=hcoop,dc=net
72 objectClass: top
73 objectClass: person
74 objectClass: posixAccount
75 cn: $USER
76 uid: $USER
77 gidNumber: $ID
78 homeDirectory: $HOMEPATH
79 sn: $USER
80 host: abulafia
81 host: mire
82
83 dn: cn=$USER,ou=Group,dc=hcoop,dc=net
84 objectClass: top
85 objectClass: posixGroup
86 cn: $USER
87 gidNumber: $ID
88 memberUid: $USER
89 " | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
90
91 # USER.mailfilter entry
92 echo "
93 dn: uid=$USER.mailfilter,ou=People,dc=hcoop,dc=net
94 objectClass: top
95 objectClass: person
96 objectClass: posixAccount
97 cn: $USER.mailfilter
98 uid: $USER.mailfilter
99 gidNumber: $ID_MF
100 homeDirectory: $HOMEPATH
101 sn: $USER.mailfilter
102
103 dn: cn=$USER.mailfilter,ou=Group,dc=hcoop,dc=net
104 objectClass: top
105 objectClass: posixGroup
106 cn: $USER.mailfilter
107 gidNumber: $ID_MF
108 memberUid: $USER.mailfilter
109 " | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
110
111 # USER.cgi entry
112 echo "
113 dn: uid=$USER.cgi,ou=People,dc=hcoop,dc=net
114 objectClass: top
115 objectClass: person
116 objectClass: posixAccount
117 cn: $USER.cgi
118 uid: $USER.cgi
119 gidNumber: $ID_CGI
120 homeDirectory: $HOMEPATH
121 sn: $USER.cgi
122
123 dn: cn=$USER.cgi,ou=Group,dc=hcoop,dc=net
124 objectClass: top
125 objectClass: posixGroup
126 cn: $USER.cgi
127 gidNumber: $ID_CGI
128 memberUid: $USER.cgi
129 " | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
130
131
132 #
133 # Export .mailfilter and .cgi keys to a keytab file
134 #
135
136 # create a mailfilter keytab (used by /etc/exim4/get-token)
137 sudo kadmin.local -p root/admin -q "ktadd -k /etc/keytabs/mailfilter/$USER $USER/mailfilter@HCOOP.NET"
138
139 # create a cgi keytab
140 sudo kadmin.local -p root/admin -q "ktadd -k /etc/keytabs/cgi/$USER $USER/cgi@HCOOP.NET"
141
142 # Properly chown/mod keytab files (www-data must own the cgi keytab)
143 sudo chown www-data:wheel /etc/keytabs/cgi/$USER
144 sudo chown $USER:wheel /etc/keytabs/mailfilter/$USER
145 sudo chmod 440 /etc/keytabs/cgi/$USER /etc/keytabs/mailfilter/$USER
146
147 # rsync keytabs to mire
148 rsync -e ssh -a /etc/keytabs/cgi/$USER mire.hcoop.net:/etc/keytabs/cgi/$USER
149
150 #
151 # Create/mount/set-perms on user's volumes (home, mail, databases, logs)
152 #
153
154 # HOME VOLUME
155 vos examine user.$USER 2>/dev/null || \
156 vos create deleuze.hcoop.net /vicepa user.$USER -maxquota 400000
157 mkdir -p `dirname $HOMEPATH`
158 fs ls $HOMEPATH || fs mkm $HOMEPATH user.$USER
159 chown $USER $HOMEPATH
160 fs sa $HOMEPATH $USER all
161 fs sa $HOMEPATH system:anyuser rl
162
163 # Apache logs
164 mkdir -p $HOMEPATH/logs/apache
165 fs sa $HOMEPATH/logs/apache $USER.cgi rlwidk
166
167 # public_html
168 mkdir -p $HOMEPATH/public_html
169 fs sa $HOMEPATH/public_html system:anyuser rl
170
171 # MAIL VOLUME
172 vos examine mail.$USER 2>/dev/null || \
173 vos create deleuze.hcoop.net /vicepa mail.$USER -maxquota 400000
174 mkdir -p `dirname $MAILPATH`
175 fs ls $MAILPATH || fs mkm $MAILPATH mail.$USER
176 fs ls $HOMEPATH/Maildir || fs mkm $HOMEPATH/Maildir mail.$USER
177 fs sa $MAILPATH $USER all
178 fs sa $MAILPATH $USER.mailfilter all
179
180 # DATABASE VOLUME
181 if ! vos examine db.$USER >/dev/null 2>/dev/null; then
182 mkdir -p `dirname /afs/.hcoop.net/common/.databases/$PATHBITS`
183 vos create -server afs -partition a -name db.$USER -maxquota 400000
184 fs mkmount -dir /afs/.hcoop.net/common/.databases/$PATHBITS -vol db.$USER -rw
185 vos release common.databases
186 fs sa -dir $DBPATH -acl system:postgres l
187 fs sa -dir $DBPATH -acl system:mysql l
188 fs sa -dir $DBPATH -acl system:backup rl
189 fi
190
191 # Create postgres user and tablespace placeholder within volume
192 if ! [ -d $PGDIR ]; then
193 mkdir -p $PGDIR
194 chown postgres:postgres $PGDIR
195 fs sa -dir $PGDIR -acl system:postgres write
196
197 sudo -u postgres psql -c "CREATE TABLESPACE user_$USER OWNER postgres LOCATION '$PGDIR'" template1
198 fi
199
200 # Create mysql user and databases placeholder within volume
201 mkdir -p $MYSQLDIR
202 chown mysql:mysql $MYSQLDIR
203 fs sa -dir $MYSQLDIR -acl system:mysql write
204
205
206 #
207 # Mount points for backup volumes
208 #
209
210 mkdir -p `dirname /afs/hcoop.net/old/user/$PATHBITS`
211 mkdir -p `dirname /afs/hcoop.net/old/mail/$PATHBITS`
212 fs ls /afs/hcoop.net/old/user/$PATHBITS || \
213 fs mkm /afs/hcoop.net/old/user/$PATHBITS user.$USER.backup
214 fs ls /afs/hcoop.net/old/mail/$PATHBITS || \
215 fs mkm /afs/hcoop.net/old/mail/$PATHBITS mail.$USER.backup
216
217 vos syncserv deleuze
218 vos syncvldb deleuze
219 fs checkvolumes
220
221 #
222 # Finally, set password for main user's principal
223 # Aborting this operation is harmless. Just re-invoke cpw.
224 #
225 # kadmin.local doesn't report errors properly, so we have to
226 # check manually
227 #
228 sudo rm -f /tmp/kadmin.out
229 sudo kadmin.local -p root/admin -q "cpw $USER@HCOOP.NET" \
230 2>&1 | tee /tmp/kadmin.out
231 cat /tmp/kadmin.out | grep 'Password for .* changed'
232 sudo rm -f /tmp/kadmin.out
233