ispman/lib/ISPMan UserMan.pm,1.58.4.8,1.58.4.9

Pedro Algarvio <[email protected]> Wed, 25 Oct 2006 23:50:07 +0000
Newsgroups gmane.comp.isp.ispman.cvs
Message-ID <[email protected]>
Update of /cvsroot/ispman/ispman/lib/ISPMan
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv13214/lib/ISPMan

Modified Files:
      Tag: dev_1_3-cyrus_virtdomain2
	UserMan.pm 
Log Message:
Updated to current HEAD.


Index: UserMan.pm
===================================================================
RCS file: /cvsroot/ispman/ispman/lib/ISPMan/UserMan.pm,v
retrieving revision 1.58.4.8
retrieving revision 1.58.4.9
diff -u -d -r1.58.4.8 -r1.58.4.9
--- UserMan.pm	24 Oct 2006 21:05:40 -0000	1.58.4.8
+++ UserMan.pm	25 Oct 2006 23:50:05 -0000	1.58.4.9
@@ -50,6 +50,7 @@
 
   addUserFileServerProcess
   containsInvalidMailAliases
+  getDNForMailAddress
 );
 
 $VERSION = '0.01';
@@ -58,8 +59,8 @@
     my $self   = shift;
     my $r      = shift;
     my $domain = $r->param("ispmanDomain");
-    print
-"Sorry cannot add users to a replica domain<br>Instead add users to its master domain which is ";
+    print "Sorry cannot add users to a replica domain<br>"
+      . "Instead add users to its master domain which is ";
     my $replicaMaster =
       $self->getEntry(
         "ispmanDomain=$domain, $self->{'Config'}->{'ldapBaseDN'}",
@@ -124,7 +125,9 @@
     my $domainUserCount = $self->getUserCount($domain);
     if ( $domainMaxUsers > 0 && $domainUserCount >= $domainMaxUsers ) {
         $self->{'message'} =
-"Max accounts limit exceeded. <br>Your limit is $domainMaxUsers <br>You cannot create any more users.";
+            "Max accounts limit exceeded. <br>"
+          . "Your limit is $domainMaxUsers <br>"
+          . "You cannot create any more users.";
         print $self->{'message'};
         return;
     }
@@ -134,6 +137,14 @@
       $self->create_uid( $r->param("userid"), $r->param("ispmanDomain") );
     $r->param( "uid", $_uid );
 
+    # check uid conflicts with existing mail addresses
+    if ( my $check = $self->getDNForMailAddress(
+       join '@', ($r->param("userid"), $r->param("ispmanDomain"))) ) {
+        print "Unable to create user \"$_uid\", because mail address is<br>"
+          . "already blocked by \"$check\".";
+        return;
+    }
+
     # build new DN for user
     my $dn =
       "uid=$_uid, ou=users, ispmanDomain=$domain, "
@@ -143,17 +154,11 @@
     $self->prepareBranchForDN(
         "ou=users, ispmanDomain=$domain, @{[$self->getConf('ldapBaseDN')]}");
 
-# get domain info and supply it to session data so we can use it in user ldif template
+    # get domain info and supply it to session data
+    # so we can use it in user ldif template
     my $domain_info = $self->getDomainInfo( $r->param("ispmanDomain") );
     $r->param( "domaininfo", $domain_info );
 
-# moved to user template
-#my $homeDirectory=join '/', ($domain_info->{'homeDirectory'}, "users", $r->param("uid"), "");
-#my $cn=($r->param("givenName") && $r->param("sn"))? join " ", ($r->param("givenName"), $r->param("sn")) :$r->param("userid");
-#$r->param("cn", $cn);
-#$r->param("sn", ($r->param("sn")? $r->param("sn"):$r->param("userid")));
-#$r->param("homeDirectory", $homeDirectory);
-
     # validate ftp quota
     if (   $r->param("FTPQuotaMBytes") !~ /\d\d*/
         || $r->param("FTPQuotaMBytes") < 0 )
@@ -173,39 +178,42 @@
 
     # add user to LDAP from user ldif template
     if ( $self->addDataFromLdif( "templates/users.ldif.template", $r ) ) {
-        
-        # reread user from LDAP to get computed values
-        my $_userInfo=$self->getUserInfo($_uid);
-        
         if ( $r->param("mailHost") ) {
-            my $quota=$r->param("mailQuota") * 1024;
-            $self->addProcessToHost($domain, $_userInfo->{'mailHost'},
-            "createMailbox",
-            "mailbox=".$_userInfo->{'mailLocalAddress'});
-            
-            $self->addProcessToHost($domain, $_userInfo->{'mailHost'},
-            "setMailboxQuota", 
-            "mailbox=".$_userInfo->{'mailLocalAddress'}."&".
-            "quota=$quota");
+            $self->addProcessToHost( $domain, $r->param("mailHost"),
+                "createMailbox", "mailbox=$_uid" );
+            my $quota = $r->param("mailQuota") * 1024;
+            $self->addProcessToHost( $domain, $r->param("mailHost"),
+                "setMailboxQuota", "mailbox=$_uid&quota=$quota" );
         }
 
-      # send the process to method addUserFileServerProcess
-      # the process will only be sent to fileHost if one is defined
-      # if not defined then we fall back to the old method and send to the filserver group
+        # send the process to method addUserFileServerProcess
+        # the process will only be sent to fileHost if one is defined
+        # if not defined then we fall back to the old method and
+        # send to the filserver group
 
         # passing domain, username, process and username as params
-        $self->addUserFileServerProcess( $domain, $_uid,
-          'createHomeDirectory',
-          "uid=$_uid&".
-          "homeDirectory=$_userInfo->{'homeDirectory'}&".
-          "uidNumber=$_userInfo->{'uidNumber'}&".
-          "gidNumber=$_userInfo->{'gidNumber'}");
-
+        my $_userInfo = $self->getUserInfo($_uid);
+        $self->addUserFileServerProcess( $domain, $_uid, 'createHomeDirectory',
+                "uid=$_uid&homeDirectory=$_userInfo->{'homeDirectory'}&"
+              . "uidNumber=$_userInfo->{'uidNumber'}&"
+              . "gidNumber=$_userInfo->{'gidNumber'}" );
         return 1;
     }
 
 }
 
+sub getDNForMailAddress {
+    my $self  = shift;
+    my $found = "";
+    while ( ! $found && (my $mail = shift )) {
+        my $entries =
+          $self->getEntriesAsHashRef( $self->{'Config'}->{'ldapBaseDN'},
+            "|(mailLocalAddress=$mail)(mailAlias=$mail)" );
+        ($found) = keys %$entries;
+    }
+    return $found;
+}
+
 sub getUserInfo {
     my $self = shift;
     my $uid  = shift;
@@ -247,7 +255,6 @@
     print "Deleting $dn<br>";
     $self->{'user'} = $self->{'ldap'}->getEntryAsHashRef($dn);
 
-
     #it is possible that as user has multiple uids.
     #hint
     #aghaffar_ispman_org
@@ -258,33 +265,31 @@
 
     # first send processes.
 
-# %%% new method %%
-# send the process to method addUserFileServerProcess
-# the process will only be sent to fileHost if one is defined
-# if not defined then we fall back to the old method and send to the filserver group
-
-# passing domain, username, process and username,homedirecory,uid,gid  as params
+    # %%% new method %%
+    # send the process to method addUserFileServerProcess
+    # the process will only be sent to fileHost if one is defined
+    # if not defined then we fall back to the old method and
+    # send to the filserver group
 
     $self->addUserFileServerProcess(
         $self->{'user'}{'ispmanDomain'},
         $self->{'user'}{'uid'},
         'deleteHomeDirectory',
-        join '&', (
-                   "uid=" . $self->{'user'}{'uid'},       
-                   "homeDirectory=" . $self->{'user'}{'homeDirectory'},
-                   "uidNumber=" . $self->{'user'}{'uidNumber'}, 
-                   "gidNumber=" . $self->{'user'}{'gidNumber'}
+        join '&',
+        (
+            "uid=" . $self->{'user'}{'uid'},
+            "homeDirectory=" . $self->{'user'}{'homeDirectory'},
+            "uidNumber=" . $self->{'user'}{'uidNumber'},
+            "gidNumber=" . $self->{'user'}{'gidNumber'}
         )
     );
 
     if ( $self->{'user'}{"mailHost"} ) {
         $self->addProcessToHost(
-            $self->{'user'}{'ispmanDomain'},
-            $self->{'user'}{"mailHost"},
-            "deleteMailbox", 
-            "mailbox=".$self->{'user'}{'mailLocalAddress'});
+            $self->{'user'}{'ispmanDomain'}, $self->{'user'}{"mailHost"},
+            "deleteMailbox",                 "mailbox=$self->{'user'}{'uid'}"
+        );
     }
-
     $self->deleteEntry($dn);
 }
 
@@ -292,9 +297,9 @@
     my $self = shift;
     my $r    = shift;
 
-    my $dn   = $self->getUserDN( $r->param("ispmanDomain"), $r->param("uid") );
+    my $dn = $self->getUserDN( $r->param("ispmanDomain"), $r->param("uid") );
 
-    $self->killUser( $dn );
+    $self->killUser($dn);
 
     print $self->refreshSignal( $r->param("ispmanDomain") );
 
@@ -312,28 +317,26 @@
 }
 
 # added by Michael Bunk, Jan 2006
-sub containsInvalidMailAliases
-{
-  my $self = shift;
-  my $aliases = shift;
-  my $masterDomain = shift;
-  my $replicas = $self->getReplicasOfDomain($masterDomain);
-  my @validDomains; 
-  if(defined($replicas)) { @validDomains = keys %$replicas; }
-  push @validDomains, $masterDomain;
-OUTER: foreach(@$aliases)
-  {
-    next if(/^[\w\.\-\+]*$/);
-    if(/^[\w\.\-\+]*@([\w\.\-]+)$/)
-    {
-      foreach my $d (@validDomains)
-      {
-	next OUTER if($d eq $1);
-      }
+sub containsInvalidMailAliases {
+    my $self         = shift;
+    my $aliases      = shift;
+    my $masterDomain = shift;
+    my $replicas     = $self->getReplicasOfDomain($masterDomain);
+    my @validDomains;
+    if ( defined($replicas) ) { @validDomains = keys %$replicas; }
+    push @validDomains, $masterDomain;
+  OUTER: foreach (@$aliases) {
+        next if (/^[\w\.\-\+]*$/);
+        if (/^[\w\.\-\+]*@([\w\.\-]+)$/) {
+            foreach my $d (@validDomains) {
+                next OUTER if ( $d eq $1 );
+            }
+        }
+        return ( 1,
+            _("Mail aliases to users from foreign domains are forbidden: '$_'")
+        );
     }
-    return (1, _("Mail aliases to users from foreign domains are forbidden: '$_'"));
-  }
-  return (0, undef);
+    return ( 0, undef );
 }
 
 sub update_user {
@@ -341,7 +344,7 @@
     my $r    = shift;
     my $dn   = $self->getUserDN( $r->param("ispmanDomain"), $r->param("uid") );
     my $userInfo = $self->getUserInfo( $r->param("uid") );
-    my $data = {};
+    my $data     = {};
     for (
         qw(sn givenName homeDirectory loginShell mailLocalAddress mailRoutingAddress FTPStatus FTPQuotaMBytes)
       )
@@ -357,32 +360,41 @@
     $data->{'mailAlias'} =
       $self->as_arrayref( split( /\s+/, $r->param("mailAlias") ) );
 
-    # added by Michael Bunk, Jan 2006
-    my ($invalid, $errmsg) = $self->containsInvalidMailAliases($data->{'mailAlias'},
-      $userInfo->{'ispmanDomain'});
-    if($invalid)
+    # check uniqueness of mail aliases
+    if ( my $check =
+        $self->getDNForMailAddress( split( /\s+/, $r->param("mailAlias") ) ) )
     {
-      print $errmsg;
-      return 0;
+        print "Mail aliases conflict with \"$check\".<br>";
+        print "Aborting update!";
+        return 0;
+    }
+
+    # added by Michael Bunk, Jan 2006
+    my ( $invalid, $errmsg ) =
+      $self->containsInvalidMailAliases( $data->{'mailAlias'},
+        $userInfo->{'ispmanDomain'} );
+    if ($invalid) {
+        print $errmsg;
+        return 0;
     }
 
     $data->{'mailForwardingAddress'} =
       $self->as_arrayref( split( /\s+/, $r->param("mailForwardingAddress") ) );
 
-    # don't set objectclasses here! It would dishonor non-ispman 
+    # don't set objectclasses here! It would dishonor non-ispman
     # objectclasses and attributes. Update issues are handled by
     # ldifupdate anyways.
     # $data->{'objectClass'} = $self->{'Config'}{'ispmanUserObjectclasses'};
 
     if ( $userInfo->{'mailHost'} && $r->param("mailQuota") ) {
-        my $quota = $r->param("mailQuota") * 1024;
-        my $mailbox=$r->param("uid");
+        my $quota   = $r->param("mailQuota") * 1024;
+        my $mailbox = $r->param("uid");
         unless ( $quota == $userInfo->{'mailQuota'} ) {
             $data->{'mailQuota'} = $quota;
             $self->addProcessToHost(
-                $userInfo->{'ispmanDomain'},
-                $userInfo->{'mailHost'},
-                "setMailboxQuota", "mailbox=$mailbox&quota=$quota");
+                $userInfo->{'ispmanDomain'}, $userInfo->{'mailHost'},
+                "setMailboxQuota",           "mailbox=$mailbox&quota=$quota"
+            );
         }
     }
 
@@ -416,13 +428,14 @@
     my ( $uid, $domain, $values ) = @_;
 
     # added by Michael Bunk, Jan 2006
-    my ($invalid, $errmsg) = $self->containsInvalidMailAliases($values, $domain);
-    if($invalid)
-    {
-      print $errmsg;
-      # actually, we should return a Net::LDAP::Message to return the same
-      # type as addUserAttributeValues()
-      return 0;
+    my ( $invalid, $errmsg ) =
+      $self->containsInvalidMailAliases( $values, $domain );
+    if ($invalid) {
+        print $errmsg;
+
+        # actually, we should return a Net::LDAP::Message to return the same
+        # type as addUserAttributeValues()
+        return 0;
     }
 
     return $self->addUserAttributeValues( $uid, $domain, 'mailAlias', $values );
@@ -433,13 +446,14 @@
     my ( $uid, $domain, $values ) = @_;
 
     # added by Michael Bunk, Jan 2006
-    my ($invalid, $errmsg) = $self->containsInvalidMailAliases($values, $domain);
-    if($invalid)
-    {
-      print $errmsg;
-      # actually, we should return a Net::LDAP::Message to return the same
-      # type as addUserAttributeValues()
-      return 0;
+    my ( $invalid, $errmsg ) =
+      $self->containsInvalidMailAliases( $values, $domain );
+    if ($invalid) {
+        print $errmsg;
+
+        # actually, we should return a Net::LDAP::Message to return the same
+        # type as addUserAttributeValues()
+        return 0;
     }
 
     return $self->replaceUserAttributeValues( $uid, $domain, 'mailAlias',
@@ -499,7 +513,6 @@
 sub getUserDN {
     my $self = shift;
     my ( $domain, $uid ) = @_;
-    $uid =~ s/\.|\@/_/g;
     return join( ",", "uid=$uid", $self->getUsersBranchDN($domain) );
 }
 
@@ -595,14 +608,11 @@
         { $attribute => $_values } );
 }
 
+# generate unique uid from userid and domain
 sub create_uid {
     my $self = shift;
     my ( $userId, $domain ) = @_;
 
- # here is the logic to convert userName to user_domain_tld
- # If you don't want this behaviour and just wasn't uid uncomment the next line;
- # return $userId;
-
     $domain =~ s/\./_/g;
     $userId =~ s/\./_/g;
 


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642