Drizzled Public API Documentation

auth_pam.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /*
00021   Sections of this were taken/modified from mod_auth_path for Apache
00022   @TODO: License?
00023 */
00024 
00025 #include <config.h>
00026 
00027 #include <drizzled/identifier.h>
00028 #include <drizzled/plugin/authentication.h>
00029 
00030 #include <security/pam_appl.h>
00031 #if !defined(__sun) && !defined(__FreeBSD__)
00032 #include <security/pam_misc.h>
00033 #endif
00034 
00035 using namespace drizzled;
00036 
00037 typedef struct {
00038     const char *name;
00039     const char *password;
00040 } auth_pam_userinfo;
00041 
00042 extern "C"
00043 int auth_pam_talker(int num_msg,
00044 #ifdef __sun
00045                     struct pam_message **msg,
00046 #else
00047                     const struct pam_message **msg,
00048 #endif
00049                     struct pam_response **resp,
00050                     void *appdata_ptr);
00051 
00052 int auth_pam_talker(int num_msg,
00053 #ifdef __sun
00054                     struct pam_message **msg,
00055 #else
00056                     const struct pam_message **msg,
00057 #endif
00058                     struct pam_response **resp,
00059                     void *appdata_ptr)
00060 {
00061   auth_pam_userinfo *userinfo = (auth_pam_userinfo*)appdata_ptr;
00062   struct pam_response *response = 0;
00063 
00064   /* parameter sanity checking */
00065   if(not resp || not msg || not userinfo)
00066     return PAM_CONV_ERR;
00067 
00068   /* allocate memory to store response */
00069   response= (struct pam_response*)malloc(num_msg * sizeof(struct pam_response));
00070 
00071   /* copy values */
00072   for(int x= 0; x < num_msg; x++)
00073   {
00074     /* initialize to safe values */
00075     response[x].resp_retcode= 0;
00076     response[x].resp= 0;
00077 
00078     /* select response based on requested output style */
00079     switch(msg[x]->msg_style)
00080     {
00081     case PAM_PROMPT_ECHO_ON:
00082       /* on memory allocation failure, auth fails */
00083       response[x].resp = strdup(userinfo->name);
00084       break;
00085     case PAM_PROMPT_ECHO_OFF:
00086       response[x].resp = strdup(userinfo->password);
00087       break;
00088     default:
00089       free(response);
00090       return PAM_CONV_ERR;
00091     }
00092   }
00093 
00094   /* everything okay, set PAM response values */
00095   *resp = response;
00096 
00097   return PAM_SUCCESS;
00098 }
00099 
00100 class Auth_pam : public drizzled::plugin::Authentication
00101 {
00102 public:
00103   Auth_pam(std::string name_arg)
00104     : drizzled::plugin::Authentication(name_arg) {}
00105   virtual bool authenticate(const identifier::User &sctx,
00106                             const std::string &password)
00107   {
00108     int retval;
00109     auth_pam_userinfo userinfo= { NULL, NULL };
00110     struct pam_conv conv_info= { &auth_pam_talker, (void*)&userinfo };
00111     pam_handle_t *pamh= NULL;
00112 
00113     userinfo.name= sctx.username().c_str();
00114     userinfo.password= password.c_str();
00115 
00116     retval= pam_start("drizzle", userinfo.name, &conv_info, &pamh);
00117 
00118     if (retval == PAM_SUCCESS)
00119       retval= pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
00120 
00121     if (retval == PAM_SUCCESS)
00122       retval= pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK);
00123 
00124     pam_end(pamh, retval);
00125 
00126     return (retval == PAM_SUCCESS) ? true: false;
00127   }
00128 };
00129 
00130 
00131 static Auth_pam *auth= NULL;
00132 
00133 static int initialize(drizzled::module::Context &context)
00134 {
00135   auth= new Auth_pam("auth_pam");
00136   context.add(auth);
00137   return 0;
00138 }
00139 
00140 DRIZZLE_DECLARE_PLUGIN
00141 {
00142   DRIZZLE_VERSION_ID,
00143   "pam",
00144   "0.1",
00145   "Brian Aker",
00146   "PAM based authenication.",
00147   PLUGIN_LICENSE_GPL,
00148   initialize, /* Plugin Init */
00149   NULL,   /* depends */
00150   NULL    /* config options */
00151 }
00152 DRIZZLE_DECLARE_PLUGIN_END;