summaryrefslogtreecommitdiff
path: root/modules/pam_issue/pam_issue.c
blob: 5b3c864b93c9a32130514ff42fe5ca1e4f6a8e71 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* pam_issue module - a simple /etc/issue parser to set PAM_USER_PROMPT
 *
 * Copyright 1999 by Ben Collins <bcollins@debian.org>
 *
 * Needs to be called before any other auth modules so we can setup the
 * user prompt before it's first used. Allows one argument option, which
 * is the full path to a file to be used for issue (uses /etc/issue as a
 * default) such as "issue=/etc/issue.telnet".
 *
 * We can also parse escapes within the the issue file (enabled by
 * default, but can be disabled with the "noesc" option). It's the exact
 * same parsing as util-linux's agetty program performs.
 *
 * Released under the GNU LGPL version 2 or later
 */

#define _GNU_SOURCE
#define _BSD_SOURCE

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <utmp.h>
#include <malloc.h>
#include <time.h>

#include <security/_pam_macros.h>

#define PAM_SM_AUTH

#include <security/pam_modules.h>

static int _user_prompt_set = 0;

static char *do_prompt (FILE *);

/* --- authentication management functions (only) --- */

PAM_EXTERN
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
                        const char **argv)
{
    int retval = PAM_SUCCESS;
    FILE *fd;
    int parse_esc = 1;
    char *prompt_tmp = NULL;
    const char *cur_prompt = NULL;
    struct stat st;
    char *issue_file = NULL;

   /* If we've already set the prompt, don't set it again */
    if(_user_prompt_set)
	return PAM_IGNORE;
    else
       /* we set this here so if we fail below, we wont get further
	  than this next time around (only one real failure) */
	_user_prompt_set = 1;

    for ( ; argc-- > 0 ; ++argv ) {
	if (!strncmp(*argv,"issue=",6)) {
	    issue_file = (char *) strdup(6+*argv);
	    if (issue_file != NULL) {
		D(("set issue_file to: %s", issue_file));
	    } else {
		D(("failed to strdup issue_file - ignored"));
		return PAM_IGNORE;
	    }
	} else if (!strcmp(*argv,"noesc")) {
	    parse_esc = 0;
	    D(("turning off escape parsing by request"));
	} else
	    D(("unknown option passed: %s", *argv));
    }

    if (issue_file == NULL)
	issue_file = strdup("/etc/issue");

    if ((fd = fopen(issue_file, "r")) != NULL) {
	int tot_size = 0;

	if (fstat(fileno(fd), &st) < 0) {
	    fclose(fd);
	    if (issue_file)
	        free(issue_file);
	    return PAM_IGNORE;
	}

	retval = pam_get_item(pamh, PAM_USER_PROMPT,
			      (const void **) &cur_prompt);
	if (retval != PAM_SUCCESS) {
	    fclose(fd);
	    if (issue_file)
	        free(issue_file);
	    return PAM_IGNORE;
	}
	if (cur_prompt == NULL) {
	    cur_prompt = "";
	}

	/* first read in the issue file */

	if (parse_esc) {
	    prompt_tmp = do_prompt(fd);
	    if (prompt_tmp == NULL) {
	        fclose(fd);
	        if (issue_file)
	           free(issue_file);
		return PAM_IGNORE;
	    }
	} else {
	    int count = 0;

	    prompt_tmp = malloc(st.st_size + 1);
	    if (prompt_tmp == NULL) {
	        fclose(fd);
	        if (issue_file)
	           free(issue_file);
		return PAM_IGNORE;
	    }
	    memset (prompt_tmp, '\0', st.st_size + 1);
	    count = fread(prompt_tmp, 1, st.st_size, fd);
	    if (count != st.st_size) {
	        fclose(fd);
		retval = PAM_IGNORE;
		goto cleanup;
	    }
	    prompt_tmp[st.st_size] = '\0';
	}

	fclose(fd);

	tot_size = strlen(prompt_tmp) + strlen(cur_prompt) + 1;

       /*
	* alloc some extra space for the original prompt
	* and postpend it to the buffer
	*/
	{
	    char *prompt_tmp_tmp = prompt_tmp;

	    prompt_tmp = realloc(prompt_tmp, tot_size + 1);
	    if (prompt_tmp == NULL) {
		prompt_tmp = prompt_tmp_tmp;
		retval = PAM_IGNORE;
		goto cleanup;
	    }
	}

	strcpy(prompt_tmp+strlen(prompt_tmp), cur_prompt);

	prompt_tmp[tot_size] = '\0';

	retval = pam_set_item(pamh, PAM_USER_PROMPT,
			      (const char *) prompt_tmp);

    cleanup:
	free(issue_file);
	free(prompt_tmp);

    } else {
	D(("could not open issue_file: %s", issue_file));
	free(issue_file);
	return PAM_IGNORE;
    }

    return retval;
}

PAM_EXTERN
int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
                   const char **argv)
{
     return PAM_IGNORE;
}

static char *do_prompt(FILE *fd)
{
    int c, size = 1024;
    char *issue;
    char buf[1024];
    struct utsname uts;

    if (fd == NULL)
	return NULL;

    issue = (char *)malloc(size);
    if (issue == NULL)
	return NULL;

    issue[0] = '\0'; /* zero this, for strcat to work on first buf */
    (void) uname(&uts);

    while ((c = getc(fd)) != EOF) {
	if (c == '\\') {
	    c = getc(fd);
	    switch (c) {
	      case 's':
		snprintf (buf, 1024, "%s", uts.sysname);
		break;
	      case 'n':
		snprintf (buf, 1024, "%s", uts.nodename);
		break;
	      case 'r':
		snprintf (buf, 1024, "%s", uts.release);
		break;
	      case 'v':
		snprintf (buf, 1024, "%s", uts.version);
		break;
	      case 'm':
		snprintf (buf, 1024, "%s", uts.machine);
		break;
	      case 'o':
		{
		    char domainname[256];

		    if (getdomainname(domainname, sizeof(domainname)) == -1)
		      domainname[0] = '\0';
		    else
		      domainname[sizeof(domainname)-1] = '\0';
		    snprintf (buf, 1024, "%s", domainname);
		}
		break;

	      case 'd':
	      case 't':
		{
		    const char *weekday[] = {
			"Sun", "Mon", "Tue", "Wed", "Thu",
			"Fri", "Sat" };
		    const char *month[] = {
			"Jan", "Feb", "Mar", "Apr", "May",
			"Jun", "Jul", "Aug", "Sep", "Oct",
			"Nov", "Dec" };
		    time_t now;
		    struct tm *tm;

		    (void) time (&now);
		    tm = localtime(&now);

		    if (c == 'd')
			snprintf (buf, 1024, "%s %s %d  %d",
				weekday[tm->tm_wday], month[tm->tm_mon],
				tm->tm_mday,
				tm->tm_year + 1900);
		    else
			snprintf (buf, 1024, "%02d:%02d:%02d",
				tm->tm_hour, tm->tm_min, tm->tm_sec);
		}
		break;
	      case 'l':
		{
		    char *ttyn = ttyname(1);
		    if (!strncmp(ttyn, "/dev/", 5))
			ttyn += 5;
		    snprintf (buf, 1024, "%s", ttyn);
		}
		break;
	      case 'u':
	      case 'U':
		{
		    int users = 0;
		    struct utmp *ut;
		    setutent();
		    while ((ut = getutent()))
			if (ut->ut_type == USER_PROCESS)
			users++;
		    endutent();
		    if (c == 'U')
			snprintf (buf, 1024, "%d %s", users,
			          (users == 1) ? "user" : "users");
		    else
			snprintf (buf, 1024, "%d", users);
		    break;
		}
	      default:
		buf[0] = c; buf[1] = '\0';
	    }
	    if ((strlen(issue) + strlen(buf)) < size + 1) {
		char *old_issue = issue;

		size += strlen(buf) + 1;
		issue = (char *) realloc (issue, size);
		if (issue == NULL) {
		    free(old_issue);
		    return NULL;
		}
	    }
	    strcat(issue, buf);
	} else {
	    buf[0] = c; buf[1] = '\0';
	    if ((strlen(issue) + strlen(buf)) < size + 1) {
		char *old_issue = issue;

		size += strlen(buf) + 1;
		issue = (char *) realloc (issue, size);
		if (issue == NULL) {
		    free(old_issue);
		    return NULL;
		}
	    }
	    strcat(issue, buf);
	}
    }

    return issue;
}

#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_issue_modstruct = {
     "pam_issue",
     pam_sm_authenticate,
     pam_sm_setcred,
     NULL,
     NULL,
     NULL,
     NULL,
};

#endif

/* end of module definition */