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 # look for CGI parameters
52 foreach my $v (keys %PARAM) {
53 my $vv = despace($PARAM{$v});
54 # debuglog("Param: $v=\"$vv\"");
56 $v =~ /^user$/i ? $user = normalize_address($vv):
57 $v =~ /^exuser$/i ? $exuser = normalize_address($vv):
58 $v =~ /^demouser$/i ? $demouser = normalize_address($vv):
59 $v =~ /^verify$/i ? $verify = lc(checkchars('URL-parameter',$vv)):
60 $v =~ /^confirm$/i ? $confirm = checkchars('URL-parameter',$vv):
61 $v =~ /^domain$/i ? $domain = lc(checkchars('URL-parameter',$vv)):
66 if ($confirm =~ /^(\w+)$/i) {
69 http_die("illegal registration key");
71 open $confirm,"<.reg/$confirm" or http_die("no registration key $confirm");
72 $user = untaint(getline($confirm));
73 $id = getline($confirm);
75 # unlink ".reg/$confirm";
76 unless ($user and $id) {
77 http_die("no registration data for key $confirm");
79 unless (-f "$user/.auto") {
80 http_die("registration expired");
82 # if (-f "$user/@") { http_die("$user is already activated") }
83 open $user,'>',"$user/@" or http_die("open $user/@ - $!\n");
84 print {$user} $id,"\n";
85 close $user or http_die("close $user/@ - $!\n");
87 http_header("200 OK");
88 print html_header($head);
89 my $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/fup/" . b64("from=$user&id=$id");
91 '<h3>Your registration was successful. Your new F*EX account is:</h3>'
93 '<code><a href="$url">$url</a></code>'
95 '(bookmark this URL!)'
100 ' <tr><td>URL:<td><code><b>$ENV{PROTO}://$ENV{HTTP_HOST}/fup/</code></b></tr>'
101 ' <tr><td>Sender:<td><code><b>$user</code></b></tr>'
102 ' <tr><td>auth-ID:<td><code><b>$id</code></b></tr>'
106 furlog("confirm: account $user created");
111 unless ($user or $exuser or $demouser) {
112 http_header("200 OK");
113 print html_header($head);
115 '<form action="$ENV{SCRIPT_NAME}"'
117 ' accept-charset="UTF-8"'
118 ' enctype="multipart/form-data">'
121 if (@local_domains and @local_hosts and ipin($ra,@local_hosts)) {
123 if (grep(/\*/,@local_domains)) {
125 ' new user (may send to internal or external users):<br>'
126 ' <input type="text" name="user" size="80" value="$user">'
128 ' allowed domains are:'
131 foreach my $ld (@local_domains) {
136 if ($mdomain and not grep /^\Q$mdomain\E$/i,@local_domains) {
137 unshift @local_domains,$mdomain;
139 my @mydomains = map { "\t<option>$_</option>\n" } @local_domains;
141 ' new user (may send to internal or external users):<br>'
142 ' <input type="text" name="user" size="40" value="$user">\@<select name="domain" size="1">@mydomains</select>'
147 if (@local_rdomains and @local_rhosts and
148 (not @registration_hosts or ipin($ra,@registration_hosts))) {
149 print " <p>or<p>\n" if $reg;
152 ' new external user (may send only to internal users):<br>'
153 ' <input type="text" name="exuser" size="80">'
159 print " <p>or<p>\n" if $reg;
161 local $_ = sprintf "with %d MB quota and %d day%s account life time",
162 @demo,$demo[1]>1 ? 's' : '';
164 ' new demo user ($_):<br>'
165 ' <input type="text" name="demouser" size="80">'
173 ' you must enter your e-mail address and <input type="submit" value="submit">'
177 if (@local_rdomains) {
180 'internal domains are:'
183 foreach my $lrd (@local_rdomains) {
190 '<a href="http://fex.rus.uni-stuttgart.de/users.html">User types overview</a>'
195 "Registrations from your host ($ra) are not allowed.",
196 "Contact $ENV{SERVER_ADMIN} for details."
203 unless (@local_rdomains) {
204 http_die("no \@local_rdomains");
206 if (@registration_hosts and not ipin($ra,@registration_hosts)) {
208 "Registrations from your host ($ra) are not allowed.",
209 "Contact $ENV{SERVER_ADMIN} for details."
212 if ($exuser =~ /\@(.+)/) {
214 if (@registration_domains and
215 not grep /^\Q$exd\E$/i,@registration_domains) {
217 "Your domain <code>$exd</code> is not allowed for registration.",
218 "Contact $ENV{SERVER_ADMIN} for details."
222 html_error($error,"<code>$exuser</code> is not an email address");
225 } elsif ($demouser) {
228 unless (@local_domains) {
230 "No local domains for registration are defined.",
231 "Contact $ENV{SERVER_ADMIN} for details."
234 my $mydomains = join('|',@local_domains);
235 $mydomains =~ s/\./\\./g;
236 $mydomains =~ s/\*/.*/g;
237 $mydomains .= "|$mdomain" if $mdomain;
238 $user .= '@'.$domain if $domain and $user !~ /@/;
239 # $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
241 unless (@local_hosts and ipin($ra,@local_hosts)) {
243 "Registrations from your host ($ra) are not allowed.",
244 "Contact $ENV{SERVER_ADMIN} for details."
247 if ("@local_domains" ne "*" and $user !~ /\@($mydomains)$/i) {
249 "Illegal domain for username.",
250 "Contact $ENV{SERVER_ADMIN} for details."
254 html_error($error,"No user type found.");
257 unless (checkforbidden($user)) {
258 html_error($error,"<code>$user</code> is not allowed");
260 unless (checkaddress($user)) {
261 html_error($error,"<code>$user</code> is not a valid e-mail address");
263 $user = untaint($user);
268 "you are already registered".
269 " (<a href=\"/fup?from=$user&ID_forgotten=1\">I have lost my auth-ID</a>)"
274 mkdir $user,0770 or http_die("mkdir $user - $!\n");
279 # recipients e-mail address restrictions
280 $rf = "$exuser/\@ALLOWED_RECIPIENTS";
281 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
282 print {$rf} "\@LOCAL_RDOMAINS\n";
283 print {$rf} "# See also file \@ALLOWED_RHOSTS\n";
285 # recipients ip restrictions
286 $rf = "$exuser/\@ALLOWED_RHOSTS";
287 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
288 print {$rf} "\@LOCAL_RHOSTS\n";
290 if (open $user,'>',"$user/.auto") {
291 print {$user} "fur:external\n";
294 } elsif ($demouser) {
295 my $quota = "$demouser/\@QUOTA";
296 open $quota,'>',$quota or http_die("cannot write $quota - $!\n");
297 printf {$quota} "recipient:%d\n",$demo[0];
298 printf {$quota} "sender:%d\n",$demo[0];
300 if (open $user,'>',"$user/.auto") {
301 print {$user} "fur:demo\n";
304 open $demouser,'>',"$demouser/.demo" and close $demouser;
306 if (open $user,'>',"$user/.auto") {
307 print {$user} "fur:internal\n";
314 if ("@local_domains" eq "*") {
315 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
316 print {$id} $id,"\n";
317 close $id or http_die("close $user/@ - $!\n");
318 http_header("200 OK");
319 print html_header($head);
320 $uid = "from=$user&id=$id";
325 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup?$uid'
326 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup/$b64'
334 if ($verify eq 'no') {
335 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
336 print {$id} $id,"\n";
337 close $id or http_die("close $user/@ - $!\n");
338 http_header("200 OK",'Content-Type: text/plain');
339 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fup?from=$user&ID=$id\n";
340 furlog("direct: account $user created");
341 if ($bcc and open my $mail,"|$sendmail '$bcc' 2>>$logdir[0]/$log") {
345 'Subject: F*EX user registration'
347 '$user has been auto-registrated with verify=no'
351 furlog("ERROR: cannot run sendmail - $!\n");
357 mkdir '.reg',0770 or http_die("mkdir .reg - $!\n");
359 $reg = randstring(8);
360 open $reg,'>',".reg/$reg" or http_die("open .reg/$reg - $!\n");
361 print {$reg} $user,"\n",$id,"\n";
362 close $reg or http_die("close .reg/$reg - $!\n");
364 open my $mail,'|-',$sendmail,$user,$bcc
365 or http_die("cannot start sendmail - $!\n");
369 'Subject: F*EX user registration request'
371 'To activate your new F*EX account go to this URL:'
373 '$ENV{PROTO}://$ENV{HTTP_HOST}/fur?confirm=$reg'
376 if ($usage_conditions and open $usage_conditions,$usage_conditions) {
377 print {$mail} "The conditions of usage are:\n\n";
378 print {$mail} $_ while <$usage_conditions>;
379 close $usage_conditions;
381 close $mail or http_die("cannot send mail - $!\n");
383 http_header("200 OK");
384 print html_header($head);
385 print "confirmation e-mail has been sent to <code>$user</code>\n";
386 print "</body></html>\n";
387 furlog("confirmation request mailed to $user");
397 $msg = sprintf "%s [%s_%s] %s %s\n",
398 isodate(time),$$,$ENV{REQUESTCOUNT},$fra,$msg;
403 sub normalize_address {
406 $a = lc(normalize(despace($a)));
407 checkchars('address',$a);