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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Copyright (c) 2005, 2006 Thorsten Kukuk <kukuk@suse.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* ALTERNATIVELY, this product may be distributed under the terms of
* the GNU Public License, in which case the provisions of the GPL are
* required INSTEAD OF the above restrictions. (This clause is
* necessary due to a potential bad interaction between the GPL and
* the restrictions contained in a BSD-style copyright.)
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif
#include <security/pam_modules.h>
#include <security/pam_modutil.h>
#include <security/_pam_macros.h>
#include <security/pam_ext.h>
#include "pam_inline.h"
static int
replace_and_print (pam_handle_t *pamh, const char *mesg)
{
char *output;
size_t length = strlen (mesg) + PAM_MAX_MSG_SIZE;
char myhostname[HOST_NAME_MAX+1];
const void *str = NULL;
const char *p, *q;
int item;
size_t len;
output = malloc (length);
if (output == NULL)
{
pam_syslog (pamh, LOG_CRIT, "running out of memory");
return PAM_BUF_ERR;
}
for (p = mesg, len = 0; *p != '\0' && len < length - 1; ++p)
{
if (*p != '%' || p[1] == '\0')
{
output[len++] = *p;
continue;
}
switch (*++p)
{
case 'H':
item = PAM_RHOST;
break;
case 'h':
item = -2; /* aka PAM_LOCALHOST */
break;
case 's':
item = PAM_SERVICE;
break;
case 't':
item = PAM_TTY;
break;
case 'U':
item = PAM_RUSER;
break;
case 'u':
item = PAM_USER;
break;
default:
output[len++] = *p;
continue;
}
if (item == -2)
{
if (gethostname (myhostname, sizeof (myhostname)) == -1)
str = NULL;
else
str = &myhostname;
}
else
{
if (pam_get_item (pamh, item, &str) != PAM_SUCCESS)
str = NULL;
}
if (str == NULL)
str = "(null)";
for (q = str; *q != '\0' && len < length - 1; ++q)
output[len++] = *q;
}
output[len] = '\0';
pam_info (pamh, "%s", output);
free (output);
return PAM_SUCCESS;
}
static int
pam_echo (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int fd;
int orig_argc = argc;
const char **orig_argv = argv;
const char *file = NULL;
int retval;
if (flags & PAM_SILENT)
return PAM_IGNORE;
for (; argc-- > 0; ++argv)
{
const char *str = pam_str_skip_prefix(*argv, "file=");
if (str != NULL)
file = str;
}
/* No file= option, use argument for output. */
if (file == NULL || file[0] == '\0')
{
char msg[PAM_MAX_MSG_SIZE];
const char *p;
int i;
size_t len;
for (i = 0, len = 0; i < orig_argc && len < sizeof (msg) - 1; ++i)
{
if (i > 0)
msg[len++] = ' ';
for (p = orig_argv[i]; *p != '\0' && len < sizeof(msg) - 1; ++p)
msg[len++] = *p;
}
msg[len] = '\0';
retval = replace_and_print (pamh, msg);
}
else if ((fd = open (file, O_RDONLY, 0)) >= 0)
{
char *mtmp = NULL;
struct stat st;
/* load file into message buffer. */
if ((fstat (fd, &st) < 0) || !st.st_size)
{
close (fd);
return PAM_IGNORE;
}
mtmp = malloc (st.st_size + 1);
if (!mtmp)
{
close (fd);
return PAM_BUF_ERR;
}
if (pam_modutil_read (fd, mtmp, st.st_size) == -1)
{
pam_syslog (pamh, LOG_ERR, "Error while reading %s: %m", file);
free (mtmp);
close (fd);
return PAM_IGNORE;
}
if (mtmp[st.st_size - 1] == '\n')
mtmp[st.st_size - 1] = '\0';
else
mtmp[st.st_size] = '\0';
close (fd);
retval = replace_and_print (pamh, mtmp);
free (mtmp);
}
else
{
pam_syslog (pamh, LOG_ERR, "Cannot open %s: %m", file);
retval = PAM_IGNORE;
}
return retval;
}
int
pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
return pam_echo (pamh, flags, argc, argv);
}
int
pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
int argc UNUSED, const char **argv UNUSED)
{
return PAM_IGNORE;
}
int
pam_sm_acct_mgmt (pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
return pam_echo (pamh, flags, argc, argv);
}
int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
return pam_echo (pamh, flags, argc, argv);
}
int
pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
int argc UNUSED, const char **argv UNUSED)
{
return PAM_IGNORE;
}
int
pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc,
const char **argv)
{
if (flags & PAM_PRELIM_CHECK)
return pam_echo (pamh, flags, argc, argv);
else
return PAM_IGNORE;
}
|