1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
* Copyright (C) 2000 Eazel, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Ramiro Estrugo <ramiro@eazel.com>
*/
/* nautilus-authenticate-pam.c - Use PAM to authenticate a user.
*/
#include <config.h>
#include "nautilus-authenticate.h"
#include <security/pam_appl.h>
#include <security/pam_misc.h>
typedef struct _PamConvData
{
char *username;
char *password;
} PamConvData;
static int pam_conversion_func (int num_msg,
const struct pam_message **msg,
struct pam_response **response,
void *appdata_ptr)
{
PamConvData * pdata = (PamConvData *) appdata_ptr;
struct pam_response * reply =
(struct pam_response *) malloc (sizeof (struct pam_response) * num_msg);
g_assert (pdata);
g_assert (reply);
if (reply) {
int replies;
for (replies = 0; replies < num_msg; replies++) {
switch (msg[replies]->msg_style) {
case PAM_PROMPT_ECHO_ON:
reply[replies].resp_retcode = PAM_SUCCESS;
reply[replies].resp = strdup (pdata->username);
/* PAM frees resp */
break;
case PAM_PROMPT_ECHO_OFF:
reply[replies].resp_retcode = PAM_SUCCESS;
reply[replies].resp = strdup (pdata->password);
/* PAM frees resp */
break;
case PAM_TEXT_INFO:
/* nothing */
case PAM_ERROR_MSG:
/* Ignore */
reply[replies].resp_retcode = PAM_SUCCESS;
reply[replies].resp = NULL;
break;
default:
/* Huh? */
free (reply);
reply=NULL;
return PAM_CONV_ERR;
}
}
if (reply)
*response = reply;
return PAM_SUCCESS;
}
return PAM_CONV_ERR;
}
gboolean
nautilus_authenticate_authenticate(const char *username,
const char *password)
{
char * username_copy = g_strdup(username);
char * password_copy = g_strdup(password);
gboolean rv = FALSE;
pam_handle_t * pam_handle = NULL;
struct pam_conv pam_conv_data;
static PamConvData client_data;
client_data.username = username_copy;
client_data.password = password_copy;
/* Setup the pam conversion structure */
pam_conv_data.conv = pam_conversion_func;
pam_conv_data.appdata_ptr = (void *) &client_data;
/* Start pam */
if (pam_start("su", username_copy, &pam_conv_data, &pam_handle) == PAM_SUCCESS) {
/* Attempt auth */
if (pam_authenticate(pam_handle, PAM_SILENT) == PAM_SUCCESS) {
/* Authentication worked */
pam_end (pam_handle, PAM_SUCCESS);
rv = TRUE;
}
}
if (!rv)
pam_end (pam_handle, 0);
g_free (username_copy);
g_free (password_copy);
return rv;
}
|