87d56ecf8f8a97382eeca34f4479ea977f2cffe3
[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 on deleuze
7 # - while holding tickets for a user who can 'ssh -K' to mire
8 # - and is a member of "wheel" on mire
9 # - while holding tokens for a user who is:
10 # - a member of system:administrator
11 # - listed in 'bos listusers deleuze'
12
13 USER=$1
14
15 export PATH=$PATH:/afs/hcoop.net/common/bin/
16
17 if test -z "$USER"; then
18 echo "Invoke as create-user <USERNAME>"
19 exit 1
20 fi
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
34 sudo kadmin.local -p root/admin -q "ank -policy user -randkey +requires_preauth $USER@HCOOP.NET"
35 sudo kadmin.local -p root/admin -q "modprinc -maxlife 1day $USER@HCOOP.NET"
36 sudo kadmin.local -p root/admin -q "ank -policy daemon -randkey +requires_preauth $USER/daemon@HCOOP.NET"
37
38 #
39 # Create AFS users corresponding to krb5 principals.
40 # (fred/cgi principal == fred.cgi AFS user)
41 #
42
43 pts cu $USER || true
44 ID=`pts examine $USER | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
45 pts cu $USER.daemon || true
46 ID_DAEMON=`pts examine $USER.daemon | head -n1 | sed 's_.*, id: *__' | sed 's_,.*__'`
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)
54 PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
55 HOMEPATH=/afs/hcoop.net/user/$PATHBITS
56 MAILPATH=/afs/hcoop.net/common/email/$PATHBITS
57 DBPATH=/afs/hcoop.net/common/.databases/$PATHBITS
58 PGDIR=$DBPATH/postgres
59 MYSQLDIR=$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
71 echo "
72 dn: uid=$USER,ou=People,dc=hcoop,dc=net
73 objectClass: top
74 objectClass: person
75 objectClass: posixAccount
76 cn: $USER
77 uid: $USER
78 gidNumber: $ID
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.daemon entry
92 echo "
93 dn: uid=$USER.daemon,ou=People,dc=hcoop,dc=net
94 objectClass: top
95 objectClass: person
96 objectClass: posixAccount
97 cn: $USER.daemon
98 uid: $USER.daemon
99 gidNumber: $ID_DAEMON
100 sn: $USER.daemon
101
102 dn: cn=$USER.daemon,ou=Group,dc=hcoop,dc=net
103 objectClass: top
104 objectClass: posixGroup
105 cn: $USER.daemon
106 gidNumber: $ID_DAEMON
107 memberUid: $USER.daemon
108 " | sudo ldapadd -x -D cn=admin,dc=hcoop,dc=net -y /etc/ldap.secret || true
109
110
111 #
112 # Export .mailfilter and .cgi keys to a keytab file
113 #
114
115 # create a daemon keytab (used by /etc/exim4/get-token)
116 # *only* if it does not exist!
117 test -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"
119
120 # Properly chown/mod keytab files (must be $USER:www-data)
121 sudo chown $USER:www-data /etc/keytabs/user.daemon/$USER
122 sudo chmod 440 /etc/keytabs/user.daemon/$USER
123
124 # rsync keytabs to mire
125 (cd /etc/keytabs
126 sudo tar clpf - user.daemon/$USER | \
127 ssh mire.hcoop.net cd /etc/keytabs\; sudo tar xlpf -)
128
129 #
130 # Create/mount/set-perms on user's volumes (home, mail, databases, logs)
131 #
132
133 # HOME VOLUME
134 vos examine user.$USER 2>/dev/null || \
135 vos create deleuze.hcoop.net /vicepa user.$USER -maxquota 400000
136 mkdir -p `dirname $HOMEPATH`
137 fs ls $HOMEPATH || fs mkm $HOMEPATH user.$USER
138 chown $USER:nogroup $HOMEPATH
139 fs sa $HOMEPATH $USER all
140 fs sa $HOMEPATH system:anyuser l
141
142 # Apache logs
143 mkdir -p $HOMEPATH/logs/apache
144 chown $USER:nogroup $HOMEPATH/logs/apache
145 fs sa $HOMEPATH/logs/apache $USER.daemon rlwidk
146
147 # public_html
148 mkdir -p $HOMEPATH/public_html
149 chown $USER:nogroup $HOMEPATH/public_html
150 fs sa $HOMEPATH/public_html system:anyuser rl
151
152 # .procmail.d
153 mkdir -p $HOMEPATH/.procmail.d
154 chown $USER:nogroup $HOMEPATH/.procmail.d
155 fs sa $HOMEPATH/.procmail.d system:anyuser rl
156
157 # .public
158 mkdir -p $HOMEPATH/.public/
159 chown $USER:nogroup $HOMEPATH/.public
160 fs sa $HOMEPATH/.public system:anyuser rl
161
162 # .domtool
163 mkdir -p $HOMEPATH/.public/.domtool
164 chown $USER:nogroup $HOMEPATH/.public/.domtool
165 test -e $HOMEPATH/.domtool || \
166 test -L $HOMEPATH/.domtool || \
167 ln -s $HOMEPATH/.public/.domtool $HOMEPATH/.domtool
168
169 # MAIL VOLUME
170 vos examine mail.$USER 2>/dev/null || \
171 vos create deleuze.hcoop.net /vicepa mail.$USER -maxquota 400000
172 mkdir -p `dirname $MAILPATH`
173 chown $USER:nogroup $MAILPATH
174 fs ls $MAILPATH || fs mkm $MAILPATH mail.$USER
175 fs ls $HOMEPATH/Maildir || fs mkm $HOMEPATH/Maildir mail.$USER
176 fs sa $MAILPATH $USER all
177 fs sa $MAILPATH $USER.daemon all
178
179 # DATABASE VOLUME
180 if ! vos examine db.$USER >/dev/null 2>/dev/null; then
181 mkdir -p `dirname /afs/.hcoop.net/common/.databases/$PATHBITS`
182 vos create -server afs -partition a -name db.$USER -maxquota 400000
183 fs mkmount -dir /afs/.hcoop.net/common/.databases/$PATHBITS -vol db.$USER -rw
184 vos release common.databases
185 fs sa -dir $DBPATH -acl system:postgres l
186 fs sa -dir $DBPATH -acl system:mysql l
187 fs sa -dir $DBPATH -acl system:backup rl
188 fi
189
190 # Create postgres user and tablespace placeholder within volume
191 if ! [ -d $PGDIR ]; then
192 mkdir -p $PGDIR
193 chown postgres:postgres $PGDIR
194 fs sa -dir $PGDIR -acl system:postgres write
195
196 sudo -u postgres psql -c "CREATE TABLESPACE user_$USER OWNER postgres LOCATION '$PGDIR'" template1
197 fi
198
199 # Create mysql user and databases placeholder within volume
200 mkdir -p $MYSQLDIR
201 chown mysql:mysql $MYSQLDIR
202 fs sa -dir $MYSQLDIR -acl system:mysql write
203
204
205 #
206 # Mount points for backup volumes
207 #
208
209 mkdir -p `dirname /afs/hcoop.net/.old/user/$PATHBITS`
210 mkdir -p `dirname /afs/hcoop.net/.old/mail/$PATHBITS`
211 fs ls /afs/hcoop.net/.old/user/$PATHBITS || \
212 fs mkm /afs/hcoop.net/.old/user/$PATHBITS user.$USER.backup
213 fs ls /afs/hcoop.net/.old/mail/$PATHBITS || \
214 fs mkm /afs/hcoop.net/.old/mail/$PATHBITS mail.$USER.backup
215 vos release old
216
217 # technically this might not be necessary, but for good measure...
218 vos syncserv deleuze
219 vos syncvldb deleuze
220
221 # refresh volume location cache (takes ~2hrs otherwise)
222 fs checkvolumes
223 ssh mire.hcoop.net fs checkvolumes
224
225 #
226 # Files and directories on deleuze
227 #
228
229 # make per-user apache DAV lock directory
230 sudo mkdir -p /var/lock/apache2/dav/$USER
231 sudo chown $USER:nogroup /var/lock/apache2/dav/$USER
232 sudo chmod u=rwx,go= /var/lock/apache2/dav/$USER