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 and @local_rdomains) {
44 "No domains for registrations are defined.",
45 "Contact $ENV{SERVER_ADMIN} for details."
49 unless (@local_hosts and ipin($ENV{REMOTE_ADDR}||0,@local_hosts)) {
51 "Registrations from your host ($ENV{REMOTE_ADDR}) are not allowed.",
52 "Contact $ENV{SERVER_ADMIN} for details."
56 # look for CGI parameters
59 foreach my $v (keys %PARAM) {
60 my $vv = despace($PARAM{$v});
61 # debuglog("Param: $v=\"$vv\"");
63 $v =~ /^user$/i ? $user = normalize_address($vv):
64 $v =~ /^exuser$/i ? $exuser = normalize_address($vv):
65 $v =~ /^demouser$/i ? $demouser = normalize_address($vv):
66 $v =~ /^verify$/i ? $verify = lc(checkchars('URL-parameter',$vv)):
67 $v =~ /^confirm$/i ? $confirm = checkchars('URL-parameter',$vv):
68 $v =~ /^domain$/i ? $domain = lc(checkchars('URL-parameter',$vv)):
73 if ($confirm =~ /^(\w+)$/i) {
76 http_die("illegal registration key");
78 open $confirm,"<.reg/$confirm" or http_die("no registration key $confirm");
79 $user = untaint(getline($confirm));
80 $id = getline($confirm);
82 # unlink ".reg/$confirm";
83 unless ($user and $id) {
84 http_die("no registration data for key $confirm");
86 unless (-f "$user/.auto") {
87 http_die("registration expired");
89 # if (-f "$user/@") { http_die("$user is already activated") }
90 open $user,'>',"$user/@" or http_die("open $user/@ - $!\n");
91 print {$user} $id,"\n";
92 close $user or http_die("close $user/@ - $!\n");
94 http_header("200 OK");
95 print html_header($head);
96 my $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/fup/" . b64("from=$user&id=$id");
98 '<h3>Your registration was successful. Your new F*EX account is:</h3>'
100 '<code><a href="$url">$url</a></code>'
102 '(bookmark this URL!)'
107 ' <tr><td>URL:<td><code><b>$ENV{PROTO}://$ENV{HTTP_HOST}/fup/</code></b></tr>'
108 ' <tr><td>Sender:<td><code><b>$user</code></b></tr>'
109 ' <tr><td>auth-ID:<td><code><b>$id</code></b></tr>'
113 furlog("confirm: account $user created");
118 unless ($user or $exuser or $demouser) {
119 http_header("200 OK");
120 print html_header($head);
122 '<form action="$ENV{SCRIPT_NAME}"'
124 ' accept-charset="UTF-8"'
125 ' enctype="multipart/form-data">'
128 if (@local_domains and @local_hosts and ipin($ra,@local_hosts)) {
130 if (grep(/\*/,@local_domains)) {
132 ' new user (may send to internal or external users):<br>'
133 ' <input type="text" name="user" size="80" value="$user">'
135 ' allowed domains are:'
138 foreach my $ld (@local_domains) {
143 if ($mdomain and not grep /^\Q$mdomain\E$/i,@local_domains) {
144 unshift @local_domains,$mdomain;
146 my @mydomains = map { "\t<option>$_</option>\n" } @local_domains;
148 ' new user (may send to internal or external users):<br>'
149 ' <input type="text" name="user" size="40" value="$user">\@<select name="domain" size="1">@mydomains</select>'
154 if (@local_rdomains and @local_rhosts and
155 (not @registration_hosts or ipin($ra,@registration_hosts))) {
156 print " <p>or<p>\n" if $reg;
159 ' new external user (may send only to internal users):<br>'
160 ' <input type="text" name="exuser" size="80">'
166 print " <p>or<p>\n" if $reg;
168 local $_ = sprintf "with %d MB quota and %d day%s account life time",
169 @demo,$demo[1]>1 ? 's' : '';
171 ' new demo user ($_):<br>'
172 ' <input type="text" name="demouser" size="80">'
180 ' you must enter your e-mail address and <input type="submit" value="submit">'
184 if (@local_rdomains) {
187 'internal domains are:'
190 foreach my $lrd (@local_rdomains) {
197 '<a href="http://fex.rus.uni-stuttgart.de/users.html">User types overview</a>'
202 "Registrations from your host ($ra) are not allowed.",
203 "Contact $ENV{SERVER_ADMIN} for details."
210 unless (@local_rdomains) {
211 http_die("no \@local_rdomains");
213 if (@registration_hosts and not ipin($ra,@registration_hosts)) {
215 "Registrations from your host ($ra) are not allowed.",
216 "Contact $ENV{SERVER_ADMIN} for details."
219 if ($exuser =~ /\@(.+)/) {
221 if (@registration_domains and
222 not grep /^\Q$exd\E$/i,@registration_domains) {
224 "Your domain <code>$exd</code> is not allowed for registration.",
225 "Contact $ENV{SERVER_ADMIN} for details."
229 html_error($error,"<code>$exuser</code> is not an email address");
232 } elsif ($demouser) {
235 unless (@local_domains) {
237 "No local domains for registration are defined.",
238 "Contact $ENV{SERVER_ADMIN} for details."
241 my $mydomains = join('|',@local_domains);
242 $mydomains =~ s/\./\\./g;
243 $mydomains =~ s/\*/.*/g;
244 $mydomains .= "|$mdomain" if $mdomain;
245 $user .= '@'.$domain if $domain and $user !~ /@/;
246 # $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
248 unless (@local_hosts and ipin($ra,@local_hosts)) {
250 "Registrations from your host ($ra) are not allowed.",
251 "Contact $ENV{SERVER_ADMIN} for details."
254 if ("@local_domains" ne "*" and $user !~ /\@($mydomains)$/i) {
256 "Illegal domain for username.",
257 "Contact $ENV{SERVER_ADMIN} for details."
261 html_error($error,"No user type found.");
264 unless (checkforbidden($user)) {
265 html_error($error,"<code>$user</code> is not allowed");
267 unless (checkaddress($user)) {
268 html_error($error,"<code>$user</code> is not a valid e-mail address");
270 $user = untaint($user);
275 "you are already registered".
276 " (<a href=\"/fup?from=$user&ID_forgotten=1\">I have lost my auth-ID</a>)"
281 mkdir $user,0770 or http_die("mkdir $user - $!\n");
286 # recipients e-mail address restrictions
287 $rf = "$exuser/\@ALLOWED_RECIPIENTS";
288 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
289 print {$rf} "\@LOCAL_RDOMAINS\n";
290 print {$rf} "# See also file \@ALLOWED_RHOSTS\n";
292 # recipients ip restrictions
293 $rf = "$exuser/\@ALLOWED_RHOSTS";
294 open $rf,'>',$rf or http_die("cannot write $rf - $!\n");
295 print {$rf} "\@LOCAL_RHOSTS\n";
297 if (open $user,'>',"$user/.auto") {
298 print {$user} "fur:external\n";
301 } elsif ($demouser) {
302 my $quota = "$demouser/\@QUOTA";
303 open $quota,'>',$quota or http_die("cannot write $quota - $!\n");
304 printf {$quota} "recipient:%d\n",$demo[0];
305 printf {$quota} "sender:%d\n",$demo[0];
307 if (open $user,'>',"$user/.auto") {
308 print {$user} "fur:demo\n";
311 open $demouser,'>',"$demouser/.demo" and close $demouser;
313 if (open $user,'>',"$user/.auto") {
314 print {$user} "fur:internal\n";
321 if ("@local_domains" eq "*") {
322 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
323 print {$id} $id,"\n";
324 close $id or http_die("close $user/@ - $!\n");
325 http_header("200 OK");
326 print html_header($head);
327 $uid = "from=$user&id=$id";
332 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup?$uid'
333 '$ENV{PROTO}://$ENV{HTTP_HOST}/fup/$b64'
341 if ($verify eq 'no') {
342 open $id,'>',"$user/@" or http_die("open $user/@ - $!\n");
343 print {$id} $id,"\n";
344 close $id or http_die("close $user/@ - $!\n");
345 http_header("200 OK",'Content-Type: text/plain');
346 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fup?from=$user&ID=$id\n";
347 furlog("direct: account $user created");
348 if ($bcc and open my $mail,"|$sendmail '$bcc' 2>>$logdir[0]/$log") {
352 'Subject: F*EX user registration'
354 '$user has been auto-registrated with verify=no'
358 furlog("ERROR: cannot run sendmail - $!\n");
364 mkdir '.reg',0770 or http_die("mkdir .reg - $!\n");
366 $reg = randstring(8);
367 open $reg,'>',".reg/$reg" or http_die("open .reg/$reg - $!\n");
368 print {$reg} $user,"\n",$id,"\n";
369 close $reg or http_die("close .reg/$reg - $!\n");
371 open my $mail,'|-',$sendmail,$user,$bcc
372 or http_die("cannot start sendmail - $!\n");
376 'Subject: F*EX user registration request'
378 'To activate your new F*EX account go to this URL:'
380 '$ENV{PROTO}://$ENV{HTTP_HOST}/fur?confirm=$reg'
383 if ($usage_conditions and open $usage_conditions,$usage_conditions) {
384 print {$mail} "The conditions of usage are:\n\n";
385 print {$mail} $_ while <$usage_conditions>;
386 close $usage_conditions;
388 close $mail or http_die("cannot send mail - $!\n");
390 http_header("200 OK");
391 print html_header($head);
392 print "confirmation e-mail has been sent to <code>$user</code>\n";
393 print "</body></html>\n";
394 furlog("confirmation request mailed to $user");
404 $msg = sprintf "%s [%s_%s] %s %s\n",
405 isodate(time),$$,$ENV{REQUESTCOUNT},$fra,$msg;
410 sub normalize_address {
413 $a = lc(normalize(despace($a)));
414 checkchars('address',$a);