4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 #include <security/pam_appl.h>
31 #include <security/pam_modules.h>
34 #include <user_attr.h>
36 #include <nss_dbdefs.h>
37 #include <security/pam_impl.h>
39 static int roleinlist();
43 * Account management module
44 * This module disallows roles for primary logins and adds special
45 * checks to allow roles for secondary logins.
50 pam_sm_acct_mgmt(pam_handle_t
*pamh
, int flags
, int argc
, const char **argv
)
53 userattr_t
*user_entry
;
58 char messages
[PAM_MAX_NUM_MSG
][PAM_MAX_MSG_SIZE
];
59 struct passwd
*pw_entry
, pwd
;
60 char buf
[NSS_BUFLEN_PASSWD
];
66 (void) pam_get_item(pamh
, PAM_USER
, (void **)&username
);
67 (void) pam_get_item(pamh
, PAM_AUSER
, (void **)&auser
);
68 (void) pam_get_item(pamh
, PAM_RHOST
, (void **)&rhost
);
70 for (i
= 0; i
< argc
; i
++) {
71 if (strcmp(argv
[i
], "allow_remote") == 0) {
73 } else if (strcmp(argv
[i
], "debug") == 0) {
76 __pam_log(LOG_AUTH
| LOG_ERR
,
77 "pam_roles:pam_sm_acct_mgmt: illegal module "
78 "option %s", argv
[i
]);
86 (void) pam_get_item(pamh
, PAM_RUSER
, (void **)&ruser
);
87 (void) pam_get_item(pamh
, PAM_SERVICE
, (void **)&service
);
88 __pam_log(LOG_AUTH
| LOG_DEBUG
, "pam_roles:pam_sm_acct_mgmt: "
89 "service = %s, allow_remote = %d, user = %s auser = %s "
90 "ruser = %s rhost = %s\n", (service
) ? service
: "not set",
91 allow_remote
, (username
) ? username
: "not set",
92 (auser
) ? auser
: "not set", (ruser
) ? ruser
: "not set",
93 (rhost
) ? rhost
: "not set");
97 return (PAM_USER_UNKNOWN
);
99 /* stop masquerades by mapping username to uid to username */
101 if ((pw_entry
= getpwnam_r(username
, &pwd
, buf
, sizeof (buf
))) == NULL
)
102 return (PAM_USER_UNKNOWN
);
103 if ((pw_entry
= getpwuid_r(pw_entry
->pw_uid
, &pwd
, buf
,
104 sizeof (buf
))) == NULL
)
105 return (PAM_USER_UNKNOWN
);
107 * If there's no user_attr entry for the primary user or it's not a
108 * role, no further checks are needed.
111 if (((user_entry
= getusernam(pw_entry
->pw_name
)) == NULL
) ||
112 ((kva_value
= kva_match((kva_t
*)user_entry
->attr
,
113 USERATTR_TYPE_KW
)) == NULL
) ||
114 ((strcmp(kva_value
, USERATTR_TYPE_NONADMIN_KW
) != 0) &&
115 (strcmp(kva_value
, USERATTR_TYPE_ADMIN_KW
) != 0))) {
116 free_userattr(user_entry
);
119 free_userattr(user_entry
);
121 /* username is a role */
123 if (strcmp(username
, pw_entry
->pw_name
) != 0) {
124 __pam_log(LOG_AUTH
| LOG_ALERT
,
125 "pam_roles:pam_sm_acct_mgmt: user name %s "
126 "maps to user id %d which is user name %s",
127 username
, pw_entry
->pw_uid
, pw_entry
->pw_name
);
131 /* Who's the user requesting the role? */
133 if (auser
!= NULL
&& *auser
!= '\0') {
134 /* authenticated requesting user */
136 user_entry
= getusernam(auser
);
138 /* user is implied by real UID */
140 if ((uid
= getuid()) == 0) {
142 * Root user_attr entry cannot have roles.
143 * Force error and deny access.
147 if ((pw_entry
= getpwuid_r(uid
, &pwd
, buf
,
148 sizeof (buf
))) == NULL
) {
149 return (PAM_USER_UNKNOWN
);
151 user_entry
= getusernam(pw_entry
->pw_name
);
155 if ((rhost
!= NULL
&& *rhost
!= '\0') &&
157 /* don't allow remote roles for this service */
159 free_userattr(user_entry
);
160 return (PAM_PERM_DENIED
);
164 * If the original user does not have a user_attr entry or isn't
165 * assigned the role being assumed, fail.
168 if ((user_entry
== NULL
) ||
169 ((kva_value
= kva_match((kva_t
*)user_entry
->attr
,
170 USERATTR_ROLES_KW
)) == NULL
) ||
171 (roleinlist(kva_value
, username
) == 0)) {
172 free_userattr(user_entry
);
173 (void) strlcpy(messages
[0], dgettext(TEXT_DOMAIN
,
174 "Roles can only be assumed by authorized users"),
175 sizeof (messages
[0]));
176 (void) __pam_display_msg(pamh
, PAM_ERROR_MSG
, 1, messages
,
178 return (PAM_PERM_DENIED
);
181 free_userattr(user_entry
);
186 roleinlist(char *list
, char *role
)
188 char *lasts
= (char *)NULL
;
189 char *rolename
= (char *)strtok_r(list
, ",", &lasts
);
192 if (strcmp(rolename
, role
) == 0)
195 rolename
= (char *)strtok_r(NULL
, ",", &lasts
);