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