17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53bfb48feSsemery * Common Development and Distribution License (the "License").
63bfb48feSsemery * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*8ce3ffdfSPeter Shoults * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
277c478bd9Sstevel@tonic-gate #include <pwd.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <malloc.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
34505d05c7Sgtb #include <errno.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "utils.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate extern const char *error_message(long);
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /* ******************************************************************** */
417c478bd9Sstevel@tonic-gate /* */
427c478bd9Sstevel@tonic-gate /* Utilities Functions */
437c478bd9Sstevel@tonic-gate /* */
447c478bd9Sstevel@tonic-gate /* ******************************************************************** */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * get_pw_uid():
487c478bd9Sstevel@tonic-gate * To get the uid from the passwd entry for specified user
497c478bd9Sstevel@tonic-gate * It returns 0 if the user can't be found, otherwise returns 1.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate int
get_pw_uid(char * user,uid_t * uid)527c478bd9Sstevel@tonic-gate get_pw_uid(char *user, uid_t *uid)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate struct passwd sp;
557c478bd9Sstevel@tonic-gate char buffer[1024];
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate if (getpwnam_r(user, &sp, buffer, sizeof (buffer)) == NULL) {
587c478bd9Sstevel@tonic-gate return (0);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate *uid = sp.pw_uid;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate return (1);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * get_pw_gid():
687c478bd9Sstevel@tonic-gate * To get the gid from the passwd entry for specified user
697c478bd9Sstevel@tonic-gate * It returns 0 if the user can't be found, otherwise returns 1.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate int
get_pw_gid(char * user,gid_t * gid)727c478bd9Sstevel@tonic-gate get_pw_gid(char *user, gid_t *gid)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate struct passwd sp;
757c478bd9Sstevel@tonic-gate char buffer[1024];
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate if (getpwnam_r(user, &sp, buffer, sizeof (buffer)) == NULL) {
787c478bd9Sstevel@tonic-gate return (0);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate *gid = sp.pw_gid;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate return (1);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * get_kmd_kuser():
897c478bd9Sstevel@tonic-gate * To get the kerberos user name for the specified user.
907c478bd9Sstevel@tonic-gate * Assumes that the kuser string is allocated. It will be
917c478bd9Sstevel@tonic-gate * overwritten. This saves us having to deal will allocating
927c478bd9Sstevel@tonic-gate * and freeing the kuser string.
937c478bd9Sstevel@tonic-gate *
947c478bd9Sstevel@tonic-gate * RFC 1510 does not mention how to handle mixed case domainnames
957c478bd9Sstevel@tonic-gate * while constructing client principals. So we will follow the same
967c478bd9Sstevel@tonic-gate * procedure as for server principals and lowercase the domainname.
977c478bd9Sstevel@tonic-gate *
987c478bd9Sstevel@tonic-gate * Returns:
997c478bd9Sstevel@tonic-gate * PAM_BUF_ERR - if there is an error from krb5_sname_to_principal(),
1007c478bd9Sstevel@tonic-gate * or krb5_unparse_name()
1017c478bd9Sstevel@tonic-gate * 0 - if there was no error
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate int
get_kmd_kuser(krb5_context kcontext,const char * user,char * kuser,int length)1047c478bd9Sstevel@tonic-gate get_kmd_kuser(krb5_context kcontext, const char *user, char *kuser, int length)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate if (strcmp(user, ROOT_UNAME) == 0) {
1077c478bd9Sstevel@tonic-gate krb5_principal princ;
1087c478bd9Sstevel@tonic-gate char *name, *princname, *lasts;
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (krb5_sname_to_principal(kcontext, NULL, ROOT_UNAME,
1117c478bd9Sstevel@tonic-gate KRB5_NT_SRV_HST, &princ)) {
1127c478bd9Sstevel@tonic-gate return (PAM_BUF_ERR);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate if (krb5_unparse_name(kcontext, princ, &princname)) {
1157c478bd9Sstevel@tonic-gate krb5_free_principal(kcontext, princ);
1167c478bd9Sstevel@tonic-gate return (PAM_BUF_ERR);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate /* just interested in princ name before the @REALM part */
1197c478bd9Sstevel@tonic-gate if ((name = strtok_r(princname, "@", &lasts)) == NULL) {
1207c478bd9Sstevel@tonic-gate krb5_free_principal(kcontext, princ);
1217c478bd9Sstevel@tonic-gate free(princname);
1227c478bd9Sstevel@tonic-gate return (PAM_BUF_ERR);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate if (strlcpy(kuser, name, length) >= length) {
1257c478bd9Sstevel@tonic-gate krb5_free_principal(kcontext, princ);
1267c478bd9Sstevel@tonic-gate free(princname);
1277c478bd9Sstevel@tonic-gate return (PAM_BUF_ERR);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate krb5_free_principal(kcontext, princ);
1307c478bd9Sstevel@tonic-gate free(princname);
1317c478bd9Sstevel@tonic-gate } else {
1327c478bd9Sstevel@tonic-gate if (strlcpy(kuser, user, length) >= length) {
1337c478bd9Sstevel@tonic-gate return (PAM_BUF_ERR);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate return (0);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate * return true (1) if the user's key is in the (default) keytab
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate int
key_in_keytab(const char * user,int debug)1437c478bd9Sstevel@tonic-gate key_in_keytab(const char *user, int debug)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate krb5_keytab kt_handle;
1467c478bd9Sstevel@tonic-gate krb5_keytab_entry kt_ent;
1477c478bd9Sstevel@tonic-gate char *whoami = "key_in_keytab";
1487c478bd9Sstevel@tonic-gate krb5_error_code retval = 0;
1497c478bd9Sstevel@tonic-gate krb5_error_code code = 0;
1507c478bd9Sstevel@tonic-gate krb5_context kcontext = NULL;
1517c478bd9Sstevel@tonic-gate krb5_principal princ = NULL;
1527c478bd9Sstevel@tonic-gate char kuser[2*MAXHOSTNAMELEN];
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if (debug)
1563bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
1577c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): start for user '%s'",
1587c478bd9Sstevel@tonic-gate whoami, user ? user : "<null>");
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate if (!user)
1617c478bd9Sstevel@tonic-gate return (retval);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /* need to free context with krb5_free_context */
164*8ce3ffdfSPeter Shoults if (code = krb5_init_secure_context(&kcontext)) {
1657c478bd9Sstevel@tonic-gate if (debug)
1663bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
1677c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): Error initializing "
1687c478bd9Sstevel@tonic-gate "krb5: %s", whoami,
1697c478bd9Sstevel@tonic-gate error_message(code));
1707c478bd9Sstevel@tonic-gate return (retval);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate if ((code = get_kmd_kuser(kcontext, (const char *)user, kuser,
1747c478bd9Sstevel@tonic-gate 2*MAXHOSTNAMELEN)) != 0) {
1757c478bd9Sstevel@tonic-gate goto out;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /* need to free princ with krb5_free_principal */
1797c478bd9Sstevel@tonic-gate if ((code = krb5_parse_name(kcontext, kuser, &princ)) != 0) {
1807c478bd9Sstevel@tonic-gate if (debug)
1813bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
1827c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): can't parse name (%s)",
1837c478bd9Sstevel@tonic-gate whoami, error_message(code));
1847c478bd9Sstevel@tonic-gate goto out;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /* need to close keytab handle with krb5_kt_close */
1887c478bd9Sstevel@tonic-gate if ((code = krb5_kt_default(kcontext, &kt_handle))) {
1897c478bd9Sstevel@tonic-gate if (debug)
1903bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
1917c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): krb5_kt_default failed (%s)",
1927c478bd9Sstevel@tonic-gate whoami, error_message(code));
1937c478bd9Sstevel@tonic-gate goto out;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate code = krb5_kt_get_entry(kcontext, kt_handle, princ, 0, 0, &kt_ent);
1977c478bd9Sstevel@tonic-gate if (code != 0) {
1987c478bd9Sstevel@tonic-gate if (code == ENOENT) {
1997c478bd9Sstevel@tonic-gate if (debug)
2003bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
2017c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): "
2027c478bd9Sstevel@tonic-gate "Keytab does not exist",
2037c478bd9Sstevel@tonic-gate whoami);
2047c478bd9Sstevel@tonic-gate } else if (code == KRB5_KT_NOTFOUND) {
2057c478bd9Sstevel@tonic-gate if (debug)
2063bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
2077c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): "
2087c478bd9Sstevel@tonic-gate "No entry for principal "
2097c478bd9Sstevel@tonic-gate "'%s' exists in keytab",
2107c478bd9Sstevel@tonic-gate whoami, kuser);
2117c478bd9Sstevel@tonic-gate } else {
2127c478bd9Sstevel@tonic-gate if (debug)
2133bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
2147c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): "
2157c478bd9Sstevel@tonic-gate "krb5_kt_get_entry failed (%s)",
2167c478bd9Sstevel@tonic-gate whoami, error_message(code));
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate } else { /* Key found in keytab, return success */
2197c478bd9Sstevel@tonic-gate (void) krb5_kt_free_entry(kcontext, &kt_ent);
2207c478bd9Sstevel@tonic-gate if (debug)
2213bfb48feSsemery __pam_log(LOG_AUTH | LOG_DEBUG,
2227c478bd9Sstevel@tonic-gate "PAM-KRB5 (%s): "
2237c478bd9Sstevel@tonic-gate "keytab entry for '%s' found",
2247c478bd9Sstevel@tonic-gate whoami, user);
2257c478bd9Sstevel@tonic-gate retval = 1;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate (void) krb5_kt_close(kcontext, kt_handle);
2297c478bd9Sstevel@tonic-gate out:
2307c478bd9Sstevel@tonic-gate if (princ && kcontext)
2317c478bd9Sstevel@tonic-gate krb5_free_principal(kcontext, princ);
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate if (kcontext)
2347c478bd9Sstevel@tonic-gate krb5_free_context(kcontext);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate return (retval);
2377c478bd9Sstevel@tonic-gate }
238