drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
auth_pam.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 Sections of this were taken/modified from mod_auth_path for Apache
22 @TODO: License?
23*/
24
25#include <config.h>
26
27#include <drizzled/identifier.h>
28#include <drizzled/plugin/authentication.h>
29
30#include <security/pam_appl.h>
31#include <security/pam_misc.h>
32
33using namespace drizzled;
34
35typedef struct {
36 const char *name;
37 const char *password;
39
40extern "C"
41int auth_pam_talker(int num_msg,
42 const struct pam_message **msg,
43 struct pam_response **resp,
44 void *appdata_ptr);
45
46int auth_pam_talker(int num_msg,
47 const struct pam_message **msg,
48 struct pam_response **resp,
49 void *appdata_ptr)
50{
51 auth_pam_userinfo *userinfo = (auth_pam_userinfo*)appdata_ptr;
52 struct pam_response *response = 0;
53
54 /* parameter sanity checking */
55 if(not resp || not msg || not userinfo)
56 return PAM_CONV_ERR;
57
58 /* allocate memory to store response */
59 response= (struct pam_response*)malloc(num_msg * sizeof(struct pam_response));
60
61 /* copy values */
62 for(int x= 0; x < num_msg; x++)
63 {
64 /* initialize to safe values */
65 response[x].resp_retcode= 0;
66 response[x].resp= 0;
67
68 /* select response based on requested output style */
69 switch(msg[x]->msg_style)
70 {
71 case PAM_PROMPT_ECHO_ON:
72 /* on memory allocation failure, auth fails */
73 response[x].resp = strdup(userinfo->name);
74 break;
75 case PAM_PROMPT_ECHO_OFF:
76 response[x].resp = strdup(userinfo->password);
77 break;
78 default:
79 free(response);
80 return PAM_CONV_ERR;
81 }
82 }
83
84 /* everything okay, set PAM response values */
85 *resp = response;
86
87 return PAM_SUCCESS;
88}
89
90class Auth_pam : public drizzled::plugin::Authentication
91{
92public:
93 Auth_pam(std::string name_arg)
95 virtual bool authenticate(const identifier::User &sctx,
96 const std::string &password)
97 {
98 int retval;
99 auth_pam_userinfo userinfo= { NULL, NULL };
100 struct pam_conv conv_info= { &auth_pam_talker, (void*)&userinfo };
101 pam_handle_t *pamh= NULL;
102
103 userinfo.name= sctx.username().c_str();
104 userinfo.password= password.c_str();
105
106 retval= pam_start("drizzle", userinfo.name, &conv_info, &pamh);
107
108 if (retval == PAM_SUCCESS)
109 retval= pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
110
111 if (retval == PAM_SUCCESS)
112 retval= pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK);
113
114 pam_end(pamh, retval);
115
116 return (retval == PAM_SUCCESS) ? true: false;
117 }
118};
119
120
121static Auth_pam *auth= NULL;
122
123static int initialize(drizzled::module::Context &context)
124{
125 auth= new Auth_pam("auth_pam");
126 context.add(auth);
127 return 0;
128}
129
130DRIZZLE_DECLARE_PLUGIN
131{
132 DRIZZLE_VERSION_ID,
133 "pam",
134 "0.1",
135 "Brian Aker",
136 N_("Authenication against system user accounts using PAM"),
137 PLUGIN_LICENSE_GPL,
138 initialize,
139 NULL,
140 NULL
141}
142DRIZZLE_DECLARE_PLUGIN_END;
A set of Session members describing the current authenticated user.
TODO: Rename this file - func.h is stupid.