xref: /netbsd-src/lib/libpam/modules/pam_exec/pam_exec.c (revision e2fa600c84b0acdbcca6dda54442d5ea5eddb655)
1*e2fa600cSnia /*	$NetBSD: pam_exec.c,v 1.8 2021/10/30 11:34:59 nia Exp $	*/
2e7d22a2eSchristos 
36f11bdf1Schristos /*-
46f11bdf1Schristos  * Copyright (c) 2001,2003 Networks Associates Technology, Inc.
56f11bdf1Schristos  * All rights reserved.
66f11bdf1Schristos  *
76f11bdf1Schristos  * This software was developed for the FreeBSD Project by ThinkSec AS and
86f11bdf1Schristos  * NAI Labs, the Security Research Division of Network Associates, Inc.
96f11bdf1Schristos  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
106f11bdf1Schristos  * DARPA CHATS research program.
116f11bdf1Schristos  *
126f11bdf1Schristos  * Redistribution and use in source and binary forms, with or without
136f11bdf1Schristos  * modification, are permitted provided that the following conditions
146f11bdf1Schristos  * are met:
156f11bdf1Schristos  * 1. Redistributions of source code must retain the above copyright
166f11bdf1Schristos  *    notice, this list of conditions and the following disclaimer.
176f11bdf1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
186f11bdf1Schristos  *    notice, this list of conditions and the following disclaimer in the
196f11bdf1Schristos  *    documentation and/or other materials provided with the distribution.
206f11bdf1Schristos  * 3. The name of the author may not be used to endorse or promote
216f11bdf1Schristos  *    products derived from this software without specific prior written
226f11bdf1Schristos  *    permission.
236f11bdf1Schristos  *
246f11bdf1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
256f11bdf1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
266f11bdf1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
276f11bdf1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
286f11bdf1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
296f11bdf1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
306f11bdf1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
316f11bdf1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
326f11bdf1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
336f11bdf1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
346f11bdf1Schristos  * SUCH DAMAGE.
356f11bdf1Schristos  */
366f11bdf1Schristos 
376f11bdf1Schristos #include <sys/cdefs.h>
38e7d22a2eSchristos #ifdef __FreeBSD__
397331ee20Sthorpej __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_exec/pam_exec.c,v 1.4 2005/02/01 10:37:07 des Exp $");
40e7d22a2eSchristos #else
41*e2fa600cSnia __RCSID("$NetBSD: pam_exec.c,v 1.8 2021/10/30 11:34:59 nia Exp $");
42e7d22a2eSchristos #endif
436f11bdf1Schristos 
446f11bdf1Schristos #include <sys/types.h>
456f11bdf1Schristos #include <sys/wait.h>
466f11bdf1Schristos 
476f11bdf1Schristos #include <errno.h>
486f11bdf1Schristos #include <stdlib.h>
497331ee20Sthorpej #include <stdio.h>
506f11bdf1Schristos #include <string.h>
516f11bdf1Schristos #include <unistd.h>
526f11bdf1Schristos 
536f11bdf1Schristos #include <security/pam_appl.h>
546f11bdf1Schristos #include <security/pam_modules.h>
556f11bdf1Schristos #include <security/openpam.h>
566f11bdf1Schristos 
577331ee20Sthorpej #define ENV_ITEM(n) { (n), #n }
587331ee20Sthorpej static struct {
597331ee20Sthorpej 	int item;
607331ee20Sthorpej 	const char *name;
617331ee20Sthorpej } env_items[] = {
627331ee20Sthorpej 	ENV_ITEM(PAM_SERVICE),
637331ee20Sthorpej 	ENV_ITEM(PAM_USER),
647331ee20Sthorpej 	ENV_ITEM(PAM_TTY),
657331ee20Sthorpej 	ENV_ITEM(PAM_RHOST),
667331ee20Sthorpej 	ENV_ITEM(PAM_RUSER),
677331ee20Sthorpej };
687331ee20Sthorpej 
696f11bdf1Schristos static int
_pam_exec(pam_handle_t * pamh __unused,int flags __unused,int argc,const char * argv[])706f11bdf1Schristos _pam_exec(pam_handle_t *pamh __unused, int flags __unused,
716f11bdf1Schristos     int argc, const char *argv[])
726f11bdf1Schristos {
739778b180Schristos 	size_t envlen, i, nitems;
749778b180Schristos 	int pam_err, status;
75*e2fa600cSnia 	char **envlist;
76d7b34675Schristos 	volatile int childerr;
776f11bdf1Schristos 	pid_t pid;
786f11bdf1Schristos 
796f11bdf1Schristos 	if (argc < 1)
806f11bdf1Schristos 		return (PAM_SERVICE_ERR);
816f11bdf1Schristos 
826f11bdf1Schristos 	/*
836f11bdf1Schristos 	 * XXX For additional credit, divert child's stdin/stdout/stderr
846f11bdf1Schristos 	 * to the conversation function.
856f11bdf1Schristos 	 */
867331ee20Sthorpej 
877331ee20Sthorpej 	/*
887331ee20Sthorpej 	 * Set up the child's environment list.  It consists of the PAM
897331ee20Sthorpej 	 * environment, plus a few hand-picked PAM items.
907331ee20Sthorpej 	 */
916f11bdf1Schristos 	envlist = pam_getenvlist(pamh);
927331ee20Sthorpej 	for (envlen = 0; envlist[envlen] != NULL; ++envlen)
937331ee20Sthorpej 		/* nothing */ ;
947331ee20Sthorpej 	nitems = sizeof(env_items) / sizeof(*env_items);
95*e2fa600cSnia 	if (reallocarr(&envlist, envlen + nitems + 1, sizeof(*envlist)) != 0) {
967331ee20Sthorpej 		openpam_free_envlist(envlist);
977331ee20Sthorpej 		return (PAM_BUF_ERR);
987331ee20Sthorpej 	}
997331ee20Sthorpej 	for (i = 0; i < nitems; ++i) {
1007331ee20Sthorpej 		const void *item;
1017331ee20Sthorpej 		char *envstr;
1027331ee20Sthorpej 
1037331ee20Sthorpej 		pam_err = pam_get_item(pamh, env_items[i].item, &item);
1047331ee20Sthorpej 		if (pam_err != PAM_SUCCESS || item == NULL)
1057331ee20Sthorpej 			continue;
1067331ee20Sthorpej 		asprintf(&envstr, "%s=%s", env_items[i].name,
1077331ee20Sthorpej 		    (const char *)item);
1087331ee20Sthorpej 		if (envstr == NULL) {
1097331ee20Sthorpej 			openpam_free_envlist(envlist);
1107331ee20Sthorpej 			return (PAM_BUF_ERR);
1117331ee20Sthorpej 		}
1127331ee20Sthorpej 		envlist[envlen++] = envstr;
1137331ee20Sthorpej 		envlist[envlen] = NULL;
1147331ee20Sthorpej 	}
1157331ee20Sthorpej 
1167331ee20Sthorpej 	/*
1177331ee20Sthorpej 	 * Fork and run the command.  By using vfork() instead of fork(),
1187331ee20Sthorpej 	 * we can distinguish between an execve() failure and a non-zero
1197331ee20Sthorpej 	 * exit code from the command.
1207331ee20Sthorpej 	 */
1216f11bdf1Schristos 	childerr = 0;
1226f11bdf1Schristos 	if ((pid = vfork()) == 0) {
123e7d22a2eSchristos 		/*LINTED const cast*/
124e7d22a2eSchristos 		execve(argv[0], (char **)__UNCONST(argv), envlist);
1256f11bdf1Schristos 		childerr = errno;
1266f11bdf1Schristos 		_exit(1);
1276f11bdf1Schristos 	}
1287331ee20Sthorpej 	openpam_free_envlist(envlist);
1296f11bdf1Schristos 	if (pid == -1) {
1301b695acdSchristos 		openpam_log(PAM_LOG_ERROR, "vfork(): %s", strerror(errno));
1316f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1326f11bdf1Schristos 	}
1336f11bdf1Schristos 	if (waitpid(pid, &status, 0) == -1) {
1341b695acdSchristos 		openpam_log(PAM_LOG_ERROR, "waitpid(): %s", strerror(errno));
1356f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1366f11bdf1Schristos 	}
1376f11bdf1Schristos 	if (childerr != 0) {
1381b695acdSchristos 		openpam_log(PAM_LOG_ERROR, "execve(): %s", strerror(errno));
1396f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1406f11bdf1Schristos 	}
1416f11bdf1Schristos 	if (WIFSIGNALED(status)) {
1426f11bdf1Schristos 		openpam_log(PAM_LOG_ERROR, "%s caught signal %d%s",
1436f11bdf1Schristos 		    argv[0], WTERMSIG(status),
1446f11bdf1Schristos 		    WCOREDUMP(status) ? " (core dumped)" : "");
1456f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1466f11bdf1Schristos 	}
1476f11bdf1Schristos 	if (!WIFEXITED(status)) {
1486f11bdf1Schristos 		openpam_log(PAM_LOG_ERROR, "unknown status 0x%x", status);
1496f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1506f11bdf1Schristos 	}
1516f11bdf1Schristos 	if (WEXITSTATUS(status) != 0) {
1526f11bdf1Schristos 		openpam_log(PAM_LOG_ERROR, "%s returned code %d",
1536f11bdf1Schristos 		    argv[0], WEXITSTATUS(status));
1546f11bdf1Schristos 		return (PAM_SYSTEM_ERR);
1556f11bdf1Schristos 	}
1566f11bdf1Schristos 	return (PAM_SUCCESS);
1576f11bdf1Schristos }
1586f11bdf1Schristos 
1596f11bdf1Schristos PAM_EXTERN int
pam_sm_authenticate(pam_handle_t * pamh,int flags,int argc,const char * argv[])1606f11bdf1Schristos pam_sm_authenticate(pam_handle_t *pamh, int flags,
1616f11bdf1Schristos     int argc, const char *argv[])
1626f11bdf1Schristos {
1636f11bdf1Schristos 
1646f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
1656f11bdf1Schristos }
1666f11bdf1Schristos 
1676f11bdf1Schristos PAM_EXTERN int
pam_sm_setcred(pam_handle_t * pamh,int flags,int argc,const char * argv[])1686f11bdf1Schristos pam_sm_setcred(pam_handle_t *pamh, int flags,
1696f11bdf1Schristos     int argc, const char *argv[])
1706f11bdf1Schristos {
1716f11bdf1Schristos 
1726f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
1736f11bdf1Schristos }
1746f11bdf1Schristos 
1756f11bdf1Schristos PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags,int argc,const char * argv[])1766f11bdf1Schristos pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
1776f11bdf1Schristos     int argc, const char *argv[])
1786f11bdf1Schristos {
1796f11bdf1Schristos 
1806f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
1816f11bdf1Schristos }
1826f11bdf1Schristos 
1836f11bdf1Schristos PAM_EXTERN int
pam_sm_open_session(pam_handle_t * pamh,int flags,int argc,const char * argv[])1846f11bdf1Schristos pam_sm_open_session(pam_handle_t *pamh, int flags,
1856f11bdf1Schristos     int argc, const char *argv[])
1866f11bdf1Schristos {
1876f11bdf1Schristos 
1886f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
1896f11bdf1Schristos }
1906f11bdf1Schristos 
1916f11bdf1Schristos PAM_EXTERN int
pam_sm_close_session(pam_handle_t * pamh,int flags,int argc,const char * argv[])1926f11bdf1Schristos pam_sm_close_session(pam_handle_t *pamh, int flags,
1936f11bdf1Schristos     int argc, const char *argv[])
1946f11bdf1Schristos {
1956f11bdf1Schristos 
1966f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
1976f11bdf1Schristos }
1986f11bdf1Schristos 
1996f11bdf1Schristos PAM_EXTERN int
pam_sm_chauthtok(pam_handle_t * pamh,int flags,int argc,const char * argv[])2006f11bdf1Schristos pam_sm_chauthtok(pam_handle_t *pamh, int flags,
2016f11bdf1Schristos     int argc, const char *argv[])
2026f11bdf1Schristos {
2036f11bdf1Schristos 
2046f11bdf1Schristos 	return (_pam_exec(pamh, flags, argc, argv));
2056f11bdf1Schristos }
2066f11bdf1Schristos 
2076f11bdf1Schristos PAM_MODULE_ENTRY("pam_exec");
208