~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/auth_pam/auth_pam.cc

  • Committer: Brian Aker
  • Date: 2008-07-13 21:20:24 UTC
  • Revision ID: brian@tangent.org-20080713212024-o6263c1vha7yxdeu
More bool removal. More cow bell!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  Sections of this where taken/modified from mod_auth_path for Apache 
3
 
*/
4
 
 
5
 
#define DRIZZLE_SERVER 1
6
 
#include <drizzled/server_includes.h>
7
 
#include <drizzled/plugin_authentication.h>
8
 
#include <security/pam_appl.h>
9
 
#ifndef __sun
10
 
#include <security/pam_misc.h>
11
 
#endif
12
 
 
13
 
typedef struct {
14
 
    const char *name;
15
 
    const char *password;
16
 
} auth_pam_userinfo;
17
 
 
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)
26
 
{
27
 
  auth_pam_userinfo *userinfo = (auth_pam_userinfo*)appdata_ptr;
28
 
  struct pam_response *response = 0;
29
 
  int x;
30
 
 
31
 
  /* parameter sanity checking */
32
 
  if(!resp || !msg || !userinfo)
33
 
    return PAM_CONV_ERR;
34
 
 
35
 
  /* allocate memory to store response */
36
 
  response= (struct pam_response*)malloc(num_msg * sizeof(struct pam_response));
37
 
  if(!response)
38
 
    return PAM_CONV_ERR;
39
 
 
40
 
  /* copy values */
41
 
  for(x= 0; x < num_msg; x++) 
42
 
  {
43
 
    /* initialize to safe values */
44
 
    response[x].resp_retcode= 0;
45
 
    response[x].resp= 0;
46
 
 
47
 
    /* select response based on requested output style */
48
 
    switch(msg[x]->msg_style) 
49
 
    {
50
 
    case PAM_PROMPT_ECHO_ON:
51
 
      /* on memory allocation failure, auth fails */
52
 
      response[x].resp = strdup(userinfo->name);
53
 
      break;
54
 
    case PAM_PROMPT_ECHO_OFF:
55
 
      response[x].resp = strdup(userinfo->password);
56
 
      break;
57
 
    default:
58
 
      if(response)
59
 
        free(response);
60
 
      return PAM_CONV_ERR;
61
 
    }
62
 
  }
63
 
 
64
 
  /* everything okay, set PAM response values */
65
 
  *resp = response;
66
 
 
67
 
  return PAM_SUCCESS;
68
 
}
69
 
 
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,
112
 
  "pam",
113
 
  "0.1",
114
 
  "Brian Aker",
115
 
  "PAM based authenication.",
116
 
  PLUGIN_LICENSE_GPL,
117
 
  initialize, /* Plugin Init */
118
 
  finalize, /* Plugin Deinit */
119
 
  NULL,   /* status variables */
120
 
  NULL,   /* system variables */
121
 
  NULL    /* config options */
122
 
}
123
 
mysql_declare_plugin_end;