pam_nologin.c revision 1.1
11.1Schristos/*-
21.1Schristos * Copyright 2001 Mark R V Murray
31.1Schristos * All rights reserved.
41.1Schristos * Copyright (c) 2001 Networks Associates Technology, Inc.
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Portions of this software were developed for the FreeBSD Project by
81.1Schristos * ThinkSec AS and NAI Labs, the Security Research Division of Network
91.1Schristos * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
101.1Schristos * ("CBOSS"), as part of the DARPA CHATS research program.
111.1Schristos *
121.1Schristos * Redistribution and use in source and binary forms, with or without
131.1Schristos * modification, are permitted provided that the following conditions
141.1Schristos * are met:
151.1Schristos * 1. Redistributions of source code must retain the above copyright
161.1Schristos *    notice, this list of conditions and the following disclaimer.
171.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
181.1Schristos *    notice, this list of conditions and the following disclaimer in the
191.1Schristos *    documentation and/or other materials provided with the distribution.
201.1Schristos * 3. The name of the author may not be used to endorse or promote
211.1Schristos *    products derived from this software without specific prior written
221.1Schristos *    permission.
231.1Schristos *
241.1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
251.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
281.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Schristos * SUCH DAMAGE.
351.1Schristos */
361.1Schristos
371.1Schristos#include <sys/cdefs.h>
381.1Schristos__FBSDID("$FreeBSD: src/lib/libpam/modules/pam_nologin/pam_nologin.c,v 1.10 2002/04/12 22:27:21 des Exp $");
391.1Schristos
401.1Schristos#include <sys/types.h>
411.1Schristos#include <sys/stat.h>
421.1Schristos#include <fcntl.h>
431.1Schristos#include <login_cap.h>
441.1Schristos#include <pwd.h>
451.1Schristos#include <stdio.h>
461.1Schristos#include <stdlib.h>
471.1Schristos#include <unistd.h>
481.1Schristos
491.1Schristos#define PAM_SM_AUTH
501.1Schristos
511.1Schristos#include <security/pam_appl.h>
521.1Schristos#include <security/pam_modules.h>
531.1Schristos#include <security/pam_mod_misc.h>
541.1Schristos
551.1Schristos#define	NOLOGIN	"/var/run/nologin"
561.1Schristos
571.1Schristosstatic char nologin_def[] = NOLOGIN;
581.1Schristos
591.1SchristosPAM_EXTERN int
601.1Schristospam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
611.1Schristos    int argc __unused, const char *argv[] __unused)
621.1Schristos{
631.1Schristos	login_cap_t *lc;
641.1Schristos	struct passwd *pwd;
651.1Schristos	struct stat st;
661.1Schristos	int retval, fd;
671.1Schristos	const char *user, *nologin;
681.1Schristos	char *mtmp;
691.1Schristos
701.1Schristos	retval = pam_get_user(pamh, &user, NULL);
711.1Schristos	if (retval != PAM_SUCCESS)
721.1Schristos		return (retval);
731.1Schristos
741.1Schristos	PAM_LOG("Got user: %s", user);
751.1Schristos
761.1Schristos	lc = login_getclass(NULL);
771.1Schristos	nologin = login_getcapstr(lc, "nologin", nologin_def, nologin_def);
781.1Schristos	login_close(lc);
791.1Schristos	lc = NULL;
801.1Schristos
811.1Schristos	fd = open(nologin, O_RDONLY, 0);
821.1Schristos	if (fd < 0)
831.1Schristos		return (PAM_SUCCESS);
841.1Schristos
851.1Schristos	PAM_LOG("Opened %s file", NOLOGIN);
861.1Schristos
871.1Schristos	pwd = getpwnam(user);
881.1Schristos	if (pwd && pwd->pw_uid == 0)
891.1Schristos		retval = PAM_SUCCESS;
901.1Schristos	else {
911.1Schristos		if (!pwd)
921.1Schristos			retval = PAM_USER_UNKNOWN;
931.1Schristos		else
941.1Schristos			retval = PAM_AUTH_ERR;
951.1Schristos	}
961.1Schristos
971.1Schristos	if (fstat(fd, &st) < 0)
981.1Schristos		return (retval);
991.1Schristos
1001.1Schristos	mtmp = malloc(st.st_size + 1);
1011.1Schristos	if (mtmp != NULL) {
1021.1Schristos		read(fd, mtmp, st.st_size);
1031.1Schristos		mtmp[st.st_size] = '\0';
1041.1Schristos		pam_error(pamh, "%s", mtmp, NULL);
1051.1Schristos		free(mtmp);
1061.1Schristos	}
1071.1Schristos
1081.1Schristos	if (retval != PAM_SUCCESS)
1091.1Schristos		PAM_VERBOSE_ERROR("Administrator refusing you: %s", NOLOGIN);
1101.1Schristos
1111.1Schristos	return (retval);
1121.1Schristos}
1131.1Schristos
1141.1SchristosPAM_EXTERN int
1151.1Schristospam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
1161.1Schristos    int argc __unused, const char *argv[] __unused)
1171.1Schristos{
1181.1Schristos
1191.1Schristos	return (PAM_SUCCESS);
1201.1Schristos}
1211.1Schristos
1221.1SchristosPAM_MODULE_ENTRY("pam_nologin");
123