Commits

Sebastian Sdorra committed 8994d97

added possibility to extract username and/or password from gecos field

  • Participants
  • Parent commits ed9d86d

Comments (0)

Files changed (5)

src/main/java/sonia/scm/pam/GecosParser.java

+/**
+ * Copyright (c) 2010, Sebastian Sdorra All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer. 2. Redistributions in
+ * binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution. 3. Neither the name of SCM-Manager;
+ * nor the names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * http://bitbucket.org/sdorra/scm-manager
+ *
+ */
+
+
+
+package sonia.scm.pam;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import sonia.scm.user.User;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+public interface GecosParser
+{
+
+  /**
+   * Method description
+   *
+   *
+   * @param config
+   * @param user
+   * @param gecos
+   */
+  public void parse(PAMConfig config, User user, String gecos);
+}

src/main/java/sonia/scm/pam/PAMAuthenticationHandler.java

           {
             User user = new User(username);
 
+            parseGecos(unixUser, user);
             result = new AuthenticationResult(user, unixUser.getGroups());
           }
         }
     this.config = config;
   }
 
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param unixUser
+   * @param user
+   */
+  private void parseGecos(UnixUser unixUser, User user)
+  {
+    try
+    {
+      GecosParser parser = new RegexGecosParser();
+
+      parser.parse(config, user, unixUser.getGecos());
+    }
+    catch (Exception ex)
+    {
+      logger.error("could not parse gecos", ex);
+    }
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**

src/main/java/sonia/scm/pam/PAMConfig.java

    *
    * @return
    */
+  public String getGecosExpression()
+  {
+    return gecosExpression;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
   public String getServiceName()
   {
     return serviceName;
    * Method description
    *
    *
+   * @param gecosExpression
+   */
+  public void setGecosExpression(String gecosExpression)
+  {
+    this.gecosExpression = gecosExpression;
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param serviceName
    */
   public void setServiceName(String serviceName)
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
+  @XmlElement(name = "gecos-expression")
+  private String gecosExpression;
+
+  /** Field description */
   @XmlElement(name = "service-name")
   private String serviceName = "sshd";
 }

src/main/java/sonia/scm/pam/RegexGecosParser.java

+/**
+ * Copyright (c) 2010, Sebastian Sdorra All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer. 2. Redistributions in
+ * binary form must reproduce the above copyright notice, this list of
+ * conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution. 3. Neither the name of SCM-Manager;
+ * nor the names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * http://bitbucket.org/sdorra/scm-manager
+ *
+ */
+
+
+
+package sonia.scm.pam;
+
+//~--- non-JDK imports --------------------------------------------------------
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import sonia.regexp.NamedMatcher;
+import sonia.regexp.NamedPattern;
+
+import sonia.scm.user.User;
+import sonia.scm.util.Util;
+
+//~--- JDK imports ------------------------------------------------------------
+
+import java.util.List;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+public class RegexGecosParser implements GecosParser
+{
+
+  /** Field description */
+  public static final String GROUP_DISPLAYNAME = "displayName";
+
+  /** Field description */
+  public static final String GROUP_MAIL = "mail";
+
+  /**
+   * the logger for RegexGecosParser
+   */
+  private static final Logger logger =
+    LoggerFactory.getLogger(RegexGecosParser.class);
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param config
+   * @param user
+   * @param gecos
+   */
+  @Override
+  public void parse(PAMConfig config, User user, String gecos)
+  {
+    if (Util.isNotEmpty(config.getGecosExpression()))
+    {
+      parse(user, config.getGecosExpression(), gecos);
+    }
+    else if (logger.isDebugEnabled())
+    {
+      logger.debug("no gecos expression defined");
+    }
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param user
+   * @param expression
+   * @param gecos
+   */
+  private void parse(User user, String expression, String gecos)
+  {
+    if (Util.isNotEmpty(gecos))
+    {
+      if (logger.isTraceEnabled())
+      {
+        logger.trace("parse gecos '{}' with expression '{}'");
+      }
+
+      NamedPattern p = NamedPattern.compile(expression);
+      List<String> groupNames = p.groupNames();
+      NamedMatcher m = p.matcher(gecos);
+
+      if (m.matches())
+      {
+        if (groupNames.contains(GROUP_DISPLAYNAME))
+        {
+          String displayName = m.group(GROUP_DISPLAYNAME);
+
+          if (Util.isNotEmpty(displayName))
+          {
+            if (logger.isTraceEnabled())
+            {
+              logger.trace("extracted displayName '{}' from gecos '{}'",
+                           displayName, gecos);
+            }
+
+            user.setDisplayName(displayName);
+          }
+          else if (logger.isTraceEnabled())
+          {
+            logger.trace("'{}' is empty in gecos '{}'", GROUP_DISPLAYNAME,
+                         gecos);
+          }
+        }
+        else if (logger.isTraceEnabled())
+        {
+          logger.trace("expression '{}' contains no '{}' group", expression,
+                       GROUP_DISPLAYNAME);
+        }
+
+        if (groupNames.contains(GROUP_MAIL))
+        {
+          String mail = m.group(GROUP_MAIL);
+
+          if (Util.isNotEmpty(mail))
+          {
+            if (logger.isTraceEnabled())
+            {
+              logger.trace("extracted mail '{}' from gecos '{}'", mail, gecos);
+            }
+
+            user.setMail(mail);
+          }
+          else if (logger.isTraceEnabled())
+          {
+            logger.trace("'{}' is empty in gecos '{}'", GROUP_MAIL, gecos);
+          }
+        }
+        else if (logger.isTraceEnabled())
+        {
+          logger.trace("expression '{}' contains no '{}' group", expression,
+                       GROUP_MAIL);
+        }
+      }
+    }
+    else if (logger.isDebugEnabled())
+    {
+      logger.debug("gecos is empty");
+    }
+  }
+}

src/main/resources/sonia/scm/pam/sonia.pam.js

   title : 'PAM Authentication',
   items : [{
     xtype : 'textfield',
+    fieldLabel : 'Gecos Expression',
+    name : 'gecos-expression',
+    helpText: 'Regular Expression to extract display name and/or mail from gecos\n\
+               field. The expression should contain one or both of the named groups \n\
+               displayName and mail. e.g. (?&lt;displayName&gt;[^,]*,(?&lt;mail&gt;[^,]*)',
+    allowBlank : true    
+  },{
+    xtype : 'textfield',
     fieldLabel : 'Service name',
     name : 'service-name',
     helpText: 'PAM service name. This corresponds to the service name that \n\