3 # FEX CGI for user registration
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
9 use CGI::Carp qw(fatalsToBrowser);
10 use Fcntl qw(:flock :seek :mode);
12 $CGI::LIST_CONTEXT_WARN = 0;
13 $CGI::LIST_CONTEXT_WARN = 0;
16 our (@local_hosts,@local_domains,@local_rhosts,@local_rdomains);
17 our (@registration_hosts,@registration_domains);
18 our ($usage_conditions);
21 our ($mdomain,$logdir,$spooldir,$fra,$hostname,$sendmail,$admin,$bcc);
23 our $error = "F*EX user registration ERROR";
25 my $ra = $ENV{REMOTE_ADDR}||0;
30 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
31 die "$0: no $FEXLIB\n" unless -d $FEXLIB;
33 # load common code, local config: $HOME/lib/fex.ph
34 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
36 my $log = "$logdir/fur.log";
37 my $head = "$ENV{SERVER_NAME} F*EX user registration";
39 chdir $spooldir or die "$spooldir - $!\n";
41 my $user = my $id = my $verify = '';
45 unless (@local_domains or @local_rdomains) {
47 "No domains for registrations are defined.",
48 "Contact $ENV{SERVER_ADMIN} for details."
52 # look for CGI parameters
53 foreach my $v (param) {
54 my $vv = despace(param($v));
55 debuglog("Param: $v=\"$vv\"");
57 $v =~ /^user$/i ? $user = normalize_address($vv):
58 $v =~ /^exuser$/i ? $exuser = normalize_address($vv):
59 $v =~ /^demouser$/i ? $demouser = normalize_address($vv):
60 $v =~ /^verify$/i ? $verify = lc(checkchars('URL-parameter',$vv)):
61 $v =~ /^confirm$/i ? $confirm = checkchars('URL-parameter',$vv):
62 $v =~ /^domain$/i ? $domain = lc(checkchars('URL-parameter',$vv)):
67 if ($confirm =~ /^(\w+)$/i) {
70 http_die("illegal registration key");
72 open $confirm,"<.reg/$confirm" or http_die("no registration key $confirm");
73 $user = untaint(getline($confirm));
74 $id = getline($confirm);
76 # unlink ".reg/$confirm";
77 unless ($user and $id) {
78 http_die("no registration data for key $confirm");
80 unless (-f "$user/.auto") {
81 http_die("registration expired");
83 # if (-f "$user/@") { http_die("$user is already activated") }
84 open $user,'>',"$user/@" or http_die("open $user/@ - $!\n");
85 print {$user} $id,"\n";
86 close $user or http_die("close $user/@ - $!\n");
88 http_header("200 OK");
89 print html_header($head);
90 my $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/fup/" . b64("from=$user&id=$id");
92 '<h3>Your registration was successful. Your new F*EX account is:</h3>'
94 '<code><a href="$url">$url</a></code>'
96 '(bookmark this URL!)'
101 ' <tr><td>URL:<td><code><b>$ENV{PROTO}://$ENV{HTTP_HOST}/fup/</code></b></tr>'
102 ' <tr><td>Sender:<td><code><b>$user</code></b></tr>'
103 ' <tr><td>auth-ID:<td><code><b>$id</code></b></tr>'
107 furlog("confirm: account $user created");
112 unless ($user or $exuser or $demouser) {
113 http_header("200 OK");
114 print html_header($head);
116 '<form action="$ENV{SCRIPT_NAME}"'
118 ' accept-charset="UTF-8"'
119 ' enctype="multipart/form-data">'
122 if (@local_domains and @local_hosts and ipin($ra,@local_hosts)) {
124 if (grep(/\*/,@local_domains)) {
126 ' new user (may send to internal or external users):<br>'
127 ' <input type="text" name="user" size="80" value="$user">'
129 ' allowed domains are:'
132 foreach my $ld (@local_domains) {
137 if ($mdomain and not grep /^\Q$mdomain\E$/i,@local_domains) {
138 unshift @local_domains,$mdomain;
140 my @mydomains = map { "\t<option>$_</option>\n" } @local_domains;
142 ' new user (may send to internal or external users):<br>'
143 ' <input type="text" name="user" size="40" value="$user">\@<select name="domain" size="1">@mydomains</select>'
148 if (@local_rdomains and @local_rhosts and
149 (not @registration_hosts or ipin($ra,@registration_hosts))) {
150 print " <p>or<p>\n" if $reg;
153 ' new external user (may send only to internal users):<br>'
154 ' <input type="text" name="exuser" size="80">'
160 print " <p>or<p>\n" if $reg;
162 local $_ = sprintf "with %d MB quota and %d day%s account life time",
163 @demo,$demo[1]>1 ? 's' : '';
165 ' new demo user ($_):<br>'
166 ' <input type="text" name="demouser" size="80">'
174 ' you must enter your e-mail address and <input type="submit" value="submit">'
178 if (@local_rdomains) {
181 'internal domains are:'
184 foreach my $lrd (@local_rdomains) {
191 '<a href="http://fex.rus.uni-stuttgart.de/users.html">User types overview</a>'
196 "Registrations from your host ($ra) are not allowed.",
197 "Contact $ENV{SERVER_ADMIN} for details."
204 unless (@local_rdomains) {
205 http_die("no \@local_rdomains");
207 if (@registration_hosts and not ipin($ra,@registration_hosts)) {
209 "Registrations from your host ($ra) are not allowed.",
210 "Contact $ENV{SERVER_ADMIN} for details."
213 if ($exuser =~ /\@(.+)/) {
215 if (@registration_domains and
216 not grep /^\Q$exd\E$/i,@registration_domains) {
218 "Your domain <code>$exd</code> is not allowed for registration.",
219 "Contact $ENV{SERVER_ADMIN} for details."
223 html_error($error,"<code>$exuser</code> is not an email address");
226 } elsif ($demouser) {
229 unless (@local_domains) {
231 "No local domains for registration are defined.",
232 "Contact $ENV{SERVER_ADMIN} for details."
235 my $mydomains = join('|',@local_domains);
236 $mydomains =~ s/\./\\./g;
237 $mydomains =~ s/\*/.*/g;
238 $mydomains .= "|$mdomain" if $mdomain;
239 $user .= '@'.$domain if $domain and $user !~ /@/;
240 # $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
242 unless (@local_hosts and ipin($ra,@local_hosts)) {
244 "Registrations from your host ($ra) are not allowed.",
245 "Contact $ENV{SERVER_ADMIN} for details."
248 if ("@local_domains" ne "*" and $user !~ /\@($mydomains)$/i) {
250 "Illegal domain for username.",
251 "Contact $ENV{SERVER_ADMIN} for details."
255 html_error($error,"No user type found.");
258 unless (checkforbidden($user)) {
259 html_error($error,"<code>$user</code> is not allowed");
261 unless (checkaddress($user)) {
262 html_error($error,"<code>$user</code> is not a valid e-mail address");
264 $user = untaint($user);
269 "you are already registered".
270 " (<a href=\"/fup?from=$user&ID_forgotten=1\">I have lost my auth-ID</a>)"
275 mkdir $user,0770 or http_die("mkdir $user - $!\n");
280 # recipients e-mail address restrictions
281 $rf = "$exuser/\@ALLOWED_RECIPIENTS";
282 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
283 print {$rf} "\@LOCAL_RDOMAINS\n";
284 print {$rf} "# See also file \@ALLOWED_RHOSTS\n";
286 # recipients ip restrictions
287 $rf = "$exuser/\@ALLOWED_RHOSTS";
288 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
289 print {$rf} "\@LOCAL_RHOSTS\n";
291 if (open $user,'>',"$user/.auto") {
292 print {$user} "fur:external\n";
295 } elsif ($demouser) {
296 my $quota = "$demouser/\@QUOTA";
297 open $quota,'>',$quota or http_die("cannot write $quota - $!\n");
298 printf {$quota} "recipient:%d\n",$demo[0];
299 printf {$quota} "sender:%d\n",$demo[0];
301 if (open $user,'>',"$user/.auto") {
302 print {$user} "fur:demo\n";
305 open $demouser,'>',"$demouser/.demo" and close $demouser;
307 if (open $user,'>',"$user/.auto") {
308 print {$user} "fur:internal\n";
315 if ("@local_domains" eq "*") {
316 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
317 print {$id} $id,"\n";
318 close $id or http_die("close $user/@ - $!\n");
319 http_header("200 OK");
320 print html_header($head);
321 $uid = "from=$user&id=$id";
326 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup?$uid'
327 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup/$b64'
335 if ($verify eq 'no') {
336 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
337 print {$id} $id,"\n";
338 close $id or http_die("close $user/@ - $!\n");
339 http_header("200 OK",'Content-Type: text/plain');
340 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fup?from=$user&ID=$id\n";
341 furlog("direct: account $user created");
342 if ($bcc and open my $mail,"|$sendmail '$bcc' 2>>$log") {
346 'Subject: F*EX user registration'
348 '$user has been auto-registrated with verify=no'
352 furlog("ERROR: cannot run sendmail - $!\n");
358 mkdir '.reg',0770 or http_die("mkdir .reg - $!\n");
360 $reg = randstring(8);
361 open $reg,'>',".reg/$reg" or http_die("open .reg/$reg - $!\n");
362 print {$reg} $user,"\n",$id,"\n";
363 close $reg or http_die("close .reg/$reg - $!\n");
365 open my $mail,'|-',$sendmail,$user,$bcc
366 or http_die("cannot start sendmail - $!\n");
370 'Subject: F*EX user registration request'
372 'To activate your new F*EX account go to this URL:'
374 '$ENV{PROTO}://$ENV{HTTP_HOST}/fur?confirm=$reg'
377 if ($usage_conditions and open $usage_conditions,$usage_conditions) {
378 print {$mail} "The conditions of usage are:\n\n";
379 print {$mail} $_ while <$usage_conditions>;
380 close $usage_conditions;
382 close $mail or http_die("cannot send mail - $!\n");
384 http_header("200 OK");
385 print html_header($head);
386 print "confirmation e-mail has been sent to <code>$user</code>\n";
387 print "</body></html>\n";
388 furlog("confirmation request mailed to $user");
399 if (open $log,'>>',$log) {
401 seek $log,0,SEEK_END;
402 printf {$log} "%s [%s_%s] %s %s\n",
403 isodate(time),$$,$ENV{REQUESTCOUNT},$fra,$msg;
408 sub normalize_address {
411 $a = lc(normalize(despace($a)));
412 checkchars('address',$a);