~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_pam/auth_pam.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
1
/*
21
 
  Sections of this were taken/modified from mod_auth_path for Apache
22
 
  @TODO: License?
 
2
  Sections of this where taken/modified from mod_auth_path for Apache 
23
3
*/
24
4
 
25
 
#include "config.h"
26
 
 
 
5
#define DRIZZLE_SERVER 1
 
6
#include <drizzled/server_includes.h>
 
7
#include <drizzled/plugin_authentication.h>
27
8
#include <security/pam_appl.h>
28
 
#if !defined(__sun) && !defined(__FreeBSD__)
 
9
#ifndef __sun
29
10
#include <security/pam_misc.h>
30
11
#endif
31
12
 
32
 
#include "drizzled/security_context.h"
33
 
#include "drizzled/plugin/authentication.h"
34
 
 
35
 
using namespace drizzled;
36
 
 
37
13
typedef struct {
38
14
    const char *name;
39
15
    const char *password;
40
16
} auth_pam_userinfo;
41
17
 
42
 
extern "C"
43
 
int auth_pam_talker(int num_msg,
44
 
#ifdef __sun
45
 
                    struct pam_message **msg,
46
 
#else
47
 
                    const struct pam_message **msg,
48
 
#endif
49
 
                    struct pam_response **resp,
50
 
                    void *appdata_ptr);
51
 
 
52
 
int auth_pam_talker(int num_msg,
53
 
#ifdef __sun
54
 
                    struct pam_message **msg,
55
 
#else
56
 
                    const struct pam_message **msg,
57
 
#endif
58
 
                    struct pam_response **resp,
59
 
                    void *appdata_ptr)
 
18
static int auth_pam_talker(int num_msg,
 
19
#ifdef __sun
 
20
                           struct pam_message **msg,
 
21
#else
 
22
                           const struct pam_message **msg,
 
23
#endif
 
24
                           struct pam_response **resp,
 
25
                           void *appdata_ptr)
60
26
{
61
27
  auth_pam_userinfo *userinfo = (auth_pam_userinfo*)appdata_ptr;
62
28
  struct pam_response *response = 0;
72
38
    return PAM_CONV_ERR;
73
39
 
74
40
  /* copy values */
75
 
  for(x= 0; x < num_msg; x++)
 
41
  for(x= 0; x < num_msg; x++) 
76
42
  {
77
43
    /* initialize to safe values */
78
44
    response[x].resp_retcode= 0;
79
45
    response[x].resp= 0;
80
46
 
81
47
    /* select response based on requested output style */
82
 
    switch(msg[x]->msg_style)
 
48
    switch(msg[x]->msg_style) 
83
49
    {
84
50
    case PAM_PROMPT_ECHO_ON:
85
51
      /* on memory allocation failure, auth fails */
101
67
  return PAM_SUCCESS;
102
68
}
103
69
 
104
 
class Auth_pam : public drizzled::plugin::Authentication
105
 
{
106
 
public:
107
 
  Auth_pam(std::string name_arg)
108
 
    : drizzled::plugin::Authentication(name_arg) {}
109
 
  virtual bool authenticate(const SecurityContext &sctx,
110
 
                            const std::string &password)
111
 
  {
112
 
    int retval;
113
 
    auth_pam_userinfo userinfo= { NULL, NULL };
114
 
    struct pam_conv conv_info= { &auth_pam_talker, (void*)&userinfo };
115
 
    pam_handle_t *pamh= NULL;
116
 
 
117
 
    userinfo.name= sctx.getUser().c_str();
118
 
    userinfo.password= password.c_str();
119
 
 
120
 
    retval= pam_start("drizzle", userinfo.name, &conv_info, &pamh);
121
 
 
122
 
    if (retval == PAM_SUCCESS)
123
 
      retval= pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
124
 
 
125
 
    if (retval == PAM_SUCCESS)
126
 
      retval= pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK);
127
 
 
128
 
    pam_end(pamh, retval);
129
 
 
130
 
    return (retval == PAM_SUCCESS) ? true: false;
131
 
  }
132
 
};
133
 
 
134
 
 
135
 
static Auth_pam *auth= NULL;
136
 
 
137
 
static int initialize(drizzled::module::Context &context)
138
 
{
139
 
  auth= new Auth_pam("auth_pam");
140
 
  context.add(auth);
141
 
  return 0;
142
 
}
143
 
 
144
 
DRIZZLE_DECLARE_PLUGIN
145
 
{
146
 
  DRIZZLE_VERSION_ID,
 
70
static bool authenticate(THD *thd, const char *password)
 
71
{
 
72
  int retval;
 
73
  auth_pam_userinfo userinfo= { NULL, NULL };
 
74
  struct pam_conv conv_info= { &auth_pam_talker, (void*)&userinfo };
 
75
  pam_handle_t *pamh= NULL;
 
76
 
 
77
  userinfo.name= thd->main_security_ctx.user;
 
78
  userinfo.password= password;
 
79
 
 
80
  retval= pam_start("check_user", userinfo.name, &conv_info, &pamh);
 
81
 
 
82
  if (retval == PAM_SUCCESS)
 
83
    retval= pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK);
 
84
 
 
85
  if (retval == PAM_SUCCESS)
 
86
    retval= pam_acct_mgmt(pamh, PAM_DISALLOW_NULL_AUTHTOK);
 
87
 
 
88
  pam_end(pamh, retval);
 
89
 
 
90
  return (retval == PAM_SUCCESS) ? true: false;
 
91
}
 
92
 
 
93
static int initialize(void *p)
 
94
{
 
95
  authentication_st *auth= (authentication_st *)p;
 
96
  
 
97
  auth->authenticate= authenticate;
 
98
 
 
99
  return 0;
 
100
}
 
101
 
 
102
static int finalize(void *p)
 
103
{
 
104
  (void)p;
 
105
 
 
106
  return 0;
 
107
}
 
108
 
 
109
mysql_declare_plugin(auth_pam)
 
110
{
 
111
  DRIZZLE_AUTH_PLUGIN,
147
112
  "pam",
148
113
  "0.1",
149
114
  "Brian Aker",
150
115
  "PAM based authenication.",
151
116
  PLUGIN_LICENSE_GPL,
152
117
  initialize, /* Plugin Init */
 
118
  finalize, /* Plugin Deinit */
 
119
  NULL,   /* status variables */
153
120
  NULL,   /* system variables */
154
121
  NULL    /* config options */
155
122
}
156
 
DRIZZLE_DECLARE_PLUGIN_END;
 
123
mysql_declare_plugin_end;