1 /*
2 * pam_rhosts module
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, and the entire permission notice in its entirety,
9 * including the disclaimer of warranties.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
16 *
17 * ALTERNATIVELY, this product may be distributed under the terms of
18 * the GNU Public License, in which case the provisions of the GPL are
19 * required INSTEAD OF the above restrictions. (This clause is
20 * necessary due to a potential bad interaction between the GPL and
21 * the restrictions contained in a BSD-style copyright.)
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 #include "config.h"
36
37 #include <pwd.h>
38 #include <netdb.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <syslog.h>
42
43 #include <security/pam_modules.h>
44 #include <security/pam_modutil.h>
45 #include <security/pam_ext.h>
46 #include "pam_inline.h"
47
48 int pam_sm_authenticate (pam_handle_t *pamh, int flags, int argc,
49 const char **argv)
50 {
51 const char *luser = NULL;
52 const char *ruser = NULL, *rhost = NULL;
53 const char *opt_superuser = NULL;
54 const void *c_void;
55 int opt_debug = 0;
56 int opt_silent;
57 int as_root;
58 int retval;
59
60 opt_silent = flags & PAM_SILENT;
61
62 while (argc-- > 0) {
63 const char *str;
64
65 if (strcmp(*argv, "debug") == 0)
66 opt_debug = 1;
67 else if (strcmp (*argv, "silent") == 0 || strcmp(*argv, "suppress") == 0)
68 opt_silent = 1;
69 else if ((str = pam_str_skip_prefix(*argv, "superuser=")) != NULL)
70 opt_superuser = str;
71 else
72 pam_syslog(pamh, LOG_WARNING, "unrecognized option '%s'", *argv);
73
74 ++argv;
75 }
76
77 retval = pam_get_item (pamh, PAM_RHOST, &c_void);
78 if (retval != PAM_SUCCESS) {
79 pam_syslog(pamh, LOG_ERR, "could not get the remote host name");
80 return retval;
81 }
82 rhost = c_void;
83
84 retval = pam_get_item(pamh, PAM_RUSER, &c_void);
85 ruser = c_void;
86 if (retval != PAM_SUCCESS) {
87 pam_syslog(pamh, LOG_ERR, "could not get the remote username");
88 return retval;
89 }
90
91 retval = pam_get_user(pamh, &luser, NULL);
92 if (retval != PAM_SUCCESS) {
93 pam_syslog(pamh, LOG_NOTICE, "cannot determine local user name: %s",
94 pam_strerror(pamh, retval));
95 return retval;
96 }
97
98 if (rhost == NULL || ruser == NULL)
99 return PAM_AUTH_ERR;
100
101 if (opt_superuser && strcmp(opt_superuser, luser) == 0)
102 as_root = 1;
103 else {
104 struct passwd *lpwd;
105
106 lpwd = pam_modutil_getpwnam(pamh, luser);
107 if (lpwd == NULL) {
108 if (opt_debug)
109 /* don't print by default, could be the user's password */
110 pam_syslog(pamh, LOG_DEBUG,
111 "user '%s' unknown to this system", luser);
112 return PAM_USER_UNKNOWN;
113
114 }
115 as_root = (lpwd->pw_uid == 0);
116 }
117
118 #ifdef HAVE_RUSEROK_AF
119 retval = ruserok_af (rhost, as_root, ruser, luser, PF_UNSPEC);
120 #else
121 retval = ruserok (rhost, as_root, ruser, luser);
122 #endif
123 if (retval != 0) {
124 if (!opt_silent || opt_debug)
125 pam_syslog(pamh, LOG_WARNING, "denied access to %s@%s as %s",
126 ruser, rhost, luser);
127 return PAM_AUTH_ERR;
128 } else {
129 if (!opt_silent || opt_debug)
130 pam_syslog(pamh, LOG_NOTICE, "allowed access to %s@%s as %s",
131 ruser, rhost, luser);
132 return PAM_SUCCESS;
133 }
134 }
135
136
137 int
138 pam_sm_setcred (pam_handle_t *pamh UNUSED, int flags UNUSED,
139 int argc UNUSED, const char **argv UNUSED)
140 {
141 return PAM_SUCCESS;
142 }