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 5*36e852a1SRaja Andra * Common Development and Distribution License (the "License"). 6*36e852a1SRaja Andra * 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 /* 227c478bd9Sstevel@tonic-gate * ns_generic.c 237c478bd9Sstevel@tonic-gate * 24*36e852a1SRaja Andra * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <syslog.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <nsswitch.h> 337c478bd9Sstevel@tonic-gate #include <sys/param.h> 347c478bd9Sstevel@tonic-gate #include <netdb.h> 357c478bd9Sstevel@tonic-gate #include <errno.h> 367c478bd9Sstevel@tonic-gate #include <assert.h> 377c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 387c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h> 397c478bd9Sstevel@tonic-gate #include "automount.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Each name service is represented by a ns_info structure. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate struct ns_info { 457c478bd9Sstevel@tonic-gate char *ns_name; /* service name */ 467c478bd9Sstevel@tonic-gate void (*ns_init)(); /* initialization routine */ 477c478bd9Sstevel@tonic-gate int (*ns_getmapent)(); /* get map entry given key */ 487c478bd9Sstevel@tonic-gate int (*ns_loadmaster)(); /* load master map */ 497c478bd9Sstevel@tonic-gate int (*ns_loaddirect)(); /* load direct map */ 507c478bd9Sstevel@tonic-gate int (*ns_getmapkeys)(); /* readdir */ 517c478bd9Sstevel@tonic-gate }; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate static struct ns_info ns_info[] = { 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate "files", init_files, getmapent_files, 567c478bd9Sstevel@tonic-gate loadmaster_files, loaddirect_files, 577c478bd9Sstevel@tonic-gate getmapkeys_files, 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate "ldap", init_ldap, getmapent_ldap, 607c478bd9Sstevel@tonic-gate loadmaster_ldap, loaddirect_ldap, 617c478bd9Sstevel@tonic-gate getmapkeys_ldap, 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate "nis", init_nis, getmapent_nis, 647c478bd9Sstevel@tonic-gate loadmaster_nis, loaddirect_nis, 657c478bd9Sstevel@tonic-gate getmapkeys_nis, 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL, NULL, NULL, NULL 687c478bd9Sstevel@tonic-gate }; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static struct ns_info *get_next_ns(struct __nsw_lookup **, int); 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate void 737c478bd9Sstevel@tonic-gate ns_setup(char **stack, char ***stkptr) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate struct ns_info *nsp; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate for (nsp = ns_info; nsp->ns_name; nsp++) { 787c478bd9Sstevel@tonic-gate nsp->ns_init(stack, stkptr); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static struct ns_info * 837c478bd9Sstevel@tonic-gate get_next_ns(curr_ns, curr_nserr) 847c478bd9Sstevel@tonic-gate struct __nsw_lookup **curr_ns; 857c478bd9Sstevel@tonic-gate int curr_nserr; 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate static struct __nsw_switchconfig *conf = NULL; 887c478bd9Sstevel@tonic-gate enum __nsw_parse_err pserr; 897c478bd9Sstevel@tonic-gate struct __nsw_lookup *lkp; 907c478bd9Sstevel@tonic-gate struct ns_info *nsp; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (conf == NULL) { 937c478bd9Sstevel@tonic-gate /* __nsw_getconfig() is protected by a lock */ 947c478bd9Sstevel@tonic-gate conf = __nsw_getconfig("automount", &pserr); 957c478bd9Sstevel@tonic-gate if (conf == NULL) { 967c478bd9Sstevel@tonic-gate return (NULL); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (*curr_ns == NULL) 1017c478bd9Sstevel@tonic-gate /* first time */ 1027c478bd9Sstevel@tonic-gate lkp = conf->lookups; 1037c478bd9Sstevel@tonic-gate else { 1047c478bd9Sstevel@tonic-gate lkp = *curr_ns; 1057c478bd9Sstevel@tonic-gate /* __NSW_ACTION is MT-Safe */ 1067c478bd9Sstevel@tonic-gate if (__NSW_ACTION(lkp, curr_nserr) == __NSW_RETURN) 1077c478bd9Sstevel@tonic-gate return (NULL); 1087c478bd9Sstevel@tonic-gate lkp = lkp->next; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate for (; lkp; lkp = lkp->next) { 1127c478bd9Sstevel@tonic-gate for (nsp = ns_info; nsp->ns_name; nsp++) { 1137c478bd9Sstevel@tonic-gate if (strcmp(lkp->service_name, nsp->ns_name) == 0) { 1147c478bd9Sstevel@tonic-gate *curr_ns = lkp; 1157c478bd9Sstevel@tonic-gate return (nsp); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * Note: if we get here then we've found 1207c478bd9Sstevel@tonic-gate * an unsupported name service. 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate return (NULL); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate int 1287c478bd9Sstevel@tonic-gate getmapent(key, mapname, ml, stack, stkptr, iswildcard, isrestricted) 1297c478bd9Sstevel@tonic-gate char *key, *mapname; 1307c478bd9Sstevel@tonic-gate struct mapline *ml; 1317c478bd9Sstevel@tonic-gate char **stack, ***stkptr; 1327c478bd9Sstevel@tonic-gate bool_t *iswildcard; 1337c478bd9Sstevel@tonic-gate bool_t isrestricted; 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate struct __nsw_lookup *curr_ns = NULL; 1367c478bd9Sstevel@tonic-gate int ns_err = __NSW_SUCCESS; 1377c478bd9Sstevel@tonic-gate struct ns_info *nsp; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (strcmp(mapname, "-hosts") == 0) { 1407c478bd9Sstevel@tonic-gate (void) strcpy(ml->linebuf, "-hosts"); 1417c478bd9Sstevel@tonic-gate return (__NSW_SUCCESS); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (*mapname == '/') /* must be a file */ 1457c478bd9Sstevel@tonic-gate return (getmapent_files(key, mapname, ml, stack, stkptr, 1467c478bd9Sstevel@tonic-gate iswildcard, isrestricted)); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) { 1497c478bd9Sstevel@tonic-gate ns_err = nsp->ns_getmapent(key, mapname, ml, stack, stkptr, 1507c478bd9Sstevel@tonic-gate iswildcard, isrestricted); 1517c478bd9Sstevel@tonic-gate if (ns_err == __NSW_SUCCESS) 1527c478bd9Sstevel@tonic-gate return (__NSW_SUCCESS); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate int 1597c478bd9Sstevel@tonic-gate loadmaster_map(mapname, defopts, stack, stkptr) 1607c478bd9Sstevel@tonic-gate char *mapname, *defopts; 1617c478bd9Sstevel@tonic-gate char **stack, ***stkptr; 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate struct __nsw_lookup *curr_ns = NULL; 1647c478bd9Sstevel@tonic-gate int ns_err = __NSW_SUCCESS; 1657c478bd9Sstevel@tonic-gate struct ns_info *nsp; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if (*mapname == '/') /* must be a file */ 1687c478bd9Sstevel@tonic-gate return (loadmaster_files(mapname, defopts, stack, stkptr)); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) { 1717c478bd9Sstevel@tonic-gate ns_err = nsp->ns_loadmaster(mapname, defopts, stack, stkptr); 1727c478bd9Sstevel@tonic-gate if (ns_err == __NSW_SUCCESS) 1737c478bd9Sstevel@tonic-gate return (__NSW_SUCCESS); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 17911606941Sjwahlig int 1807c478bd9Sstevel@tonic-gate loaddirect_map(mapname, localmap, defopts, stack, stkptr) 1817c478bd9Sstevel@tonic-gate char *mapname, *localmap, *defopts; 1827c478bd9Sstevel@tonic-gate char **stack, ***stkptr; 1837c478bd9Sstevel@tonic-gate { 1847c478bd9Sstevel@tonic-gate struct __nsw_lookup *curr_ns = NULL; 1857c478bd9Sstevel@tonic-gate int ns_err = __NSW_SUCCESS; 1867c478bd9Sstevel@tonic-gate struct ns_info *nsp; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate if (*mapname == '/') /* must be a file */ 1897c478bd9Sstevel@tonic-gate return (loaddirect_files(mapname, localmap, defopts, 1907c478bd9Sstevel@tonic-gate stack, stkptr)); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) { 1937c478bd9Sstevel@tonic-gate ns_err = nsp->ns_loaddirect(mapname, localmap, defopts, stack, 1947c478bd9Sstevel@tonic-gate stkptr); 1957c478bd9Sstevel@tonic-gate if (ns_err == __NSW_SUCCESS) 1967c478bd9Sstevel@tonic-gate return (__NSW_SUCCESS); 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate int 2037c478bd9Sstevel@tonic-gate gethostkeys(mapname, list, error, cache_time) 2047c478bd9Sstevel@tonic-gate char *mapname; 2057c478bd9Sstevel@tonic-gate struct dir_entry **list; 2067c478bd9Sstevel@tonic-gate int *error; 2077c478bd9Sstevel@tonic-gate int *cache_time; 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate char *buffer, **p; 2107c478bd9Sstevel@tonic-gate int bufferlen = 1000; 2117c478bd9Sstevel@tonic-gate struct dir_entry *last = NULL; 2127c478bd9Sstevel@tonic-gate struct hostent ent; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate #ifdef lint 2157c478bd9Sstevel@tonic-gate mapname = mapname; 2167c478bd9Sstevel@tonic-gate #endif 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate *cache_time = RDDIR_CACHE_TIME * 2; 2197c478bd9Sstevel@tonic-gate *error = 0; 2207c478bd9Sstevel@tonic-gate if (trace > 1) 2217c478bd9Sstevel@tonic-gate trace_prt(1, "gethostkeys called\n"); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (sethostent(1)) { 2247c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "gethostkeys: sethostent failed"); 2257c478bd9Sstevel@tonic-gate *error = EIO; 2267c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate buffer = (char *)malloc(bufferlen); 2307c478bd9Sstevel@tonic-gate if (buffer == NULL) { 2317c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "gethostkeys: malloc of buffer failed"); 2327c478bd9Sstevel@tonic-gate *error = ENOMEM; 2337c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate while (gethostent_r(&ent, buffer, bufferlen, error)) { 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * add canonical name 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate if (add_dir_entry(ent.h_name, list, &last)) { 2417c478bd9Sstevel@tonic-gate *error = ENOMEM; 2427c478bd9Sstevel@tonic-gate goto done; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate if (ent.h_aliases == NULL) 2457c478bd9Sstevel@tonic-gate goto done; /* no aliases */ 2467c478bd9Sstevel@tonic-gate for (p = ent.h_aliases; *p != 0; p++) { 2477c478bd9Sstevel@tonic-gate if (strcmp(*p, ent.h_name) != 0) { 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * add alias only if different 2507c478bd9Sstevel@tonic-gate * from canonical name 2517c478bd9Sstevel@tonic-gate */ 2527c478bd9Sstevel@tonic-gate if (add_dir_entry(*p, list, &last)) { 2537c478bd9Sstevel@tonic-gate *error = ENOMEM; 2547c478bd9Sstevel@tonic-gate goto done; 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate assert(last != NULL); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate done: if (*list != NULL) { 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * list of entries found 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate *error = 0; 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate endhostent(); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate return (__NSW_SUCCESS); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* 2727c478bd9Sstevel@tonic-gate * enumerate all entries in the map in the various name services. 2737c478bd9Sstevel@tonic-gate */ 27411606941Sjwahlig int 2757c478bd9Sstevel@tonic-gate getmapkeys(mapname, list, error, cache_time, stack, stkptr, uid) 2767c478bd9Sstevel@tonic-gate char *mapname; 2777c478bd9Sstevel@tonic-gate struct dir_entry **list; 2787c478bd9Sstevel@tonic-gate int *error; 2797c478bd9Sstevel@tonic-gate int *cache_time; 2807c478bd9Sstevel@tonic-gate char **stack, ***stkptr; 2817c478bd9Sstevel@tonic-gate uid_t uid; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate struct __nsw_lookup *curr_ns = NULL; 2857c478bd9Sstevel@tonic-gate int ns_err = __NSW_SUCCESS; 2867c478bd9Sstevel@tonic-gate int success = 0; 2877c478bd9Sstevel@tonic-gate struct ns_info *nsp; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate if (*mapname == '/') /* must be a file */ 2907c478bd9Sstevel@tonic-gate return (getmapkeys_files(mapname, list, error, cache_time, 2917c478bd9Sstevel@tonic-gate stack, stkptr)); 2927c478bd9Sstevel@tonic-gate if (strcmp(mapname, "-hosts") == 0) { 2937c478bd9Sstevel@tonic-gate return (gethostkeys(mapname, list, error, cache_time)); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) { 2977c478bd9Sstevel@tonic-gate ns_err = nsp->ns_getmapkeys(mapname, list, error, 2987c478bd9Sstevel@tonic-gate cache_time, stack, stkptr); 2997c478bd9Sstevel@tonic-gate if (*error == 0) { 3007c478bd9Sstevel@tonic-gate /* 3017c478bd9Sstevel@tonic-gate * return success if listing was successful 3027c478bd9Sstevel@tonic-gate * for at least one name service 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate success++; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate /* 3087c478bd9Sstevel@tonic-gate * XXX force next name service 3097c478bd9Sstevel@tonic-gate */ 3107c478bd9Sstevel@tonic-gate if (ns_err != __NSW_UNAVAIL) 3117c478bd9Sstevel@tonic-gate ns_err = __NSW_NOTFOUND; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate if (success) { 3147c478bd9Sstevel@tonic-gate /* 3157c478bd9Sstevel@tonic-gate * if succeeded at least once, return error=0 3167c478bd9Sstevel@tonic-gate */ 3177c478bd9Sstevel@tonic-gate *error = 0; 3187c478bd9Sstevel@tonic-gate }; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate return (success ? __NSW_SUCCESS : __NSW_NOTFOUND); 3217c478bd9Sstevel@tonic-gate } 322