3 # FEX CGI for user registration
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
8 BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 }
10 use Fcntl qw(:flock :seek :mode);
13 our (@local_hosts,@local_domains,@local_rhosts,@local_rdomains);
14 our (@registration_hosts,@registration_domains);
15 our ($usage_conditions);
18 our ($mdomain,@logdir,$spooldir,$fra,$hostname,$sendmail,$admin,$bcc);
20 our $error = "F*EX user registration ERROR";
22 my $ra = $ENV{REMOTE_ADDR}||0;
27 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
28 die "$0: no $FEXLIB\n" unless -d $FEXLIB;
30 # load common code, local config: $HOME/lib/fex.ph
31 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
34 my $head = "$ENV{SERVER_NAME} F*EX user registration";
36 chdir $spooldir or die "$spooldir - $!\n";
38 my $user = my $id = my $verify = '';
42 unless (@local_domains or @local_rdomains) {
44 "No domains for registrations are defined.",
45 "Contact $ENV{SERVER_ADMIN} for details."
49 unless (@local_hosts and ipin($ra,@local_hosts) or
50 @local_rhosts and ipin($ra,@local_rhosts)) {
52 "Registrations from your host ($ra) are not allowed.",
53 "Contact $ENV{SERVER_ADMIN} for details."
57 # look for CGI parameters
60 foreach my $v (keys %PARAM) {
61 my $vv = despace($PARAM{$v});
62 # debuglog("Param: $v=\"$vv\"");
64 $v =~ /^user$/i ? $user = normalize_address($vv):
65 $v =~ /^exuser$/i ? $exuser = normalize_address($vv):
66 $v =~ /^demouser$/i ? $demouser = normalize_address($vv):
67 $v =~ /^verify$/i ? $verify = lc(checkchars('URL-parameter',$vv)):
68 $v =~ /^confirm$/i ? $confirm = checkchars('URL-parameter',$vv):
69 $v =~ /^domain$/i ? $domain = lc(checkchars('URL-parameter',$vv)):
74 if ($confirm =~ /^(\w+)$/i) {
77 http_die("illegal registration key");
79 open $confirm,"<.reg/$confirm" or http_die("no registration key $confirm");
80 $user = untaint(getline($confirm));
81 $id = getline($confirm);
83 # unlink ".reg/$confirm";
84 unless ($user and $id) {
85 http_die("no registration data for key $confirm");
87 unless (-f "$user/.auto") {
88 http_die("registration expired");
90 # if (-f "$user/@") { http_die("$user is already activated") }
91 open $user,'>',"$user/@" or http_die("open $user/@ - $!\n");
92 print {$user} $id,"\n";
93 close $user or http_die("close $user/@ - $!\n");
95 http_header("200 OK");
96 print html_header($head);
97 my $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/fup/" . b64("from=$user&id=$id");
99 '<h3>Your registration was successful. Your new F*EX account is:</h3>'
101 '<code><a href="$url">$url</a></code>'
103 '(bookmark this URL!)'
108 ' <tr><td>URL:<td><code><b>$ENV{PROTO}://$ENV{HTTP_HOST}/fup/</code></b></tr>'
109 ' <tr><td>Sender:<td><code><b>$user</code></b></tr>'
110 ' <tr><td>auth-ID:<td><code><b>$id</code></b></tr>'
114 furlog("confirm: account $user created");
119 unless ($user or $exuser or $demouser) {
120 http_header("200 OK");
121 print html_header($head);
123 '<form action="$ENV{SCRIPT_NAME}"'
125 ' accept-charset="UTF-8"'
126 ' enctype="multipart/form-data">'
129 if (@local_domains and @local_hosts and ipin($ra,@local_hosts)) {
131 if (grep(/\*/,@local_domains)) {
133 ' new user (may send to internal or external users):<br>'
134 ' <input type="text" name="user" size="80" value="$user">'
136 ' allowed domains are:'
139 foreach my $ld (@local_domains) {
144 if ($mdomain and not grep /^\Q$mdomain\E$/i,@local_domains) {
145 unshift @local_domains,$mdomain;
147 my @mydomains = map { "\t<option>$_</option>\n" } @local_domains;
149 ' new user (may send to internal or external users):<br>'
150 ' <input type="text" name="user" size="40" value="$user">\@<select name="domain" size="1">@mydomains</select>'
155 if (@local_rdomains and @local_rhosts and
156 (not @registration_hosts or ipin($ra,@registration_hosts))) {
157 print " <p>or<p>\n" if $reg;
160 ' new external user (may send only to internal users):<br>'
161 ' <input type="text" name="exuser" size="80">'
167 print " <p>or<p>\n" if $reg;
169 local $_ = sprintf "with %d MB quota and %d day%s account life time",
170 @demo,$demo[1]>1 ? 's' : '';
172 ' new demo user ($_):<br>'
173 ' <input type="text" name="demouser" size="80">'
181 ' you must enter your e-mail address and <input type="submit" value="submit">'
185 if (@local_rdomains) {
188 'internal domains are:'
191 foreach my $lrd (@local_rdomains) {
198 '<a href="http://fex.rus.uni-stuttgart.de/users.html">User types overview</a>'
203 "Registrations from your host ($ra) are not allowed.",
204 "Contact $ENV{SERVER_ADMIN} for details."
211 unless (@local_rdomains) {
212 http_die("no \@local_rdomains");
214 if (@registration_hosts and not ipin($ra,@registration_hosts)) {
216 "Registrations from your host ($ra) are not allowed.",
217 "Contact $ENV{SERVER_ADMIN} for details."
220 if ($exuser =~ /\@(.+)/) {
222 if (@registration_domains and
223 not grep /^\Q$exd\E$/i,@registration_domains) {
225 "Your domain <code>$exd</code> is not allowed for registration.",
226 "Contact $ENV{SERVER_ADMIN} for details."
230 html_error($error,"<code>$exuser</code> is not an email address");
233 } elsif ($demouser) {
236 unless (@local_domains) {
238 "No local domains for registration are defined.",
239 "Contact $ENV{SERVER_ADMIN} for details."
242 my $mydomains = join('|',@local_domains);
243 $mydomains =~ s/\./\\./g;
244 $mydomains =~ s/\*/.*/g;
245 $mydomains .= "|$mdomain" if $mdomain;
246 $user .= '@'.$domain if $domain and $user !~ /@/;
247 # $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
249 unless (@local_hosts and ipin($ra,@local_hosts)) {
251 "Registrations from your host ($ra) are not allowed.",
252 "Contact $ENV{SERVER_ADMIN} for details."
255 if ("@local_domains" ne "*" and $user !~ /\@($mydomains)$/i) {
257 "Illegal domain for username.",
258 "Contact $ENV{SERVER_ADMIN} for details."
262 html_error($error,"No user type found.");
265 unless (checkforbidden($user)) {
266 html_error($error,"<code>$user</code> is not allowed");
268 unless (checkaddress($user)) {
269 html_error($error,"<code>$user</code> is not a valid e-mail address");
271 $user = untaint($user);
276 "you are already registered".
277 " (<a href=\"/fup?from=$user&ID_forgotten=1\">I have lost my auth-ID</a>)"
282 mkdir $user,0770 or http_die("mkdir $user - $!\n");
287 # recipients e-mail address restrictions
288 $rf = "$exuser/\@ALLOWED_RECIPIENTS";
289 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
290 print {$rf} "\@LOCAL_RDOMAINS\n";
291 print {$rf} "# See also file \@ALLOWED_RHOSTS\n";
293 # recipients ip restrictions
294 $rf = "$exuser/\@ALLOWED_RHOSTS";
295 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
296 print {$rf} "\@LOCAL_RHOSTS\n";
298 if (open $user,'>',"$user/.auto") {
299 print {$user} "fur:external\n";
302 } elsif ($demouser) {
303 my $quota = "$demouser/\@QUOTA";
304 open $quota,'>',$quota or http_die("cannot write $quota - $!\n");
305 printf {$quota} "recipient:%d\n",$demo[0];
306 printf {$quota} "sender:%d\n",$demo[0];
308 if (open $user,'>',"$user/.auto") {
309 print {$user} "fur:demo\n";
312 open $demouser,'>',"$demouser/.demo" and close $demouser;
314 if (open $user,'>',"$user/.auto") {
315 print {$user} "fur:internal\n";
322 if ("@local_domains" eq "*") {
323 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
324 print {$id} $id,"\n";
325 close $id or http_die("close $user/@ - $!\n");
326 http_header("200 OK");
327 print html_header($head);
328 $uid = "from=$user&id=$id";
333 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup?$uid'
334 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup/$b64'
342 if ($verify eq 'no') {
343 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
344 print {$id} $id,"\n";
345 close $id or http_die("close $user/@ - $!\n");
346 http_header("200 OK",'Content-Type: text/plain');
347 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fup?from=$user&ID=$id\n";
348 furlog("direct: account $user created");
349 if ($bcc and open my $mail,"|$sendmail '$bcc' 2>>$logdir[0]/$log") {
353 'Subject: F*EX user registration'
355 '$user has been auto-registrated with verify=no'
359 furlog("ERROR: cannot run sendmail - $!\n");
365 mkdir '.reg',0770 or http_die("mkdir .reg - $!\n");
367 $reg = randstring(8);
368 open $reg,'>',".reg/$reg" or http_die("open .reg/$reg - $!\n");
369 print {$reg} $user,"\n",$id,"\n";
370 close $reg or http_die("close .reg/$reg - $!\n");
372 open my $mail,'|-',$sendmail,$user,$bcc
373 or http_die("cannot start sendmail - $!\n");
377 'Subject: F*EX user registration request'
379 'To activate your new F*EX account go to this URL:'
381 '$ENV{PROTO}://$ENV{HTTP_HOST}/fur?confirm=$reg'
384 if ($usage_conditions and open $usage_conditions,$usage_conditions) {
385 print {$mail} "The conditions of usage are:\n\n";
386 print {$mail} $_ while <$usage_conditions>;
387 close $usage_conditions;
389 close $mail or http_die("cannot send mail - $!\n");
391 http_header("200 OK");
392 print html_header($head);
393 print "confirmation e-mail has been sent to <code>$user</code>\n";
394 print "</body></html>\n";
395 furlog("confirmation request mailed to $user");
405 $msg = sprintf "%s [%s_%s] %s %s\n",
406 isodate(time),$$,$ENV{REQUESTCOUNT},$fra,$msg;
411 sub normalize_address {
414 $a = lc(normalize(despace($a)));
415 checkchars('address',$a);