tyop ... or something
[freeside.git] / fs_selfadmin / FS-MailAdminServer / cgi / mailadmin.cgi
1 #!/usr/bin/perl
2 ########################################################################
3 #                                                                      #
4 #    mailadmin.cgi                NCI2000                              #
5 #                                 Jeff Finucane <jeff@nci2000.net>     #
6 #                                 26 April 2001                        #
7 #                                                                      #
8 ########################################################################
9
10 use DBI;
11 use strict;
12 use CGI;
13 use FS::MailAdminClient qw(authenticate list_packages list_mailboxes delete_mailbox password_mailbox add_mailbox list_forwards list_pkg_forwards delete_forward add_forward);
14
15 my $sessionfile = '/usr/local/apache/htdocs/mailadmin/adminsess';   # session file
16 my $tmpdir = '/usr/local/apache/htdocs/mailadmin/tmp';          # Location to store temp files
17 my $cookiedomain = ".your.dom";      # domain if THIS server, should prepend with a '.'
18 my $cookieexpire = '+12h';              # expire the cookie session after this much idle time
19 my $sessexpire = 43200;                 # expire session after this long of no use (in seconds)
20
21 my $body = "<body bgcolor=dddddd>";
22
23 #### Should not have to change anything under this line ####
24 my $printmainpage = 1;
25 my $i = 0;
26 my $printheader = 1;
27 my $query = new CGI;
28 my $cgi = $query->url();
29 my $now = getdatetime();
30 my $current_package = 0;
31 my $current_account = 0;
32 my $current_domname = "";
33
34 # if they are trying to login we wont check the session yet
35 if ($query->param('login') eq '' && $query->param('action') ne 'login') {
36   checksession();
37   printheader();
38 }
39
40 if ($query->param('login') ne '') {
41
42    my $username = $query->param('username');
43    my $password = $query->param('password');
44
45    if (!checkuserpass($username, $password)) {
46       printheader();
47       error('not_admin');
48    }
49
50    my @alpha = ('A'..'Z', 'a'..'z', 0..9);
51    my $sessid = '';
52    for (my $i = 0; $i < 10; $i++) {
53        $sessid .= @alpha[rand(@alpha)];
54    }
55
56    my $cookie1 = $query->cookie(-name=>'username',
57                                 -value=>$username,
58                                 -expires=>$cookieexpire,
59                                 -domain=>$cookiedomain);
60
61    my $cookie2 = $query->cookie(-name=>'ma_sessionid',
62                                 -value=>$sessid,
63                                 -expires=>$cookieexpire,
64                                 -domain=>$cookiedomain);
65
66    my $now = time();
67    open(NEWSESS, ">>$sessionfile") || error('open');
68    print NEWSESS "$username $sessid $now 0 0\n";
69    close(NEWSESS);
70
71    print $query->header(-COOKIE=>[$cookie1, $cookie2]);
72  
73    $printmainpage = 1;
74
75 } elsif ($query->param('action') eq 'blankframe') {
76    
77   print "<html>$body</body></html>\n";
78    $printmainpage = 0;
79
80 } elsif ($query->param('action') eq 'list_packages') {
81
82   my $username = $query->cookie(-name=>'username');  # session checked
83   my $list = list_packages($username);
84   print "<html>$body\n";
85   print "<center><table border=0>\n";
86   print "<tr><td></td><td><p>Package Number</td><td><p>Description</td></tr>\n";
87   foreach my $package ( @{$list} ) {
88     print "<tr>";
89     print "<td></td><td><p>$package->{'pkgnum'}</td><td><p>$package->{'domain'}</td>\n";
90     print "<td></td><td><a href=\"$cgi\?action=select&package=$package->{'pkgnum'}&account=$package->{'account'}&domname=$package->{'domain'}\" target=\"rightmainframe\">select</td>\n";
91     print "</tr>";
92   }
93   print "</table>\n";
94   print "</body></html>\n";
95   $printmainpage=0;
96
97 } elsif ($query->param('action') eq 'list_mailboxes') {
98
99   my $username = $query->cookie(-name=>'username');  # session checked
100   select_package($username)  unless $current_package;
101   my $list = list_mailboxes($username, $current_package);
102   my $forwardlist = list_pkg_forwards($username, $current_package);
103   print "<html>$body\n";
104   print "<center><table border=0>\n";
105   print "<tr><td></td><td><p>Username</td><td><p>Password</td></tr>\n";
106   foreach my $account ( @{$list} ) {
107     print "<tr>";
108     print "<td></td><td><p>$account->{'username'}</td><td><p>$account->{'_password'}</td>\n";
109     print "<td></td><td><a href=\"$cgi\?action=change&account=$account->{'svcnum'}&mailbox=$account->{'username'}\" target=\"rightmainframe\">change</td>\n";
110     print "</tr>";
111
112 #    my $forwardlist = list_forwards($username, $account->{'svcnum'});
113 #    foreach my $forward ( @{$forwardlist} ) {
114 #      my $label = qq!=> ! . $forward->{'dest'};
115 #      print "<tr><td></td><td></td><td><p>$label</td></tr>\n";
116 #    }
117     foreach my $forward ( @{$forwardlist} ) {
118       if ($forward->{'srcsvc'} == $account->{'svcnum'}) {
119         my $label = qq!=> ! . $forward->{'dest'};
120         print "<tr><td></td><td></td><td><p>$label</td></tr>\n";
121       }
122     }
123
124   }
125   print "</table>\n";
126   print "</body></html>\n";
127   $printmainpage=0;
128
129 } elsif ($query->param('action') eq 'select') {
130
131   my $username = $query->cookie(-name=>'username');  # session checked
132   $current_package = $query->param('package');
133   $current_account = $query->param('account');
134   $current_domname = $query->param('domname');
135   set_package();
136   print "<html>$body\n";
137   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
138   print "<center>\n";
139   print "<p>Selected package $current_package\n";
140   print "</center>\n";
141   print "</form>\n";
142   print "</body></html>\n";
143   $printmainpage=0;
144
145 } elsif ($query->param('action') eq 'change') {
146
147   my $username = $query->cookie(-name=>'username');  # session checked
148   select_package($username) unless $current_package;
149   my $account  = $query->param('account');
150   my $mailbox  = $query->param('mailbox');
151   my $list = list_forwards($username, $account);
152   print "<html>$body\n";
153   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
154   print "<center><table border=0>\n";
155   print "<tr><td></td><td><p>Username</td><td><p>$mailbox</td></tr>\n";
156   print "<input type=hidden name=\"account\" value=\"$account\">\n";
157   print "<input type=hidden name=\"mailbox\" value=\"$mailbox\">\n";
158   foreach my $forward ( @{$list} ) {
159     my $label = qq!=> ! . $forward->{'dest'};
160 #    print "<tr><td></td><td></td><td><p>$label</td></tr>\n";
161     print "<tr><td></td><td></td><td><p>$label</td><td><a href=\"$cgi\?action=deleteforward&service=$forward->{'svcnum'}&mailbox=$mailbox&dest=$forward->{'dest'}\" target=\"rightmainframe\">remove</td></tr>\n";
162   }
163   print "<tr><td></td><td><p>Password</td><td><input type=text name=\"_password\" value=\"\"></td></tr>\n";
164   print "</table>\n";
165   print "<input type=submit name=\"deleteaccount\" value=\"Delete This User\">\n";
166   print "<input type=submit name=\"changepassword\" value=\"Change The Password\">\n";
167   print "<input type=submit name=\"addforward\" value=\"Add Forwarding\">\n";
168   print "</center>\n";
169   print "</form>\n";
170   print "<br>\n";
171   print "<p> You may delete this user and all mailforwarding by pressing <B>Delete This User</B>.\n";
172   print "<p> To set or change the password for this user, type the new password in the box next to <B>Password</B> and press <B>Change The Password</B>.\n";
173   print "<p> If you would like to have mail destined for this user forwarded to another email address then press the <B>Add Forwarding</B> button.\n";
174   print "</body></html>\n";
175   $printmainpage=0;
176
177 } elsif ($query->param('deleteaccount') ne '') {
178
179   my $username = $query->cookie(-name=>'username');  # session checked
180   select_package($username) unless $current_package;
181   my $account  = $query->param('account');
182   my $mailbox  = $query->param('mailbox');
183   print "<html>$body\n";
184   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
185   print "<p>Are you certain you want to delete user $mailbox?\n";
186   print "<p><input type=hidden name=\"account\" value=\"$account\">\n";
187   print "<input type=submit name=\"deleteaccounty\" value=\"Confirm\">\n";
188   print "</body></html>\n";
189   $printmainpage=0;
190
191 } elsif ($query->param('deleteaccounty') ne '') {
192
193   my $username = $query->cookie(-name=>'username');  # session checked
194   select_package($username) unless $current_package;
195   my $account  = $query->param('account');
196   
197   if  ( my $error = delete_mailbox ( {
198       'authuser'         => $username,
199       'account'          => $account,
200     } ) ) {
201     print "<html>$body\n";
202     print "<p>$error\n";
203     print "</body></html>\n";
204       
205   } else {
206     print "<html>$body\n";
207     print "<p>Deleted\n";
208     print "</body></html>\n";
209   }
210
211   $printmainpage=0;
212
213 } elsif ($query->param('changepassword') ne '') {
214
215   my $username = $query->cookie(-name=>'username');  # session checked
216   select_package($username) unless $current_package;
217   my $account  = $query->param('account');
218   my $_password  = $query->param('_password');
219   
220   if  ( my $error = password_mailbox ( {
221       'authuser'         => $username,
222       'account'          => $account,
223       '_password'        => $_password,
224     } ) ) {
225     print "<html>$body\n";
226     print "<p>$error\n";
227     print "</body></html>\n";
228       
229   } else {
230     print "<html>$body\n";
231     print "<p>Changed\n";
232     print "</body></html>\n";
233   }
234
235   $printmainpage=0;
236
237 } elsif ($query->param('action') eq 'newmailbox') {
238
239   my $username = $query->cookie(-name=>'username');  # session checked
240   select_package($username) unless $current_package;
241   print "<html>$body\n";
242   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
243   print "<center><table border=0>\n";
244   print "<tr><td></td><td><p>Username </td><td><input type=text name=\"account\" value=\"\"></td><td>@ " . $current_domname . "</td></tr>\n";
245   print "<tr><td></td><td><p>Password</td><td><input type=text name=\"_password\" value=\"\"></td></tr>\n";
246   print "</table>\n";
247   print "<input type=submit name=\"addmailbox\" value=\"Add This User\">\n";
248   print "</center>\n";
249   print "</form>\n";
250   print "<br>\n";
251   print "<p>Use this screen to add a new mailbox user.  If the domain name of the email address (the part after the <B>@</B> sign) is not what you expect then you may need to use <B>List Packages</B> to select the package with the correct domain.\n";
252   print "<p>Enter the first portion of the email address in the box adjacent to <B>Username</B> and enter the password for that user in the space next to <B>Password</B>.  Then press the button labeled <B>Add The User</B>.\n";
253   print "<p>If you do not want to add a new user at this time then select a choice from the menu at the left, such as <B>List Mailboxes</B>.\n";
254   print "</body></html>\n";
255   $printmainpage=0;
256
257 } elsif ($query->param('addmailbox') ne '') {
258
259   my $username = $query->cookie(-name=>'username');  # session checked
260   select_package($username) unless $current_package;
261   my $account  = $query->param('account');
262   my $_password  = $query->param('_password');
263   
264   if  ( my $error = add_mailbox ( {
265       'authuser'         => $username,
266       'package'          => $current_package,
267       'account'          => $account,
268       '_password'        => $_password,
269     } ) ) {
270     print "<html>$body\n";
271     print "<p>$error\n";
272     print "</body></html>\n";
273       
274   } else {
275     print "<html>$body\n";
276     print "<p>Created\n";
277     print "</body></html>\n";
278   }
279
280   $printmainpage=0;
281
282 } elsif ($query->param('action') eq 'deleteforward') {
283
284   my $username = $query->cookie(-name=>'username');  # session checked
285   select_package($username) unless $current_package;
286   my $svcnum   = $query->param('service');
287   my $mailbox  = $query->param('mailbox');
288   my $dest  = $query->param('dest');
289   print "<html>$body\n";
290   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
291   print "<p>Are you certain you want to remove the forwarding from $mailbox to $dest?\n";
292   print "<p><input type=hidden name=\"service\" value=\"$svcnum\">\n";
293   print "<input type=submit name=\"deleteforwardy\" value=\"Confirm\">\n";
294   print "</body></html>\n";
295   $printmainpage=0;
296
297 } elsif ($query->param('deleteforwardy') ne '') {
298
299   my $username = $query->cookie(-name=>'username');  # session checked
300   select_package($username) unless $current_package;
301   my $service  = $query->param('service');
302   
303   if  ( my $error = delete_forward ( {
304       'authuser'        => $username,
305       'svcnum'          => $service,
306     } ) ) {
307     print "<html>$body\n";
308     print "<p>$error\n";
309     print "</body></html>\n";
310       
311   } else {
312     print "<html>$body\n";
313     print "<p>Forwarding Removed\n";
314     print "</body></html>\n";
315   }
316
317   $printmainpage=0;
318
319 } elsif ($query->param('addforward') ne '') {
320
321   my $username = $query->cookie(-name=>'username');  # session checked
322   select_package($username) unless $current_package;
323   my $account  = $query->param('account');
324   my $mailbox  = $query->param('mailbox');
325   
326   print "<html>$body\n";
327   print "<form name=form1 action=\"$cgi\" method=post target=\"rightmainframe\">\n";
328   print "<center><table border=0>\n";
329   print "<input type=hidden name=\"account\" value=\"$account\">\n";
330   print "<input type=hidden name=\"mailbox\" value=\"$mailbox\">\n";
331   print "<tr><td>Forward mail from </td><td><p>$mailbox:</td><td> to </td></tr>\n";
332   print "<tr><td></td><td><p>Destination:</td><td><input type=text name=\"dest\" value=\"\"></td></tr>\n";
333   print "</table>\n";
334   print "<input type=submit name=\"addforwarddst\" value=\"Add the Forwarding\">\n";
335   print "</center>\n";
336   print "</form>\n";
337   print "<br>\n";
338   print "<p> If you would like mail originally destined for the above address to be forwarded to a different email address then type that email address in the box next to <B>Destination:</B> and press the <B>Add the Forwarding</B> button.\n";
339   print "<p> If you do not want to add mail forwarding then select a choice from the menu at the left, such as <B>List Accounts</B>.\n";
340
341   $printmainpage=0;
342
343 } elsif ($query->param('addforwarddst') ne '') {
344
345   my $username = $query->cookie(-name=>'username');  # session checked
346   select_package($username) unless $current_package;
347   my $account  = $query->param('account');
348   my $dest  = $query->param('dest');
349   
350   if  ( my $error = add_forward ( {
351       'authuser'         => $username,
352       'package'          => $current_package,
353       'source'           => $account,
354       'dest'             => $dest,
355     } ) ) {
356     print "<html>$body\n";
357     print "<p>$error\n";
358     print "</body></html>\n";
359       
360   } else {
361     print "<html>$body\n";
362     print "<p>Forwarding Created\n";
363     print "</body></html>\n";
364   }
365
366   $printmainpage=0;
367
368 } elsif ($query->param('action') eq 'navframe') {
369
370   print "<html><body bgcolor=bbbbbb>\n";
371   print "<center><h2>NCI2000 MAIL ADMIN Web Interface</h2></center>\n";
372
373   print "<br><center>Choose Action:</center><br>\n";
374   print "<center><table border=0>\n";
375   print "<ul>\n";
376   print "<tr><td><li><a href=\"$cgi\?action=logout\" target=\"_top\">Log Off</a></td><tr>\n";
377   print "<tr><td><li><a href=\"$cgi\?action=list_packages\" target=\"rightmainframe\">List Packages</a></td><tr>\n";
378   print "<tr><td><li><a href=\"$cgi\?action=list_mailboxes\" target=\"rightmainframe\">List Accounts</a></td><tr>\n";
379   print "<tr><td><li><a href=\"$cgi\?action=newmailbox\" target=\"rightmainframe\">Add Account</a></td><tr>\n";
380   print "</ul>\n";
381   print "</table></center>\n";
382
383   print "<br><br><br>\n";
384   print "</body></html>\n";
385
386   $printmainpage = 0;
387
388 } elsif ($query->param('action') eq 'rightmainframe') {
389
390   print "<html>$body\n";
391   print "<br><br><br>\n";
392   print "<font size=4><----- Please choose function on the left menu</font>\n";
393   print "<br><br>\n";
394   print "<p> Choose <B>Log Off</B> when you are finished.  This helps prevent unauthorized access to your accounts.\n";
395   print "<p> Use <B>List Packages</B> when you administer multiple packages.  When you have multiple domains at NCI2000 you are likely to have multiple packages.  Use of <B>List Packages</B> is not necessary if administer only one package.\n";
396   print "<p> Use <B>List Accounts</B> to view your current arrangement of mailboxes.  From this list you my choose to make changes to existing mailboxes or delete mailboxes.  If you would like to modify the forwarding associated with a mailbox then choose it from this list.\n";
397   print "<p> Use <B>Add Account</B> when you would like an additional mailbox.  After you have added the mailbox you may choose to make additional changes from the list provided by <B>List Accounts<B>.\n";
398   print "</body></html>\n";
399
400   $printmainpage = 0;
401
402 }
403
404
405 if ($query->param('action') eq 'login') {
406
407     printheader();
408     printlogin();
409
410 } elsif ($query->param('action') eq 'logout') {
411
412     destroysession();
413     printheader();
414     printlogin();
415
416 } elsif ($printmainpage) {
417
418
419   print "<html><head><title>NCI2000 MAIL ADMIN Web Interface</title></head>\n";
420   print "<FRAMESET cols=\"160,*\" BORDER=\"3\">\n";
421   print "<FRAME NAME=\"navframe\" src=\"$cgi?action=navframe\">\n";
422   print "<FRAME NAME=\"rightmainframe\" src=\"$cgi?action=rightmainframe\">\n";
423   print "</FRAMESET>\n";
424   print "</html>\n";
425
426
427 }
428
429 sub getdatetime {
430   my $today = localtime(time());
431   my ($day,$mon,$dayofmon,$time,$year) = split(/\s+/,$today);
432   my @datemonths = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
433
434   my $numidx = "01";
435   my ($nummon);
436   foreach my $mons (@datemonths) {
437     if ($mon eq $mons) {
438      $nummon = $numidx;
439     }
440     $numidx++;
441   }
442
443   return "$year-$nummon-$dayofmon $time";
444
445 }
446
447 sub error {
448
449   my $error = shift;
450   my $arg1 = shift;
451
452    printheader();
453
454    if ($error eq 'not_admin') {
455      print "<html><head><title>Error!</title></head>\n";
456      print "$body\n";
457      print "<center><h1><font face=arial>Error!</font></h1></center>\n";
458      print "<font face=arial>Unauthorized attempt to access mail administration.</font>\n";
459      print "<br><font face=arial>Please login again if you think this is an error.</font>\n";
460      print "<form><input type=button value=\"<<Back\" OnClick=\"history.back()\"></form>\n";
461      print "</body></html>\n";
462    } elsif ($error eq 'exists') {
463      print "<html><head><title>Error!</title></head>\n";
464      print "$body\n";
465      print "<center><h1><font face=arial>Error!</font></h1></center>\n";
466      print "<font face=arial>The user you are trying to enter already exists. Please go back and enter a different username</font>\n";
467      print "</font></body></html>\n";
468    } elsif ($error eq 'ingroup') {
469      print "<html><head><title>Error!</title></head>\n";
470      print "$body\n";
471      print "<center><h1><font face=arial>Error!</font></h1></center>\n";
472      print "<font face=arial>This user is already in the group <i>$arg1</i>. Please go back and deselect group <i>$arg1</i> from the list.</font>\n";
473      print "<form><input type=button value=\"<<Back\" OnClick=\"history.back()\"></form>\n";
474      print "</font></body></html>\n";
475    } elsif ($error eq 'sess_expired') {
476      print "<html>$body\n";
477      print "<center><font size=4>Your session has expired.</font></center>\n";
478      print "<br><br><center>Please login again <a href=\"$cgi\?action=login\" target=\"_top\"> HERE</a></center>\n";
479      print "</body></html>\n";
480    } elsif ($error eq 'open') {
481      print "<html>$body\n";
482      print "<center><font size=4>Unable to open or rename file.</font></center>\n";
483      print "<br><br><center>If this continues, please contact your administrator</center>\n";
484      print "</body></html>\n";
485    }
486
487
488    exit;
489
490 }
491
492
493 #print a html header if not printed yet
494 sub printheader {
495
496   if ($printheader) {
497      print "Content-Type: text/html\n\n";
498      $printheader = 0;
499   }
500
501 }
502
503
504 #verify user can access administration
505 sub checksession {
506
507   my $username = $query->cookie(-name=>'username');
508   my $sessionid = $query->cookie(-name=>'ma_sessionid');
509
510   if ($sessionid eq '') {
511      printheader();
512      if ($query->param()) {
513         error('sess_expired');
514      } else {
515         printlogin();
516         exit;
517     }
518   }
519
520   my $now = time();
521   my $founduser = 0;
522   open(SESSFILE, "$sessionfile") || error('open');
523   error('open') if -l "$tmpdir/adminsess.$$";
524   open(NEWSESS, ">$tmpdir/adminsess.$$") || error('open');
525   while (<SESSFILE>) {
526         chomp();
527         my ($user, $sess, $time, $pkgnum, $svcdomain, $domname) = split(/\s+/);
528         next if $now - $sessexpire > $time;
529         if ($username eq $user && !$founduser) {
530                 if ($sess eq $sessionid) {
531                         $founduser = 1;
532                         print NEWSESS "$user $sess $now $pkgnum $svcdomain $domname\n";
533                         $current_package=$pkgnum;
534                         $current_account=$svcdomain;
535                         $current_domname=$domname;
536                         next;
537                 }
538         }
539         print NEWSESS "$user $sess $time $pkgnum $svcdomain $domname\n";
540   }
541   close(SESSFILE);
542   close(NEWSESS);
543   system("mv $tmpdir/adminsess.$$ $sessionfile");
544   error('sess_expired') unless $founduser;
545
546   my $cookie1 = $query->cookie(-name=>'username',
547                                 -value=>$username,
548                                 -expires=>$cookieexpire,
549                                 -domain=>$cookiedomain);
550
551   my $cookie2 = $query->cookie(-name=>'ma_sessionid',
552                                 -value=>$sessionid,
553                                 -expires=>$cookieexpire,
554                                 -domain=>$cookiedomain);
555
556   print $query->header(-COOKIE=>[$cookie1, $cookie2]);
557   
558   $printheader = 0;
559
560   return 0;
561
562 }
563
564 sub destroysession {
565
566   my $username = $query->cookie(-name=>'username');
567   my $sessionid = $query->cookie(-name=>'ma_sessionid');
568
569   if ($sessionid eq '') {
570      printheader();
571      if ($query->param()) {
572         error('sess_expired');
573      } else {
574         printlogin();
575         exit;
576     }
577   }
578
579   my $now = time();
580   my $founduser = 0;
581   open(SESSFILE, "$sessionfile") || error('open');
582   error('open') if -l "$tmpdir/adminsess.$$";
583   open(NEWSESS, ">$tmpdir/adminsess.$$") || error('open');
584   while (<SESSFILE>) {
585         chomp();
586         my ($user, $sess, $time, $pkgnum, $svcdomain, $domname) = split(/\s+/);
587         next if $now - $sessexpire > $time;
588         if ($username eq $user && !$founduser) {
589                 if ($sess eq $sessionid) {
590                         $founduser = 1;
591                         next;
592                 }
593         }
594         print NEWSESS "$user $sess $time $pkgnum $svcdomain $domname\n";
595   }
596   close(SESSFILE);
597   close(NEWSESS);
598   system("mv $tmpdir/adminsess.$$ $sessionfile");
599   error('sess_expired') unless $founduser;
600
601   $printheader = 0;
602
603   return 0;
604
605 }
606
607 # checks the username and pass against the database
608 sub checkuserpass {
609
610   my $username = shift;
611   my $password = shift;
612
613   my $error = authenticate ( {
614       'authuser'         => $username,
615       '_password'        => $password,
616     } ); 
617
618   if ($error eq "$username OK") {
619     return 1;
620   }else{
621     return 0;
622   }
623
624 }
625
626 #printlogin prints a login page
627 sub printlogin {
628
629         print "<html>$body\n";
630         print "<center><font size=4>Please login to access MAIL ADMIN</font></center>\n";
631         print "<form action=\"$cgi\" method=post>\n";
632         print "<center>Email Address: &nbsp; <input type=text name=\"username\">\n";
633         print "<br>Email Password: <input type=password name=\"password\">\n";
634         print "<br><input type=submit name=\"login\" value=\"Login\">\n";
635         print "</form></center>\n";
636         print "</body></html>\n";
637 }
638
639
640 #select_package chooses a administrable package if more than one exists
641 sub select_package {
642         my $user = shift;
643         my $packages = list_packages($user);
644         if (scalar(@{$packages}) eq 1) {
645           $current_package = @{$packages}[0]->{'pkgnum'};
646           set_package();
647         }
648         if (scalar(@{$packages}) > 1) {
649 #          print $query->redirect("$cgi\?action=list_packages");
650            print "<p>No package selected.  You must first <a href=\"$cgi\?action=list_packages\" target=\"rightmainframe\">select a package</a>.\n";
651           exit;
652         }
653 }
654
655 sub set_package {
656
657   my $username = $query->cookie(-name=>'username');
658   my $sessionid = $query->cookie(-name=>'ma_sessionid');
659
660   if ($sessionid eq '') {
661      printheader();
662      if ($query->param()) {
663         error('sess_expired');
664      } else {
665         printlogin();
666         exit;
667     }
668   }
669
670   my $now = time();
671   my $founduser = 0;
672   open(SESSFILE, "$sessionfile") || error('open');
673   error('open') if -l "$tmpdir/adminsess.$$";
674   open(NEWSESS, ">$tmpdir/adminsess.$$") || error('open');
675   while (<SESSFILE>) {
676         chomp();
677         my ($user, $sess, $time, $pkgnum, $svcdomain, $domname) = split(/\s+/);
678         next if $now - $sessexpire > $time;
679         if ($username eq $user && !$founduser) {
680                 if ($sess eq $sessionid) {
681                         $founduser = 1;
682                         print NEWSESS "$user $sess $time $current_package $current_account $current_domname\n";
683                         next;
684                 }
685         }
686         print NEWSESS "$user $sess $time $pkgnum $svcdomain $domname\n";
687   }
688   close(SESSFILE);
689   close(NEWSESS);
690   system("mv $tmpdir/adminsess.$$ $sessionfile");
691   error('sess_expired') unless $founduser;
692
693   $printheader = 0;
694
695   return 0;
696
697 }
698