summaryrefslogtreecommitdiff
path: root/modules/pam_nologin/pam_nologin.c
blob: 570d9dc3cbb9ddc65c5ca451f16e1a7f7845941f (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
/* pam_nologin module */

/*
 * $Id: pam_nologin.c,v 1.11 2005/09/22 22:16:02 ldv Exp $
 *
 * Written by Michael K. Johnson <johnsonm@redhat.com> 1996/10/24
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <pwd.h>

#include <security/_pam_macros.h>
/*
 * here, we make a definition for the externally accessible function
 * in this file (this definition is required for static a module
 * but strongly encouraged generally) it is used to instruct the
 * modules include file to define the function prototypes.
 */

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT

#include <security/pam_modules.h>
#include <security/pam_modutil.h>
#include <security/pam_ext.h>

/*
 * parse some command line options
 */
struct opt_s {
    int retval_when_nofile;
    const char *nologin_file;
};

static void
parse_args(pam_handle_t *pamh, int argc, const char **argv, struct opt_s *opts)
{
    int i;

    memset(opts, 0, sizeof(*opts));

    opts->retval_when_nofile = PAM_IGNORE;
    opts->nologin_file = "/etc/nologin";

    for (i=0; i<argc; ++i) {
	if (!strcmp("successok", argv[i])) {
	    opts->retval_when_nofile = PAM_SUCCESS;
	} else if (!strncmp("file=", argv[i], 5)) {
	    opts->nologin_file = argv[i] + 5;
	} else {
	    pam_syslog(pamh, LOG_ERR, "unknown option: %s", argv[i]);
	}
    }
}

/*
 * do the meat of the work for this module
 */

static int perform_check(pam_handle_t *pamh, struct opt_s *opts)
{
    const char *username;
    int retval = opts->retval_when_nofile;
    int fd;

    if ((pam_get_user(pamh, &username, NULL) != PAM_SUCCESS) || !username) {
	pam_syslog(pamh, LOG_WARNING, "cannot determine username");
	return PAM_USER_UNKNOWN;
    }

    if ((fd = open(opts->nologin_file, O_RDONLY, 0)) >= 0) {

	char *mtmp=NULL;
	int msg_style = PAM_TEXT_INFO;
	struct passwd *user_pwd;
	struct stat st;

	user_pwd = pam_modutil_getpwnam(pamh, username);
	if (user_pwd == NULL) {
	    retval = PAM_USER_UNKNOWN;
	    msg_style = PAM_ERROR_MSG;
	} else if (user_pwd->pw_uid) {
	    retval = PAM_AUTH_ERR;
	    msg_style = PAM_ERROR_MSG;
	}

	/* fill in message buffer with contents of /etc/nologin */
	if (fstat(fd, &st) < 0)  {
	    /* give up trying to display message */
	    goto clean_up_fd;
	}

	mtmp = malloc(st.st_size+1);
	if (!mtmp) {
	    pam_syslog(pamh, LOG_ERR, "out of memory");
	    retval = PAM_BUF_ERR;
	    goto clean_up_fd;
	}

	if (pam_modutil_read(fd, mtmp, st.st_size) == st.st_size) {
		mtmp[st.st_size] = '\0';
		(void) pam_prompt (pamh, msg_style, NULL, "%s", mtmp);
	}
	else
	    retval = PAM_SYSTEM_ERR;

	free(mtmp);

    clean_up_fd:

	close(fd);
    }

    return retval;
}

/* --- authentication management functions --- */

PAM_EXTERN int
pam_sm_authenticate (pam_handle_t *pamh, int flags UNUSED,
		     int argc, const char **argv)
{
    struct opt_s opts;

    parse_args(pamh, argc, argv, &opts);

    return perform_check(pamh, &opts);
}

PAM_EXTERN int
pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
		int argc, const char **argv)
{
    struct opt_s opts;

    parse_args(pamh, argc, argv, &opts);

    return opts.retval_when_nofile;
}

/* --- account management function --- */

PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags UNUSED,
		 int argc, const char **argv)
{
    struct opt_s opts;

    parse_args(pamh, argc, argv, &opts);

    return perform_check(pamh, &opts);
}


#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_nologin_modstruct = {
     "pam_nologin",
     pam_sm_authenticate,
     pam_sm_setcred,
     pam_sm_acct_mgmt,
     NULL,
     NULL,
     NULL,
};

#endif /* PAM_STATIC */

/* end of module definition */