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 51914Scasper * Common Development and Distribution License (the "License"). 61914Scasper * 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*10020SJoep.Vesseur@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <stdlib.h> 270Sstevel@tonic-gate #include "files_common.h" 280Sstevel@tonic-gate #include <time.h> 290Sstevel@tonic-gate #include <exec_attr.h> 300Sstevel@tonic-gate #include <strings.h> 310Sstevel@tonic-gate #include <sys/stat.h> 320Sstevel@tonic-gate #include <sys/mman.h> 330Sstevel@tonic-gate #include <ctype.h> 340Sstevel@tonic-gate #include <synch.h> 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <sys/uio.h> 370Sstevel@tonic-gate #include <unistd.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * files/getexecattr.c -- "files" backend for nsswitch "exec_attr" database 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * _execattr_files_read_line and _execattr_files_XY_all code based on 430Sstevel@tonic-gate * nss_files_read_line and nss_files_XY_all respectively, from files_common.c 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* externs from libnsl */ 480Sstevel@tonic-gate extern int _doexeclist(nss_XbyY_args_t *); 490Sstevel@tonic-gate extern int _readbufline(char *, int, char *, int, int *); 500Sstevel@tonic-gate extern char *_exec_wild_id(char *, const char *); 510Sstevel@tonic-gate extern void _exec_cleanup(nss_status_t, nss_XbyY_args_t *); 520Sstevel@tonic-gate 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * check_match: returns 1 if matching entry found, else returns 0. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate static int 582830Sdjl check_match(nss_XbyY_args_t *argp, const char *line, int linelen) 590Sstevel@tonic-gate { 602830Sdjl const char *limit, *linep, *keyp; 610Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 622830Sdjl const char *exec_field[6]; 632830Sdjl int i; 642830Sdjl 652830Sdjl exec_field[0] = _priv_exec->name; /* name */ 662830Sdjl exec_field[1] = _priv_exec->policy; /* policy */ 672830Sdjl exec_field[2] = _priv_exec->type; /* type */ 682830Sdjl exec_field[3] = NULL; /* res1 */ 692830Sdjl exec_field[4] = NULL; /* res2 */ 702830Sdjl exec_field[5] = _priv_exec->id; /* id */ 712830Sdjl /* No need to check attr field */ 722830Sdjl 732830Sdjl linep = line; 742830Sdjl limit = line + linelen; 750Sstevel@tonic-gate 762830Sdjl for (i = 0; i < 6; i++) { 772830Sdjl keyp = exec_field[i]; 782830Sdjl if (keyp) { 792830Sdjl /* compare field */ 802830Sdjl while (*keyp && linep < limit && 81*10020SJoep.Vesseur@Sun.COM *linep != ':' && *keyp == *linep) { 822830Sdjl keyp++; 832830Sdjl linep++; 842830Sdjl } 852830Sdjl if (*keyp || linep == limit || *linep != ':') 862830Sdjl return (0); 872830Sdjl } else { 882830Sdjl /* skip field */ 892830Sdjl while (linep < limit && *linep != ':') 902830Sdjl linep++; 912830Sdjl } 922830Sdjl linep++; 930Sstevel@tonic-gate } 940Sstevel@tonic-gate return (1); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate 980Sstevel@tonic-gate static nss_status_t 990Sstevel@tonic-gate _exec_files_XY_all(files_backend_ptr_t be, 1000Sstevel@tonic-gate nss_XbyY_args_t *argp, 1010Sstevel@tonic-gate int getby_flag) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate int parse_stat = 0; 1040Sstevel@tonic-gate int lastlen = 0; 1050Sstevel@tonic-gate int exec_fd = 0; 1060Sstevel@tonic-gate int f_size = 0; 1070Sstevel@tonic-gate time_t f_time = 0; 1080Sstevel@tonic-gate static time_t read_time = 0; 1090Sstevel@tonic-gate char *first; 1100Sstevel@tonic-gate char *last; 1110Sstevel@tonic-gate static char *f_buf = NULL; 1120Sstevel@tonic-gate struct stat f_stat; 1130Sstevel@tonic-gate nss_status_t res = NSS_NOTFOUND; 1140Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 1150Sstevel@tonic-gate static rwlock_t exec_lock; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if (((be->buf == NULL) && 1180Sstevel@tonic-gate ((be->buf = (char *)calloc(1, be->minbuf)) == NULL)) || 1190Sstevel@tonic-gate (be->filename == NULL) || 1200Sstevel@tonic-gate (rw_rdlock(&exec_lock) != 0)) { 1210Sstevel@tonic-gate return (NSS_UNAVAIL); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * check the size and the time stamp on the file 1260Sstevel@tonic-gate */ 1270Sstevel@tonic-gate if (stat(be->filename, &f_stat) != 0) { 1280Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1290Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1300Sstevel@tonic-gate return (NSS_UNAVAIL); 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate f_size = f_stat.st_size; 1340Sstevel@tonic-gate f_time = f_stat.st_mtime; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate while (f_time > read_time) { 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * file has been modified since we last read it. 1390Sstevel@tonic-gate * read it into the buffer with rw lock. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1420Sstevel@tonic-gate if (rw_wrlock(&exec_lock) != 0) { 1430Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1440Sstevel@tonic-gate return (NSS_UNAVAIL); 1450Sstevel@tonic-gate } 1461914Scasper if ((be->f = fopen(be->filename, "rF")) == 0) { 1470Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1480Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1490Sstevel@tonic-gate return (NSS_UNAVAIL); 1500Sstevel@tonic-gate } 1511914Scasper exec_fd = fileno(be->f); 1520Sstevel@tonic-gate if (f_buf != NULL) 1530Sstevel@tonic-gate free(f_buf); 1540Sstevel@tonic-gate if ((f_buf = malloc(f_size)) == NULL) { 1550Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1560Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1570Sstevel@tonic-gate return (NSS_UNAVAIL); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate if (read(exec_fd, f_buf, f_size) < f_size) { 1600Sstevel@tonic-gate free(f_buf); 1610Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1620Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1630Sstevel@tonic-gate return (NSS_UNAVAIL); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate read_time = f_time; 1660Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1670Sstevel@tonic-gate /* 1680Sstevel@tonic-gate * verify that the file did not change after 1690Sstevel@tonic-gate * we read it. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate if (rw_rdlock(&exec_lock) != 0) { 1720Sstevel@tonic-gate free(f_buf); 1730Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1740Sstevel@tonic-gate return (NSS_UNAVAIL); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate if (stat(be->filename, &f_stat) != 0) { 1770Sstevel@tonic-gate free(f_buf); 1780Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 1790Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 1800Sstevel@tonic-gate return (NSS_UNAVAIL); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate f_size = f_stat.st_size; 1830Sstevel@tonic-gate f_time = f_stat.st_mtime; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate res = NSS_NOTFOUND; 1872830Sdjl /*CONSTCOND*/ 1880Sstevel@tonic-gate while (1) { 1890Sstevel@tonic-gate int linelen = 0; 1900Sstevel@tonic-gate char *instr = be->buf; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate linelen = _readbufline(f_buf, f_size, instr, be->minbuf, 1930Sstevel@tonic-gate &lastlen); 1940Sstevel@tonic-gate if (linelen < 0) { 1950Sstevel@tonic-gate /* End of file */ 1960Sstevel@tonic-gate break; 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * If the entry doesn't contain the filter string then 2010Sstevel@tonic-gate * it can't be the entry we want, so don't bother looking 2020Sstevel@tonic-gate * more closely at it. 2030Sstevel@tonic-gate */ 2040Sstevel@tonic-gate switch (getby_flag) { 2050Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYNAME: 2060Sstevel@tonic-gate if (strstr(instr, _priv_exec->name) == NULL) 2070Sstevel@tonic-gate continue; 2080Sstevel@tonic-gate break; 2090Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYID: 2100Sstevel@tonic-gate if (strstr(instr, _priv_exec->id) == NULL) 2110Sstevel@tonic-gate continue; 2120Sstevel@tonic-gate break; 2130Sstevel@tonic-gate case NSS_DBOP_EXECATTR_BYNAMEID: 2140Sstevel@tonic-gate if ((strstr(instr, _priv_exec->name) == NULL) || 2150Sstevel@tonic-gate (strstr(instr, _priv_exec->id) == NULL)) 2160Sstevel@tonic-gate continue; 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate default: 2190Sstevel@tonic-gate break; 2200Sstevel@tonic-gate } 221*10020SJoep.Vesseur@Sun.COM if (((_priv_exec->policy != NULL) && 222*10020SJoep.Vesseur@Sun.COM (strstr(instr, _priv_exec->policy) == NULL)) || 2230Sstevel@tonic-gate ((_priv_exec->type != NULL) && 2240Sstevel@tonic-gate (strstr(instr, _priv_exec->type) == NULL))) 2250Sstevel@tonic-gate continue; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * Get rid of white spaces, comments etc. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate if ((last = strchr(instr, '#')) == NULL) 2310Sstevel@tonic-gate last = instr + linelen; 2320Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 2330Sstevel@tonic-gate /* 2340Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't any, 2350Sstevel@tonic-gate * so it's not worth calling strspn(). 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate for (first = instr; isspace(*first); first++) 2380Sstevel@tonic-gate ; 2390Sstevel@tonic-gate if (*first == '\0') 2400Sstevel@tonic-gate continue; 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * Found something non-blank on the line. Skip back 2430Sstevel@tonic-gate * over any trailing whitespace; since we know there's 2440Sstevel@tonic-gate * non-whitespace earlier in the line, checking for 2450Sstevel@tonic-gate * termination is easy. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate while (isspace(*last)) 2480Sstevel@tonic-gate --last; 2490Sstevel@tonic-gate linelen = last - first + 1; 2500Sstevel@tonic-gate if (first != instr) 2510Sstevel@tonic-gate instr = first; 2520Sstevel@tonic-gate 2532830Sdjl /* Check the entry */ 2540Sstevel@tonic-gate argp->returnval = NULL; 2552830Sdjl argp->returnlen = 0; 2562830Sdjl if (check_match(argp, instr, linelen) == 0) 2572830Sdjl continue; 2582830Sdjl 2592830Sdjl /* Marshall the data */ 2600Sstevel@tonic-gate parse_stat = (*argp->str2ent)(instr, linelen, argp->buf.result, 2610Sstevel@tonic-gate argp->buf.buffer, argp->buf.buflen); 2620Sstevel@tonic-gate if (parse_stat == NSS_STR_PARSE_SUCCESS) { 2632830Sdjl argp->returnval = (argp->buf.result != NULL)? 264*10020SJoep.Vesseur@Sun.COM argp->buf.result : argp->buf.buffer; 2652830Sdjl argp->returnlen = linelen; 2662830Sdjl res = NSS_SUCCESS; 267*10020SJoep.Vesseur@Sun.COM if (IS_GET_ONE(_priv_exec->search_flag)) { 2682830Sdjl break; 2692830Sdjl } else if (_doexeclist(argp) == 0) { 2702830Sdjl res = NSS_UNAVAIL; 2712830Sdjl break; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate } else if (parse_stat == NSS_STR_PARSE_ERANGE) { 2740Sstevel@tonic-gate argp->erange = 1; 2750Sstevel@tonic-gate break; 2760Sstevel@tonic-gate } /* else if (parse_stat == NSS_STR_PARSE_PARSE) don't care ! */ 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 2800Sstevel@tonic-gate (void) rw_unlock(&exec_lock); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate return (res); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * If search for exact match for id failed, get_wild checks if we have 2880Sstevel@tonic-gate * a wild-card entry for that id. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate static nss_status_t 2910Sstevel@tonic-gate get_wild(files_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag) 2920Sstevel@tonic-gate { 2932830Sdjl const char *orig_id = NULL; 2940Sstevel@tonic-gate char *old_id = NULL; 2950Sstevel@tonic-gate char *wild_id = NULL; 2960Sstevel@tonic-gate nss_status_t res = NSS_NOTFOUND; 2970Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 2980Sstevel@tonic-gate 2992830Sdjl orig_id = _priv_exec->id; 3000Sstevel@tonic-gate old_id = strdup(_priv_exec->id); 3010Sstevel@tonic-gate wild_id = old_id; 3020Sstevel@tonic-gate while ((wild_id = _exec_wild_id(wild_id, _priv_exec->type)) != NULL) { 3030Sstevel@tonic-gate _priv_exec->id = wild_id; 3040Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, getby_flag); 3050Sstevel@tonic-gate if (res == NSS_SUCCESS) 3060Sstevel@tonic-gate break; 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate _priv_exec->id = orig_id; 3090Sstevel@tonic-gate if (old_id) 3100Sstevel@tonic-gate free(old_id); 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 getbynam(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 3220Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAME); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate _exec_cleanup(res, argp); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate return (res); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate static nss_status_t 3310Sstevel@tonic-gate getbyid(files_backend_ptr_t be, void *a) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate nss_status_t res; 3340Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 3352830Sdjl /*LINTED*/ 3360Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYID); 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if (res != NSS_SUCCESS) 3410Sstevel@tonic-gate res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYID); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate _exec_cleanup(res, argp); 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate return (res); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate static nss_status_t 3500Sstevel@tonic-gate getbynameid(files_backend_ptr_t be, void *a) 3510Sstevel@tonic-gate { 3520Sstevel@tonic-gate nss_status_t res; 3530Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 3542830Sdjl /*LINTED*/ 3550Sstevel@tonic-gate _priv_execattr *_priv_exec = (_priv_execattr *)(argp->key.attrp); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAMEID); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate if (res != NSS_SUCCESS) 3600Sstevel@tonic-gate res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYNAMEID); 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate _exec_cleanup(res, argp); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate return (res); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate static files_backend_op_t execattr_ops[] = { 3690Sstevel@tonic-gate _nss_files_destr, 3700Sstevel@tonic-gate _nss_files_endent, 3710Sstevel@tonic-gate _nss_files_setent, 3720Sstevel@tonic-gate _nss_files_getent_netdb, 3730Sstevel@tonic-gate getbynam, 3740Sstevel@tonic-gate getbyid, 3750Sstevel@tonic-gate getbynameid 3760Sstevel@tonic-gate }; 3770Sstevel@tonic-gate 3782830Sdjl /*ARGSUSED*/ 3790Sstevel@tonic-gate nss_backend_t * 3800Sstevel@tonic-gate _nss_files_exec_attr_constr(const char *dummy1, 3810Sstevel@tonic-gate const char *dummy2, 3820Sstevel@tonic-gate const char *dummy3, 3830Sstevel@tonic-gate const char *dummy4, 3840Sstevel@tonic-gate const char *dummy5, 3850Sstevel@tonic-gate const char *dummy6, 3860Sstevel@tonic-gate const char *dummy7) 3870Sstevel@tonic-gate { 3880Sstevel@tonic-gate return (_nss_files_constr(execattr_ops, 389*10020SJoep.Vesseur@Sun.COM sizeof (execattr_ops)/sizeof (execattr_ops[0]), 390*10020SJoep.Vesseur@Sun.COM EXECATTR_FILENAME, NSS_LINELEN_EXECATTR, NULL)); 3910Sstevel@tonic-gate } 392