10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include "files_common.h" 300Sstevel@tonic-gate #include <time.h> 310Sstevel@tonic-gate #include <exec_attr.h> 320Sstevel@tonic-gate #include <strings.h> 330Sstevel@tonic-gate #include <sys/stat.h> 340Sstevel@tonic-gate #include <sys/mman.h> 350Sstevel@tonic-gate #include <ctype.h> 360Sstevel@tonic-gate #include <synch.h> 370Sstevel@tonic-gate #include <sys/types.h> 380Sstevel@tonic-gate #include <sys/uio.h> 390Sstevel@tonic-gate #include <unistd.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * files/getexecattr.c -- "files" backend for nsswitch "exec_attr" database 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * _execattr_files_read_line and _execattr_files_XY_all code based on 450Sstevel@tonic-gate * nss_files_read_line and nss_files_XY_all respectively, from files_common.c 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* externs from libnsl */ 500Sstevel@tonic-gate extern int _doexeclist(nss_XbyY_args_t *); 510Sstevel@tonic-gate extern int _readbufline(char *, int, char *, int, int *); 520Sstevel@tonic-gate extern char *_exec_wild_id(char *, const char *); 530Sstevel@tonic-gate extern void _exec_cleanup(nss_status_t, nss_XbyY_args_t *); 540Sstevel@tonic-gate 550Sstevel@tonic-gate typedef int (*_exec_XY_check_func) (nss_XbyY_args_t *); 560Sstevel@tonic-gate 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * check_match: returns 1 if matching entry found, else returns 0. 600Sstevel@tonic-gate */ 610Sstevel@tonic-gate static int 620Sstevel@tonic-gate check_match(nss_XbyY_args_t *argp) 630Sstevel@tonic-gate { 640Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 650Sstevel@tonic-gate const char *name = _priv_exec->name; 660Sstevel@tonic-gate const char *type = _priv_exec->type; 670Sstevel@tonic-gate const char *id = _priv_exec->id; 680Sstevel@tonic-gate const char *policy = _priv_exec->policy; 690Sstevel@tonic-gate execstr_t *exec = (execstr_t *)argp->returnval; 700Sstevel@tonic-gate 710Sstevel@tonic-gate if ((policy && exec->policy && (strcmp(policy, exec->policy) != 0)) || 720Sstevel@tonic-gate (name && exec->name && (strcmp(name, exec->name) != 0)) || 730Sstevel@tonic-gate (type && exec->type && (strcmp(type, exec->type) != 0)) || 740Sstevel@tonic-gate (id && exec->id && (strcmp(id, exec->id) != 0))) { 750Sstevel@tonic-gate return (0); 760Sstevel@tonic-gate } 770Sstevel@tonic-gate 780Sstevel@tonic-gate return (1); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate 820Sstevel@tonic-gate static nss_status_t 830Sstevel@tonic-gate _exec_files_XY_all(files_backend_ptr_t be, 840Sstevel@tonic-gate nss_XbyY_args_t *argp, 850Sstevel@tonic-gate int getby_flag) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate int parse_stat = 0; 880Sstevel@tonic-gate int lastlen = 0; 890Sstevel@tonic-gate int exec_fd = 0; 900Sstevel@tonic-gate int f_size = 0; 910Sstevel@tonic-gate time_t f_time = 0; 920Sstevel@tonic-gate static time_t read_time = 0; 930Sstevel@tonic-gate char *key = NULL; 940Sstevel@tonic-gate char *first; 950Sstevel@tonic-gate char *last; 960Sstevel@tonic-gate static char *f_buf = NULL; 970Sstevel@tonic-gate struct stat f_stat; 980Sstevel@tonic-gate nss_status_t res = NSS_NOTFOUND; 990Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 1000Sstevel@tonic-gate static rwlock_t exec_lock; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate if (((be->buf == NULL) && 1030Sstevel@tonic-gate ((be->buf = (char *)calloc(1, be->minbuf)) == NULL)) || 1040Sstevel@tonic-gate (be->filename == NULL) || 1050Sstevel@tonic-gate (rw_rdlock(&exec_lock) != 0)) { 1060Sstevel@tonic-gate return (NSS_UNAVAIL); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * check the size and the time stamp on the file 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate if (stat(be->filename, &f_stat) != 0) { 1130Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1140Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1150Sstevel@tonic-gate return (NSS_UNAVAIL); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate f_size = f_stat.st_size; 1190Sstevel@tonic-gate f_time = f_stat.st_mtime; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate while (f_time > read_time) { 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * file has been modified since we last read it. 1240Sstevel@tonic-gate * read it into the buffer with rw lock. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1270Sstevel@tonic-gate if (rw_wrlock(&exec_lock) != 0) { 1280Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1290Sstevel@tonic-gate return (NSS_UNAVAIL); 1300Sstevel@tonic-gate } 131*1914Scasper if ((be->f = fopen(be->filename, "rF")) == 0) { 1320Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1330Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1340Sstevel@tonic-gate return (NSS_UNAVAIL); 1350Sstevel@tonic-gate } 136*1914Scasper exec_fd = fileno(be->f); 1370Sstevel@tonic-gate if (f_buf != NULL) 1380Sstevel@tonic-gate free(f_buf); 1390Sstevel@tonic-gate if ((f_buf = malloc(f_size)) == NULL) { 1400Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1410Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1420Sstevel@tonic-gate return (NSS_UNAVAIL); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate if (read(exec_fd, f_buf, f_size) < f_size) { 1450Sstevel@tonic-gate free(f_buf); 1460Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1470Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1480Sstevel@tonic-gate return (NSS_UNAVAIL); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate read_time = f_time; 1510Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * verify that the file did not change after 1540Sstevel@tonic-gate * we read it. 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate if (rw_rdlock(&exec_lock) != 0) { 1570Sstevel@tonic-gate free(f_buf); 1580Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1590Sstevel@tonic-gate return (NSS_UNAVAIL); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate if (stat(be->filename, &f_stat) != 0) { 1620Sstevel@tonic-gate free(f_buf); 1630Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1640Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1650Sstevel@tonic-gate return (NSS_UNAVAIL); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate f_size = f_stat.st_size; 1680Sstevel@tonic-gate f_time = f_stat.st_mtime; 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate res = NSS_NOTFOUND; 1720Sstevel@tonic-gate while (1) { 1730Sstevel@tonic-gate int linelen = 0; 1740Sstevel@tonic-gate int check_stat = 0; 1750Sstevel@tonic-gate char *instr = be->buf; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate linelen = _readbufline(f_buf, f_size, instr, be->minbuf, 1780Sstevel@tonic-gate &lastlen); 1790Sstevel@tonic-gate if (linelen < 0) { 1800Sstevel@tonic-gate /* End of file */ 1810Sstevel@tonic-gate argp->erange = 0; 1820Sstevel@tonic-gate break; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* 1860Sstevel@tonic-gate * If the entry doesn't contain the filter string then 1870Sstevel@tonic-gate * it can't be the entry we want, so don't bother looking 1880Sstevel@tonic-gate * more closely at it. 1890Sstevel@tonic-gate */ 1900Sstevel@tonic-gate switch (getby_flag) { 1910Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYNAME: 1920Sstevel@tonic-gate if (strstr(instr, _priv_exec->name) == NULL) 1930Sstevel@tonic-gate continue; 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYID: 1960Sstevel@tonic-gate if (strstr(instr, _priv_exec->id) == NULL) 1970Sstevel@tonic-gate continue; 1980Sstevel@tonic-gate break; 1990Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYNAMEID: 2000Sstevel@tonic-gate if ((strstr(instr, _priv_exec->name) == NULL) || 2010Sstevel@tonic-gate (strstr(instr, _priv_exec->id) == NULL)) 2020Sstevel@tonic-gate continue; 2030Sstevel@tonic-gate break; 2040Sstevel@tonic-gate default: 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate if ((strstr(instr, _priv_exec->policy) == NULL) || 2080Sstevel@tonic-gate ((_priv_exec->type != NULL) && 2090Sstevel@tonic-gate (strstr(instr, _priv_exec->type) == NULL))) 2100Sstevel@tonic-gate continue; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * Get rid of white spaces, comments etc. 2140Sstevel@tonic-gate */ 2150Sstevel@tonic-gate if ((last = strchr(instr, '#')) == NULL) 2160Sstevel@tonic-gate last = instr + linelen; 2170Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't any, 2200Sstevel@tonic-gate * so it's not worth calling strspn(). 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate for (first = instr; isspace(*first); first++) 2230Sstevel@tonic-gate ; 2240Sstevel@tonic-gate if (*first == '\0') 2250Sstevel@tonic-gate continue; 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * Found something non-blank on the line. Skip back 2280Sstevel@tonic-gate * over any trailing whitespace; since we know there's 2290Sstevel@tonic-gate * non-whitespace earlier in the line, checking for 2300Sstevel@tonic-gate * termination is easy. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate while (isspace(*last)) 2330Sstevel@tonic-gate --last; 2340Sstevel@tonic-gate linelen = last - first + 1; 2350Sstevel@tonic-gate if (first != instr) 2360Sstevel@tonic-gate instr = first; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Parse the entry. 2400Sstevel@tonic-gate */ 2410Sstevel@tonic-gate argp->returnval = NULL; 2420Sstevel@tonic-gate parse_stat = (*argp->str2ent)(instr, linelen, argp->buf.result, 2430Sstevel@tonic-gate argp->buf.buffer, argp->buf.buflen); 2440Sstevel@tonic-gate if (parse_stat == NSS_STR_PARSE_SUCCESS) { 2450Sstevel@tonic-gate argp->returnval = argp->buf.result; 2460Sstevel@tonic-gate if (check_match(argp)) { 2470Sstevel@tonic-gate res = NSS_SUCCESS; 2480Sstevel@tonic-gate if (_priv_exec->search_flag == GET_ONE) { 2490Sstevel@tonic-gate break; 2500Sstevel@tonic-gate } else if (_doexeclist(argp) == 0) { 2510Sstevel@tonic-gate res = NSS_UNAVAIL; 2520Sstevel@tonic-gate break; 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate } else { 2550Sstevel@tonic-gate argp->returnval = NULL; 2560Sstevel@tonic-gate memset(argp->buf.buffer, NULL, 2570Sstevel@tonic-gate argp->buf.buflen); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate } else if (parse_stat == NSS_STR_PARSE_ERANGE) { 2600Sstevel@tonic-gate argp->erange = 1; 2610Sstevel@tonic-gate break; 2620Sstevel@tonic-gate } /* else if (parse_stat == NSS_STR_PARSE_PARSE) don't care ! */ 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 2660Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate return (res); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * If search for exact match for id failed, get_wild checks if we have 2740Sstevel@tonic-gate * a wild-card entry for that id. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate static nss_status_t 2770Sstevel@tonic-gate get_wild(files_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag) 2780Sstevel@tonic-gate { 2790Sstevel@tonic-gate char *orig_id = NULL; 2800Sstevel@tonic-gate char *old_id = NULL; 2810Sstevel@tonic-gate char *wild_id = NULL; 2820Sstevel@tonic-gate nss_status_t res = NSS_NOTFOUND; 2830Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate orig_id = strdup(_priv_exec->id); 2860Sstevel@tonic-gate old_id = strdup(_priv_exec->id); 2870Sstevel@tonic-gate wild_id = old_id; 2880Sstevel@tonic-gate while ((wild_id = _exec_wild_id(wild_id, _priv_exec->type)) != NULL) { 2890Sstevel@tonic-gate _priv_exec->id = wild_id; 2900Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, getby_flag); 2910Sstevel@tonic-gate if (res == NSS_SUCCESS) 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate _priv_exec->id = orig_id; 2950Sstevel@tonic-gate if (old_id) 2960Sstevel@tonic-gate free(old_id); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate return (res); 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate static nss_status_t 3030Sstevel@tonic-gate getbynam(files_backend_ptr_t be, void *a) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate nss_status_t res; 3060Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAME); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate _exec_cleanup(res, argp); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate return (res); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate static nss_status_t 3170Sstevel@tonic-gate getbyid(files_backend_ptr_t be, void *a) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate nss_status_t res; 3200Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 3210Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYID); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if (res != NSS_SUCCESS) 3260Sstevel@tonic-gate res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYID); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate _exec_cleanup(res, argp); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate return (res); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate static nss_status_t 3350Sstevel@tonic-gate getbynameid(files_backend_ptr_t be, void *a) 3360Sstevel@tonic-gate { 3370Sstevel@tonic-gate nss_status_t res; 3380Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 3390Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAMEID); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if (res != NSS_SUCCESS) 3440Sstevel@tonic-gate res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYNAMEID); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate _exec_cleanup(res, argp); 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate return (res); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate static files_backend_op_t execattr_ops[] = { 3530Sstevel@tonic-gate _nss_files_destr, 3540Sstevel@tonic-gate _nss_files_endent, 3550Sstevel@tonic-gate _nss_files_setent, 3560Sstevel@tonic-gate _nss_files_getent_netdb, 3570Sstevel@tonic-gate getbynam, 3580Sstevel@tonic-gate getbyid, 3590Sstevel@tonic-gate getbynameid 3600Sstevel@tonic-gate }; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate nss_backend_t * 3630Sstevel@tonic-gate _nss_files_exec_attr_constr(const char *dummy1, 3640Sstevel@tonic-gate const char *dummy2, 3650Sstevel@tonic-gate const char *dummy3, 3660Sstevel@tonic-gate const char *dummy4, 3670Sstevel@tonic-gate const char *dummy5, 3680Sstevel@tonic-gate const char *dummy6, 3690Sstevel@tonic-gate const char *dummy7) 3700Sstevel@tonic-gate { 3710Sstevel@tonic-gate return (_nss_files_constr(execattr_ops, 3720Sstevel@tonic-gate sizeof (execattr_ops)/sizeof (execattr_ops[0]), 3730Sstevel@tonic-gate EXECATTR_FILENAME, 3740Sstevel@tonic-gate NSS_LINELEN_EXECATTR, 3750Sstevel@tonic-gate NULL)); 3760Sstevel@tonic-gate } 377