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
5facf4a8dSllai * Common Development and Distribution License (the "License").
6facf4a8dSllai * 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 /*
22353d0ed9SGary Mills * Copyright (c) 2011 Gary Mills
23353d0ed9SGary Mills *
24ceeba6f9Srui zang - Sun Microsystems - Beijing China * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
274bc0a2efScasper #define _POSIX_PTHREAD_SEMANTICS /* for getgrnam_r */
287c478bd9Sstevel@tonic-gate #ifdef lint
297c478bd9Sstevel@tonic-gate #define _REENTRANT /* for strtok_r */
307c478bd9Sstevel@tonic-gate #endif
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
341ca93273Seota #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <dirent.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <grp.h>
407c478bd9Sstevel@tonic-gate #include <pwd.h>
417c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate #include <sys/acl.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/devinfo_impl.h>
507c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
527c478bd9Sstevel@tonic-gate #include <libnvpair.h>
537c478bd9Sstevel@tonic-gate #include <device_info.h>
547c478bd9Sstevel@tonic-gate #include <regex.h>
557c478bd9Sstevel@tonic-gate #include <strings.h>
567c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
57c0eb16d4Sjg #include <zone.h>
58aecfc01dSrui zang - Sun Microsystems - Beijing China #include <fcntl.h>
59aecfc01dSrui zang - Sun Microsystems - Beijing China #include <utmpx.h>
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate extern int is_minor_node(const char *, const char **);
627c478bd9Sstevel@tonic-gate
63aecfc01dSrui zang - Sun Microsystems - Beijing China static int is_login_user(uid_t);
647c478bd9Sstevel@tonic-gate static int logindevperm(const char *, uid_t, gid_t, void (*)());
657c478bd9Sstevel@tonic-gate static int dir_dev_acc(char *, char *, uid_t, gid_t, mode_t, char *line,
667c478bd9Sstevel@tonic-gate void (*)());
677c478bd9Sstevel@tonic-gate static int setdevaccess(char *, uid_t, gid_t, mode_t, void (*)());
687c478bd9Sstevel@tonic-gate static void logerror(char *);
697c478bd9Sstevel@tonic-gate
701ca93273Seota static int is_blank(char *);
711ca93273Seota
727c478bd9Sstevel@tonic-gate #define MAX_LINELEN 256
737c478bd9Sstevel@tonic-gate #define LOGINDEVPERM "/etc/logindevperm"
747c478bd9Sstevel@tonic-gate #define DIRWILD "/*" /* directory wildcard */
757c478bd9Sstevel@tonic-gate #define DIRWLDLEN 2 /* strlen(DIRWILD) */
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Revoke all access to a device node and make sure that there are
797c478bd9Sstevel@tonic-gate * no interposed streams devices attached. Must be called before a
807c478bd9Sstevel@tonic-gate * device is actually opened.
817c478bd9Sstevel@tonic-gate * When fdetach is called, the underlying device node is revealed; it
827c478bd9Sstevel@tonic-gate * will have the previous owner and that owner can re-attach; so we
837c478bd9Sstevel@tonic-gate * retry until we win.
847c478bd9Sstevel@tonic-gate * Ignore non-existent devices.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate static int
setdevaccess(char * dev,uid_t uid,gid_t gid,mode_t mode,void (* errmsg)(char *))877c478bd9Sstevel@tonic-gate setdevaccess(char *dev, uid_t uid, gid_t gid, mode_t mode,
887c478bd9Sstevel@tonic-gate void (*errmsg)(char *))
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate int err = 0, local_errno;
917c478bd9Sstevel@tonic-gate char errstring[MAX_LINELEN];
9218c2aff7Sartem struct stat st;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate if (chown(dev, uid, gid) == -1) {
957c478bd9Sstevel@tonic-gate if (errno == ENOENT) /* no such file */
967c478bd9Sstevel@tonic-gate return (0);
977c478bd9Sstevel@tonic-gate err = -1;
98c4ca7dc0Sfrits local_errno = errno;
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
10118c2aff7Sartem /*
10218c2aff7Sartem * don't fdetach block devices, as it will unmount them
10318c2aff7Sartem */
10418c2aff7Sartem if (!((stat(dev, &st) == 0) && ((st.st_mode & S_IFMT) == S_IFBLK))) {
10518c2aff7Sartem while (fdetach(dev) == 0) {
10618c2aff7Sartem if (chown(dev, uid, gid) == -1) {
10718c2aff7Sartem err = -1;
10818c2aff7Sartem local_errno = errno;
10918c2aff7Sartem }
11018c2aff7Sartem }
11118c2aff7Sartem if (err && errmsg) {
11218c2aff7Sartem (void) snprintf(errstring, MAX_LINELEN,
11318c2aff7Sartem "failed to chown device %s: %s\n",
11418c2aff7Sartem dev, strerror(local_errno));
11518c2aff7Sartem (*errmsg)(errstring);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
119fa9e4066Sahrens /*
120fa9e4066Sahrens * strip_acl sets an acl and changes the files owner/group
121fa9e4066Sahrens */
122fa9e4066Sahrens err = acl_strip(dev, uid, gid, mode);
1237c478bd9Sstevel@tonic-gate
124fa9e4066Sahrens if (err != 0) {
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * If the file system returned ENOSYS, we know that it
1277c478bd9Sstevel@tonic-gate * doesn't support ACLs, therefore, we must assume that
1287c478bd9Sstevel@tonic-gate * there were no ACLs to remove in the first place.
1297c478bd9Sstevel@tonic-gate */
130facf4a8dSllai err = 0;
1317c478bd9Sstevel@tonic-gate if (errno != ENOSYS) {
1327c478bd9Sstevel@tonic-gate err = -1;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if (errmsg) {
1357c478bd9Sstevel@tonic-gate (void) snprintf(errstring, MAX_LINELEN,
1367c478bd9Sstevel@tonic-gate "failed to set acl on device %s: %s\n",
1377c478bd9Sstevel@tonic-gate dev, strerror(errno));
1387c478bd9Sstevel@tonic-gate (*errmsg)(errstring);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate }
141fa9e4066Sahrens if (chmod(dev, mode) == -1) {
142fa9e4066Sahrens err = -1;
143fa9e4066Sahrens if (errmsg) {
144fa9e4066Sahrens (void) snprintf(errstring, MAX_LINELEN,
145fa9e4066Sahrens "failed to chmod device %s: %s\n",
146fa9e4066Sahrens dev, strerror(errno));
147fa9e4066Sahrens (*errmsg)(errstring);
148fa9e4066Sahrens }
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate return (err);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate * logindevperm - change owner/group/permissions of devices
1577c478bd9Sstevel@tonic-gate * list in /etc/logindevperm.
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate static int
logindevperm(const char * ttyn,uid_t uid,gid_t gid,void (* errmsg)(char *))1607c478bd9Sstevel@tonic-gate logindevperm(const char *ttyn, uid_t uid, gid_t gid, void (*errmsg)(char *))
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate int err = 0, lineno = 0;
1637c478bd9Sstevel@tonic-gate const char *field_delims = " \t\n";
1647c478bd9Sstevel@tonic-gate char line[MAX_LINELEN], errstring[MAX_LINELEN];
1657c478bd9Sstevel@tonic-gate char saveline[MAX_LINELEN];
1667c478bd9Sstevel@tonic-gate char *console;
1677c478bd9Sstevel@tonic-gate char *mode_str;
1687c478bd9Sstevel@tonic-gate char *dev_list;
1697c478bd9Sstevel@tonic-gate char *device;
1707c478bd9Sstevel@tonic-gate char *ptr;
1717c478bd9Sstevel@tonic-gate int mode;
1727c478bd9Sstevel@tonic-gate FILE *fp;
173ceeba6f9Srui zang - Sun Microsystems - Beijing China char ttyn_path[PATH_MAX + 1];
174ceeba6f9Srui zang - Sun Microsystems - Beijing China int n;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate if ((fp = fopen(LOGINDEVPERM, "r")) == NULL) {
1777c478bd9Sstevel@tonic-gate if (errmsg) {
1787c478bd9Sstevel@tonic-gate (void) snprintf(errstring, MAX_LINELEN,
1797c478bd9Sstevel@tonic-gate LOGINDEVPERM ": open failed: %s\n",
1807c478bd9Sstevel@tonic-gate strerror(errno));
1817c478bd9Sstevel@tonic-gate (*errmsg)(errstring);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate return (-1);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
186ceeba6f9Srui zang - Sun Microsystems - Beijing China if ((n = resolvepath(ttyn, ttyn_path, PATH_MAX)) == -1)
187ceeba6f9Srui zang - Sun Microsystems - Beijing China return (-1);
188ceeba6f9Srui zang - Sun Microsystems - Beijing China ttyn_path[n] = '\0';
189ceeba6f9Srui zang - Sun Microsystems - Beijing China
1907c478bd9Sstevel@tonic-gate while (fgets(line, MAX_LINELEN, fp) != NULL) {
1917c478bd9Sstevel@tonic-gate char *last;
192ceeba6f9Srui zang - Sun Microsystems - Beijing China char tmp[PATH_MAX + 1];
193ceeba6f9Srui zang - Sun Microsystems - Beijing China
1947c478bd9Sstevel@tonic-gate lineno++;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate if ((ptr = strchr(line, '#')) != NULL)
1977c478bd9Sstevel@tonic-gate *ptr = '\0'; /* handle comments */
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate (void) strcpy(saveline, line);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate console = strtok_r(line, field_delims, &last);
2027c478bd9Sstevel@tonic-gate if (console == NULL)
2037c478bd9Sstevel@tonic-gate continue; /* ignore blank lines */
2047c478bd9Sstevel@tonic-gate
205ceeba6f9Srui zang - Sun Microsystems - Beijing China if ((n = resolvepath(console, tmp, PATH_MAX)) == -1)
206ceeba6f9Srui zang - Sun Microsystems - Beijing China continue;
207ceeba6f9Srui zang - Sun Microsystems - Beijing China tmp[n] = '\0';
208ceeba6f9Srui zang - Sun Microsystems - Beijing China
209ceeba6f9Srui zang - Sun Microsystems - Beijing China if (strcmp(ttyn_path, tmp) != 0)
210ceeba6f9Srui zang - Sun Microsystems - Beijing China continue;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate mode_str = strtok_r(last, field_delims, &last);
2137c478bd9Sstevel@tonic-gate if (mode_str == NULL) {
2147c478bd9Sstevel@tonic-gate err = -1; /* invalid entry, skip */
2157c478bd9Sstevel@tonic-gate if (errmsg) {
2167c478bd9Sstevel@tonic-gate (void) snprintf(errstring, MAX_LINELEN,
2177c478bd9Sstevel@tonic-gate LOGINDEVPERM
2187c478bd9Sstevel@tonic-gate ": line %d, invalid entry -- %s\n",
2197c478bd9Sstevel@tonic-gate lineno, line);
2207c478bd9Sstevel@tonic-gate (*errmsg)(errstring);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate continue;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* convert string to octal value */
2267c478bd9Sstevel@tonic-gate mode = strtol(mode_str, &ptr, 8);
2277c478bd9Sstevel@tonic-gate if (mode < 0 || mode > 0777 || *ptr != '\0') {
2287c478bd9Sstevel@tonic-gate err = -1; /* invalid mode, skip */
2297c478bd9Sstevel@tonic-gate if (errmsg) {
2307c478bd9Sstevel@tonic-gate (void) snprintf(errstring, MAX_LINELEN,
2317c478bd9Sstevel@tonic-gate LOGINDEVPERM
2327c478bd9Sstevel@tonic-gate ": line %d, invalid mode -- %s\n",
2337c478bd9Sstevel@tonic-gate lineno, mode_str);
2347c478bd9Sstevel@tonic-gate (*errmsg)(errstring);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate continue;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate dev_list = strtok_r(last, field_delims, &last);
2407c478bd9Sstevel@tonic-gate if (dev_list == NULL) {
2417c478bd9Sstevel@tonic-gate err = -1; /* empty device list, skip */
2427c478bd9Sstevel@tonic-gate if (errmsg) {
2437c478bd9Sstevel@tonic-gate (void) snprintf(errstring, MAX_LINELEN,
2447c478bd9Sstevel@tonic-gate LOGINDEVPERM
2457c478bd9Sstevel@tonic-gate ": line %d, empty device list -- %s\n",
2467c478bd9Sstevel@tonic-gate lineno, line);
2477c478bd9Sstevel@tonic-gate (*errmsg)(errstring);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate continue;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate device = strtok_r(dev_list, ":", &last);
2537c478bd9Sstevel@tonic-gate while (device != NULL) {
2547c478bd9Sstevel@tonic-gate if ((device[0] != '/') || (strlen(device) <= 1)) {
2557c478bd9Sstevel@tonic-gate err = -1;
2567c478bd9Sstevel@tonic-gate } else if (dir_dev_acc("/", &device[1], uid, gid, mode,
2577c478bd9Sstevel@tonic-gate saveline, errmsg)) {
2587c478bd9Sstevel@tonic-gate err = -1;
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate device = strtok_r(last, ":", &last);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate (void) fclose(fp);
2647c478bd9Sstevel@tonic-gate return (err);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate * returns 0 if resolved, -1 otherwise.
2697c478bd9Sstevel@tonic-gate * devpath: Absolute path to /dev link
2707c478bd9Sstevel@tonic-gate * devfs_path: Returns malloced string: /devices path w/out "/devices"
2717c478bd9Sstevel@tonic-gate */
2727e3e5701SJan Parcel int
devfs_resolve_link(char * devpath,char ** devfs_path)2737e3e5701SJan Parcel devfs_resolve_link(char *devpath, char **devfs_path)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1];
2767c478bd9Sstevel@tonic-gate char stage_link[PATH_MAX + 1];
2777c478bd9Sstevel@tonic-gate char *ptr;
2787c478bd9Sstevel@tonic-gate int linksize;
2797c478bd9Sstevel@tonic-gate char *slashdev = "/dev/";
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (devfs_path) {
2827c478bd9Sstevel@tonic-gate *devfs_path = NULL;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate linksize = readlink(devpath, contents, PATH_MAX);
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate if (linksize <= 0) {
2887c478bd9Sstevel@tonic-gate return (-1);
2897c478bd9Sstevel@tonic-gate } else {
2907c478bd9Sstevel@tonic-gate contents[linksize] = '\0';
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate * if the link contents is not a minor node assume
2957c478bd9Sstevel@tonic-gate * that link contents is really a pointer to another
2967c478bd9Sstevel@tonic-gate * link, and if so recurse and read its link contents.
2977c478bd9Sstevel@tonic-gate */
2987c478bd9Sstevel@tonic-gate if (is_minor_node((const char *)contents, (const char **)&ptr) !=
2997c478bd9Sstevel@tonic-gate 1) {
3007c478bd9Sstevel@tonic-gate if (strncmp(contents, slashdev, strlen(slashdev)) == 0) {
3017c478bd9Sstevel@tonic-gate /* absolute path, starting with /dev */
3027c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, contents);
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate /* relative path, prefix devpath */
3057c478bd9Sstevel@tonic-gate if ((ptr = strrchr(devpath, '/')) == NULL) {
3067c478bd9Sstevel@tonic-gate /* invalid link */
3077c478bd9Sstevel@tonic-gate return (-1);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate *ptr = '\0';
3107c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, devpath);
3117c478bd9Sstevel@tonic-gate *ptr = '/';
3127c478bd9Sstevel@tonic-gate (void) strcat(stage_link, "/");
3137c478bd9Sstevel@tonic-gate (void) strcat(stage_link, contents);
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate }
3167e3e5701SJan Parcel return (devfs_resolve_link(stage_link, devfs_path));
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate if (devfs_path) {
3207c478bd9Sstevel@tonic-gate *devfs_path = strdup(ptr);
3217c478bd9Sstevel@tonic-gate if (*devfs_path == NULL) {
3227c478bd9Sstevel@tonic-gate return (-1);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate return (0);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate * check a logindevperm line for a driver list and match this against
3317c478bd9Sstevel@tonic-gate * the driver of the minor node
3327c478bd9Sstevel@tonic-gate * returns 0 if no drivers were specified or a driver match
3337c478bd9Sstevel@tonic-gate */
3347c478bd9Sstevel@tonic-gate static int
check_driver_match(char * path,char * line)3357c478bd9Sstevel@tonic-gate check_driver_match(char *path, char *line)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate char *drv, *driver, *lasts;
3387c478bd9Sstevel@tonic-gate char *devfs_path = NULL;
3397c478bd9Sstevel@tonic-gate char saveline[MAX_LINELEN];
3407c478bd9Sstevel@tonic-gate char *p;
3417c478bd9Sstevel@tonic-gate
3427e3e5701SJan Parcel if (devfs_resolve_link(path, &devfs_path) == 0) {
3437c478bd9Sstevel@tonic-gate char *p;
3447c478bd9Sstevel@tonic-gate char pwd_buf[PATH_MAX];
3457c478bd9Sstevel@tonic-gate di_node_t node;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* truncate on : so we can take a snapshot */
3487c478bd9Sstevel@tonic-gate (void) strcpy(pwd_buf, devfs_path);
3497c478bd9Sstevel@tonic-gate p = strrchr(pwd_buf, ':');
3507c478bd9Sstevel@tonic-gate *p = '\0';
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate node = di_init(pwd_buf, DINFOMINOR);
3537c478bd9Sstevel@tonic-gate free(devfs_path);
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate if (node) {
3567c478bd9Sstevel@tonic-gate drv = di_driver_name(node);
3577c478bd9Sstevel@tonic-gate di_fini(node);
3587c478bd9Sstevel@tonic-gate } else {
3597c478bd9Sstevel@tonic-gate return (0);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate } else {
3627c478bd9Sstevel@tonic-gate return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate (void) strcpy(saveline, line);
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate p = strstr(saveline, "driver");
3687c478bd9Sstevel@tonic-gate if (p == NULL) {
3697c478bd9Sstevel@tonic-gate return (0);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate driver = strtok_r(p, "=", &lasts);
3737c478bd9Sstevel@tonic-gate if (driver) {
3747c478bd9Sstevel@tonic-gate if (strcmp(driver, "driver") == 0) {
3757c478bd9Sstevel@tonic-gate driver = strtok_r(NULL, ", \t\n", &lasts);
3767c478bd9Sstevel@tonic-gate while (driver) {
3777c478bd9Sstevel@tonic-gate if (strcmp(driver, drv) == 0) {
3787c478bd9Sstevel@tonic-gate return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate driver = strtok_r(NULL, ", \t\n", &lasts);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate return (-1);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate
388aecfc01dSrui zang - Sun Microsystems - Beijing China /*
389aecfc01dSrui zang - Sun Microsystems - Beijing China * Check whether the user has logged onto "/dev/console" or "/dev/vt/#".
390aecfc01dSrui zang - Sun Microsystems - Beijing China */
391aecfc01dSrui zang - Sun Microsystems - Beijing China static int
is_login_user(uid_t uid)392aecfc01dSrui zang - Sun Microsystems - Beijing China is_login_user(uid_t uid)
393aecfc01dSrui zang - Sun Microsystems - Beijing China {
394aecfc01dSrui zang - Sun Microsystems - Beijing China int changed = 0;
395aecfc01dSrui zang - Sun Microsystems - Beijing China struct passwd pwd, *ppwd;
396aecfc01dSrui zang - Sun Microsystems - Beijing China char pwd_buf[NSS_BUFLEN_PASSWD];
397aecfc01dSrui zang - Sun Microsystems - Beijing China struct utmpx *utx;
398aecfc01dSrui zang - Sun Microsystems - Beijing China
399353d0ed9SGary Mills if ((getpwuid_r(uid, &pwd, pwd_buf, NSS_BUFLEN_PASSWD, &ppwd) != 0) ||
400353d0ed9SGary Mills (ppwd == NULL)) {
401aecfc01dSrui zang - Sun Microsystems - Beijing China return (0);
402aecfc01dSrui zang - Sun Microsystems - Beijing China }
403aecfc01dSrui zang - Sun Microsystems - Beijing China
404aecfc01dSrui zang - Sun Microsystems - Beijing China setutxent();
405aecfc01dSrui zang - Sun Microsystems - Beijing China while ((utx = getutxent()) != NULL) {
406aecfc01dSrui zang - Sun Microsystems - Beijing China if (utx->ut_type == USER_PROCESS &&
407aecfc01dSrui zang - Sun Microsystems - Beijing China strncmp(utx->ut_user, ppwd->pw_name,
408aecfc01dSrui zang - Sun Microsystems - Beijing China strlen(ppwd->pw_name)) == 0 && (strncmp(utx->ut_line,
409aecfc01dSrui zang - Sun Microsystems - Beijing China "console", strlen("console")) == 0 || strncmp(utx->ut_line,
410aecfc01dSrui zang - Sun Microsystems - Beijing China "vt", strlen("vt")) == 0)) {
411aecfc01dSrui zang - Sun Microsystems - Beijing China
412aecfc01dSrui zang - Sun Microsystems - Beijing China changed = 1;
413aecfc01dSrui zang - Sun Microsystems - Beijing China break;
414aecfc01dSrui zang - Sun Microsystems - Beijing China }
415aecfc01dSrui zang - Sun Microsystems - Beijing China }
416aecfc01dSrui zang - Sun Microsystems - Beijing China endutxent();
417aecfc01dSrui zang - Sun Microsystems - Beijing China
418aecfc01dSrui zang - Sun Microsystems - Beijing China return (changed);
419aecfc01dSrui zang - Sun Microsystems - Beijing China }
420aecfc01dSrui zang - Sun Microsystems - Beijing China
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate * Apply owner/group/perms to all files (except "." and "..")
4237c478bd9Sstevel@tonic-gate * in a directory.
4247c478bd9Sstevel@tonic-gate * This function is recursive. We start with "/" and the rest of the pathname
4257c478bd9Sstevel@tonic-gate * in left_to_do argument, and we walk the entire pathname which may contain
4267c478bd9Sstevel@tonic-gate * regular expressions or '*' for each directory name or basename.
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate static int
dir_dev_acc(char * path,char * left_to_do,uid_t uid,gid_t gid,mode_t mode,char * line,void (* errmsg)(char *))4297c478bd9Sstevel@tonic-gate dir_dev_acc(char *path, char *left_to_do, uid_t uid, gid_t gid, mode_t mode,
4307c478bd9Sstevel@tonic-gate char *line, void (*errmsg)(char *))
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate struct stat stat_buf;
4337c478bd9Sstevel@tonic-gate int err = 0;
434c4ca7dc0Sfrits char errstring[MAX_LINELEN];
435facf4a8dSllai char *p;
436facf4a8dSllai regex_t regex;
437facf4a8dSllai int alwaysmatch = 0;
438facf4a8dSllai char *match;
439facf4a8dSllai char *name, *newpath, *remainder_path;
440facf4a8dSllai finddevhdl_t handle;
441facf4a8dSllai
442facf4a8dSllai /*
443c0eb16d4Sjg * Determine if the search needs to be performed via finddev,
444c0eb16d4Sjg * which returns only persisted names in the global /dev, or
445c0eb16d4Sjg * readdir, for paths other than /dev and non-global zones.
446c0eb16d4Sjg * This use of finddev avoids triggering potential implicit
447c0eb16d4Sjg * reconfig for names managed by logindevperm but not present
448c0eb16d4Sjg * on the system.
449facf4a8dSllai */
45073de625bSjg if (!device_exists(path)) {
451facf4a8dSllai return (-1);
452facf4a8dSllai }
4537c478bd9Sstevel@tonic-gate if (stat(path, &stat_buf) == -1) {
454c4ca7dc0Sfrits /*
455c4ca7dc0Sfrits * ENOENT errors are expected errors when there are
456c4ca7dc0Sfrits * dangling /dev device links. Ignore them silently
457c4ca7dc0Sfrits */
458c4ca7dc0Sfrits if (errno == ENOENT) {
459c4ca7dc0Sfrits return (0);
460c4ca7dc0Sfrits }
461c4ca7dc0Sfrits if (errmsg) {
462c4ca7dc0Sfrits (void) snprintf(errstring, MAX_LINELEN,
463c4ca7dc0Sfrits "failed to stat %s: %s\n", path,
464c4ca7dc0Sfrits strerror(errno));
465c4ca7dc0Sfrits (*errmsg)(errstring);
466c4ca7dc0Sfrits }
4677c478bd9Sstevel@tonic-gate return (-1);
4687c478bd9Sstevel@tonic-gate } else {
4697c478bd9Sstevel@tonic-gate if (!S_ISDIR(stat_buf.st_mode)) {
4707c478bd9Sstevel@tonic-gate if (strlen(left_to_do) == 0) {
4717c478bd9Sstevel@tonic-gate /* finally check the driver matches */
4727c478bd9Sstevel@tonic-gate if (check_driver_match(path, line) == 0) {
473aecfc01dSrui zang - Sun Microsystems - Beijing China /*
474aecfc01dSrui zang - Sun Microsystems - Beijing China * if the owner of device has been
475aecfc01dSrui zang - Sun Microsystems - Beijing China * login, the ownership and mode
476aecfc01dSrui zang - Sun Microsystems - Beijing China * should be set already. in
477aecfc01dSrui zang - Sun Microsystems - Beijing China * this case, do not set the
478aecfc01dSrui zang - Sun Microsystems - Beijing China * permissions.
479aecfc01dSrui zang - Sun Microsystems - Beijing China */
480aecfc01dSrui zang - Sun Microsystems - Beijing China if (is_login_user(stat_buf.st_uid)) {
481aecfc01dSrui zang - Sun Microsystems - Beijing China
482aecfc01dSrui zang - Sun Microsystems - Beijing China return (0);
483aecfc01dSrui zang - Sun Microsystems - Beijing China }
4847c478bd9Sstevel@tonic-gate /* we are done, set the permissions */
4857c478bd9Sstevel@tonic-gate if (setdevaccess(path,
4867c478bd9Sstevel@tonic-gate uid, gid, mode, errmsg)) {
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate return (-1);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate return (0);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
49673de625bSjg if (finddev_readdir(path, &handle) != 0)
49773de625bSjg return (0);
4987c478bd9Sstevel@tonic-gate
499facf4a8dSllai p = strchr(left_to_do, '/');
500facf4a8dSllai alwaysmatch = 0;
5017c478bd9Sstevel@tonic-gate
502facf4a8dSllai newpath = (char *)malloc(MAXPATHLEN);
503facf4a8dSllai if (newpath == NULL) {
50473de625bSjg finddev_close(handle);
505facf4a8dSllai return (-1);
506facf4a8dSllai }
5076f1fe223Srm match = (char *)calloc(MAXPATHLEN + 2, 1);
508facf4a8dSllai if (match == NULL) {
50973de625bSjg finddev_close(handle);
510facf4a8dSllai free(newpath);
511facf4a8dSllai return (-1);
512facf4a8dSllai }
513facf4a8dSllai
5146f1fe223Srm /* transform pattern into ^pattern$ for exact match */
5156f1fe223Srm if (snprintf(match, MAXPATHLEN + 2, "^%.*s$",
5166f1fe223Srm p ? (p - left_to_do) : strlen(left_to_do), left_to_do) >=
5176f1fe223Srm MAXPATHLEN + 2) {
5186f1fe223Srm finddev_close(handle);
5196f1fe223Srm free(newpath);
5206f1fe223Srm free(match);
5216f1fe223Srm return (-1);
522facf4a8dSllai }
523facf4a8dSllai
5246f1fe223Srm if (strcmp(match, "^*$") == 0) {
525facf4a8dSllai alwaysmatch = 1;
526facf4a8dSllai } else {
527facf4a8dSllai if (regcomp(®ex, match, REG_EXTENDED) != 0) {
528facf4a8dSllai free(newpath);
529facf4a8dSllai free(match);
53073de625bSjg finddev_close(handle);
531facf4a8dSllai return (-1);
5327c478bd9Sstevel@tonic-gate }
533facf4a8dSllai }
5347c478bd9Sstevel@tonic-gate
53573de625bSjg while ((name = (char *)finddev_next(handle)) != NULL) {
536facf4a8dSllai if (alwaysmatch ||
537facf4a8dSllai regexec(®ex, name, 0, NULL, 0) == 0) {
538facf4a8dSllai if (strcmp(path, "/") == 0) {
539facf4a8dSllai (void) snprintf(newpath,
540facf4a8dSllai MAXPATHLEN, "%s%s", path, name);
541facf4a8dSllai } else {
542facf4a8dSllai (void) snprintf(newpath,
543facf4a8dSllai MAXPATHLEN, "%s/%s", path, name);
544facf4a8dSllai }
5457c478bd9Sstevel@tonic-gate
546facf4a8dSllai /*
547facf4a8dSllai * recurse but adjust what is still left to do
548facf4a8dSllai */
549facf4a8dSllai remainder_path = (p ?
550facf4a8dSllai left_to_do + (p - left_to_do) + 1 :
551facf4a8dSllai &left_to_do[strlen(left_to_do)]);
552facf4a8dSllai if (dir_dev_acc(newpath, remainder_path,
553facf4a8dSllai uid, gid, mode, line, errmsg)) {
554facf4a8dSllai err = -1;
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate }
557facf4a8dSllai }
558facf4a8dSllai
55973de625bSjg finddev_close(handle);
560facf4a8dSllai free(newpath);
561facf4a8dSllai free(match);
562facf4a8dSllai if (!alwaysmatch) {
563facf4a8dSllai regfree(®ex);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate return (err);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate * di_devperm_login - modify access of devices in /etc/logindevperm
5717c478bd9Sstevel@tonic-gate * by changing owner/group/permissions to that of ttyn.
5727c478bd9Sstevel@tonic-gate */
5737c478bd9Sstevel@tonic-gate int
di_devperm_login(const char * ttyn,uid_t uid,gid_t gid,void (* errmsg)(char *))5747c478bd9Sstevel@tonic-gate di_devperm_login(const char *ttyn, uid_t uid, gid_t gid,
5757c478bd9Sstevel@tonic-gate void (*errmsg)(char *))
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate int err;
5787c478bd9Sstevel@tonic-gate struct group grp, *grpp;
5797c478bd9Sstevel@tonic-gate gid_t tty_gid;
5807c478bd9Sstevel@tonic-gate char grbuf[NSS_BUFLEN_GROUP];
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate if (errmsg == NULL)
5837c478bd9Sstevel@tonic-gate errmsg = logerror;
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate if (ttyn == NULL) {
5867c478bd9Sstevel@tonic-gate (*errmsg)("di_devperm_login: NULL tty device\n");
5877c478bd9Sstevel@tonic-gate return (-1);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate if (getgrnam_r("tty", &grp, grbuf, NSS_BUFLEN_GROUP, &grpp) != 0) {
5917c478bd9Sstevel@tonic-gate tty_gid = grpp->gr_gid;
5927c478bd9Sstevel@tonic-gate } else {
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate * this should never happen, but if it does set
5957c478bd9Sstevel@tonic-gate * group to tty's traditional value.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate tty_gid = 7;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate /* set the login console device permission */
6017c478bd9Sstevel@tonic-gate err = setdevaccess((char *)ttyn, uid, tty_gid,
6027c478bd9Sstevel@tonic-gate S_IRUSR|S_IWUSR|S_IWGRP, errmsg);
6037c478bd9Sstevel@tonic-gate if (err) {
6047c478bd9Sstevel@tonic-gate return (err);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate /* set the device permissions */
6087c478bd9Sstevel@tonic-gate return (logindevperm(ttyn, uid, gid, errmsg));
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate * di_devperm_logout - clean up access of devices in /etc/logindevperm
6137c478bd9Sstevel@tonic-gate * by resetting owner/group/permissions.
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate int
di_devperm_logout(const char * ttyn)6167c478bd9Sstevel@tonic-gate di_devperm_logout(const char *ttyn)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate struct passwd *pwd;
6197c478bd9Sstevel@tonic-gate uid_t root_uid;
6207c478bd9Sstevel@tonic-gate gid_t root_gid;
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate if (ttyn == NULL)
6237c478bd9Sstevel@tonic-gate return (-1);
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate pwd = getpwnam("root");
6267c478bd9Sstevel@tonic-gate if (pwd != NULL) {
6277c478bd9Sstevel@tonic-gate root_uid = pwd->pw_uid;
6287c478bd9Sstevel@tonic-gate root_gid = pwd->pw_gid;
6297c478bd9Sstevel@tonic-gate } else {
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate * this should never happen, but if it does set user
6327c478bd9Sstevel@tonic-gate * and group to root's traditional values.
6337c478bd9Sstevel@tonic-gate */
6347c478bd9Sstevel@tonic-gate root_uid = 0;
6357c478bd9Sstevel@tonic-gate root_gid = 0;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate return (logindevperm(ttyn, root_uid, root_gid, NULL));
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate static void
logerror(char * errstring)6427c478bd9Sstevel@tonic-gate logerror(char *errstring)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate syslog(LOG_AUTH | LOG_CRIT, "%s", errstring);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0'
6507c478bd9Sstevel@tonic-gate */
6517c478bd9Sstevel@tonic-gate static int
getnexttoken(char * next,char ** nextp,char ** tokenpp,char * tchar)6527c478bd9Sstevel@tonic-gate getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate char *cp;
6557c478bd9Sstevel@tonic-gate char *cp1;
6567c478bd9Sstevel@tonic-gate char *tokenp;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate cp = next;
6597c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') {
6607c478bd9Sstevel@tonic-gate cp++; /* skip leading spaces */
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate tokenp = cp; /* start of token */
6637c478bd9Sstevel@tonic-gate while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' &&
664811e5678Scasper *cp != ':' && *cp != '=' && *cp != '&' &&
665811e5678Scasper *cp != '|' && *cp != ';') {
6667c478bd9Sstevel@tonic-gate cp++; /* point to next character */
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate * If terminating character is a space or tab, look ahead to see if
6707c478bd9Sstevel@tonic-gate * there's another terminator that's not a space or a tab.
6717c478bd9Sstevel@tonic-gate * (This code handles trailing spaces.)
6727c478bd9Sstevel@tonic-gate */
6737c478bd9Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') {
6747c478bd9Sstevel@tonic-gate cp1 = cp;
6757c478bd9Sstevel@tonic-gate while (*++cp1 == ' ' || *cp1 == '\t')
6767c478bd9Sstevel@tonic-gate ;
6777c478bd9Sstevel@tonic-gate if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' ||
678811e5678Scasper *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') {
679*f00128d8SToomas Soome *cp = '\0'; /* terminate token */
6807c478bd9Sstevel@tonic-gate cp = cp1;
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate if (tchar != NULL) {
6847c478bd9Sstevel@tonic-gate *tchar = *cp; /* save terminating character */
6857c478bd9Sstevel@tonic-gate if (*tchar == '\0') {
6867c478bd9Sstevel@tonic-gate *tchar = '\n';
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate *cp++ = '\0'; /* terminate token, point to next */
6907c478bd9Sstevel@tonic-gate *nextp = cp; /* set pointer to next character */
6917c478bd9Sstevel@tonic-gate if (cp - tokenp - 1 == 0) {
6927c478bd9Sstevel@tonic-gate return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate *tokenpp = tokenp;
6957c478bd9Sstevel@tonic-gate return (1);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * get a decimal octal or hex number. Handle '~' for one's complement.
7007c478bd9Sstevel@tonic-gate */
7017c478bd9Sstevel@tonic-gate static int
getvalue(char * token,int * valuep)7027c478bd9Sstevel@tonic-gate getvalue(char *token, int *valuep)
7037c478bd9Sstevel@tonic-gate {
7047c478bd9Sstevel@tonic-gate int radix;
7057c478bd9Sstevel@tonic-gate int retval = 0;
7067c478bd9Sstevel@tonic-gate int onescompl = 0;
7077c478bd9Sstevel@tonic-gate int negate = 0;
7087c478bd9Sstevel@tonic-gate char c;
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate if (*token == '~') {
7117c478bd9Sstevel@tonic-gate onescompl++; /* perform one's complement on result */
7127c478bd9Sstevel@tonic-gate token++;
7137c478bd9Sstevel@tonic-gate } else if (*token == '-') {
7147c478bd9Sstevel@tonic-gate negate++;
7157c478bd9Sstevel@tonic-gate token++;
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate if (*token == '0') {
7187c478bd9Sstevel@tonic-gate token++;
7197c478bd9Sstevel@tonic-gate c = *token;
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate if (c == '\0') {
7227c478bd9Sstevel@tonic-gate *valuep = 0; /* value is 0 */
7237c478bd9Sstevel@tonic-gate return (0);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate if (c == 'x' || c == 'X') {
7277c478bd9Sstevel@tonic-gate radix = 16;
7287c478bd9Sstevel@tonic-gate token++;
7297c478bd9Sstevel@tonic-gate } else {
7307c478bd9Sstevel@tonic-gate radix = 8;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate } else
7337c478bd9Sstevel@tonic-gate radix = 10;
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate while ((c = *token++)) {
7367c478bd9Sstevel@tonic-gate switch (radix) {
7377c478bd9Sstevel@tonic-gate case 8:
7387c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '7') {
7397c478bd9Sstevel@tonic-gate c -= '0';
7407c478bd9Sstevel@tonic-gate } else {
7417c478bd9Sstevel@tonic-gate /* invalid number */
7427c478bd9Sstevel@tonic-gate return (0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate retval = (retval << 3) + c;
7457c478bd9Sstevel@tonic-gate break;
7467c478bd9Sstevel@tonic-gate case 10:
7477c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9') {
7487c478bd9Sstevel@tonic-gate c -= '0';
7497c478bd9Sstevel@tonic-gate } else {
7507c478bd9Sstevel@tonic-gate /* invalid number */
7517c478bd9Sstevel@tonic-gate return (0);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate retval = (retval * 10) + c;
7547c478bd9Sstevel@tonic-gate break;
7557c478bd9Sstevel@tonic-gate case 16:
7567c478bd9Sstevel@tonic-gate if (c >= 'a' && c <= 'f') {
7577c478bd9Sstevel@tonic-gate c = c - 'a' + 10;
7587c478bd9Sstevel@tonic-gate } else if (c >= 'A' && c <= 'F') {
7597c478bd9Sstevel@tonic-gate c = c - 'A' + 10;
7607c478bd9Sstevel@tonic-gate } else if (c >= '0' && c <= '9') {
7617c478bd9Sstevel@tonic-gate c -= '0';
7627c478bd9Sstevel@tonic-gate } else {
7637c478bd9Sstevel@tonic-gate /* invalid number */
7647c478bd9Sstevel@tonic-gate return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate retval = (retval << 4) + c;
7677c478bd9Sstevel@tonic-gate break;
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate if (onescompl) {
7717c478bd9Sstevel@tonic-gate retval = ~retval;
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate if (negate) {
7747c478bd9Sstevel@tonic-gate retval = -retval;
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate *valuep = retval;
7777c478bd9Sstevel@tonic-gate return (1);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate * Read /etc/minor_perm, return mperm list of entries
7827c478bd9Sstevel@tonic-gate */
7837c478bd9Sstevel@tonic-gate struct mperm *
i_devfs_read_minor_perm(char * drvname,void (* errcb)(minorperm_err_t,int))7847c478bd9Sstevel@tonic-gate i_devfs_read_minor_perm(char *drvname, void (*errcb)(minorperm_err_t, int))
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate FILE *pfd;
7877c478bd9Sstevel@tonic-gate struct mperm *mp;
7887c478bd9Sstevel@tonic-gate char line[MAX_MINOR_PERM_LINE];
7897c478bd9Sstevel@tonic-gate char *cp, *p, t;
7907c478bd9Sstevel@tonic-gate struct mperm *minor_perms = NULL;
7917c478bd9Sstevel@tonic-gate struct mperm *mptail = NULL;
7927c478bd9Sstevel@tonic-gate struct passwd *pw;
7937c478bd9Sstevel@tonic-gate struct group *gp;
7947c478bd9Sstevel@tonic-gate uid_t root_uid;
7957c478bd9Sstevel@tonic-gate gid_t sys_gid;
7967c478bd9Sstevel@tonic-gate int ln = 0;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate * Get root/sys ids, these being the most common
8007c478bd9Sstevel@tonic-gate */
8017c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
8027c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid;
8037c478bd9Sstevel@tonic-gate } else {
8047c478bd9Sstevel@tonic-gate (*errcb)(MP_CANT_FIND_USER_ERR, 0);
8057c478bd9Sstevel@tonic-gate root_uid = (uid_t)0; /* assume 0 is root */
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
8087c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid;
8097c478bd9Sstevel@tonic-gate } else {
8107c478bd9Sstevel@tonic-gate (*errcb)(MP_CANT_FIND_GROUP_ERR, 0);
8117c478bd9Sstevel@tonic-gate sys_gid = (gid_t)3; /* assume 3 is sys */
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate if ((pfd = fopen(MINOR_PERM_FILE, "r")) == NULL) {
8157c478bd9Sstevel@tonic-gate (*errcb)(MP_FOPEN_ERR, errno);
8167c478bd9Sstevel@tonic-gate return (NULL);
8177c478bd9Sstevel@tonic-gate }
8181ca93273Seota while (fgets(line, MAX_MINOR_PERM_LINE, pfd) != NULL) {
8197c478bd9Sstevel@tonic-gate ln++;
8201ca93273Seota /* cut off comments starting with '#' */
8211ca93273Seota if ((cp = strchr(line, '#')) != NULL)
8221ca93273Seota *cp = '\0';
8231ca93273Seota /* ignore comment or blank lines */
8241ca93273Seota if (is_blank(line))
8251ca93273Seota continue;
8267c478bd9Sstevel@tonic-gate mp = (struct mperm *)calloc(1, sizeof (struct mperm));
8277c478bd9Sstevel@tonic-gate if (mp == NULL) {
8287c478bd9Sstevel@tonic-gate (*errcb)(MP_ALLOC_ERR, sizeof (struct mperm));
8297c478bd9Sstevel@tonic-gate continue;
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate cp = line;
8321ca93273Seota /* sanity-check */
8337c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == 0) {
8347c478bd9Sstevel@tonic-gate (*errcb)(MP_IGNORING_LINE_ERR, ln);
8357c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8367c478bd9Sstevel@tonic-gate continue;
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate mp->mp_drvname = strdup(p);
8397c478bd9Sstevel@tonic-gate if (mp->mp_drvname == NULL) {
8407c478bd9Sstevel@tonic-gate (*errcb)(MP_ALLOC_ERR, strlen(p)+1);
8417c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8427c478bd9Sstevel@tonic-gate continue;
8437c478bd9Sstevel@tonic-gate } else if (t == '\n' || t == '\0') {
8447c478bd9Sstevel@tonic-gate (*errcb)(MP_IGNORING_LINE_ERR, ln);
8457c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8467c478bd9Sstevel@tonic-gate continue;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate if (t == ':') {
8497c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == 0) {
8507c478bd9Sstevel@tonic-gate (*errcb)(MP_IGNORING_LINE_ERR, ln);
8517c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate mp->mp_minorname = strdup(p);
8547c478bd9Sstevel@tonic-gate if (mp->mp_minorname == NULL) {
8557c478bd9Sstevel@tonic-gate (*errcb)(MP_ALLOC_ERR, strlen(p)+1);
8567c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8577c478bd9Sstevel@tonic-gate continue;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate } else {
8607c478bd9Sstevel@tonic-gate mp->mp_minorname = NULL;
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate if (t == '\n' || t == '\0') {
8647c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8657c478bd9Sstevel@tonic-gate (*errcb)(MP_IGNORING_LINE_ERR, ln);
8667c478bd9Sstevel@tonic-gate continue;
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == 0) {
8697c478bd9Sstevel@tonic-gate goto link;
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate if (getvalue(p, (int *)&mp->mp_mode) == 0) {
8727c478bd9Sstevel@tonic-gate goto link;
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate if (t == '\n' || t == '\0') { /* no owner or group */
8757c478bd9Sstevel@tonic-gate goto link;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == 0) {
8787c478bd9Sstevel@tonic-gate goto link;
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate mp->mp_owner = strdup(p);
8817c478bd9Sstevel@tonic-gate if (mp->mp_owner == NULL) {
8827c478bd9Sstevel@tonic-gate (*errcb)(MP_ALLOC_ERR, strlen(p)+1);
8837c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8847c478bd9Sstevel@tonic-gate continue;
8857c478bd9Sstevel@tonic-gate } else if (t == '\n' || t == '\0') { /* no group */
8867c478bd9Sstevel@tonic-gate goto link;
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, 0) == 0) {
8897c478bd9Sstevel@tonic-gate goto link;
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate mp->mp_group = strdup(p);
8927c478bd9Sstevel@tonic-gate if (mp->mp_group == NULL) {
8937c478bd9Sstevel@tonic-gate (*errcb)(MP_ALLOC_ERR, strlen(p)+1);
8947c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
8957c478bd9Sstevel@tonic-gate continue;
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate link:
8987c478bd9Sstevel@tonic-gate if (drvname != NULL) {
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate * We only want the minor perm entry for a
9017c478bd9Sstevel@tonic-gate * the named driver. The driver name is the
9027c478bd9Sstevel@tonic-gate * minor in the clone case.
9037c478bd9Sstevel@tonic-gate */
9047c478bd9Sstevel@tonic-gate if (strcmp(mp->mp_drvname, "clone") == 0) {
9057c478bd9Sstevel@tonic-gate if (mp->mp_minorname == NULL ||
9067c478bd9Sstevel@tonic-gate strcmp(drvname, mp->mp_minorname) != 0) {
9077c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
9087c478bd9Sstevel@tonic-gate continue;
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate } else {
9117c478bd9Sstevel@tonic-gate if (strcmp(drvname, mp->mp_drvname) != 0) {
9127c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mp);
9137c478bd9Sstevel@tonic-gate continue;
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate }
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate if (minor_perms == NULL) {
9187c478bd9Sstevel@tonic-gate minor_perms = mp;
9197c478bd9Sstevel@tonic-gate } else {
9207c478bd9Sstevel@tonic-gate mptail->mp_next = mp;
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate mptail = mp;
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate * Compute the uid's and gid's here - there are
9267c478bd9Sstevel@tonic-gate * fewer lines in the /etc/minor_perm file than there
9277c478bd9Sstevel@tonic-gate * are devices to be stat(2)ed. And almost every
9287c478bd9Sstevel@tonic-gate * device is 'root sys'. See 1135520.
9297c478bd9Sstevel@tonic-gate */
9307c478bd9Sstevel@tonic-gate if (mp->mp_owner == NULL ||
9317c478bd9Sstevel@tonic-gate strcmp(mp->mp_owner, DEFAULT_DEV_USER) == 0 ||
9327c478bd9Sstevel@tonic-gate (pw = getpwnam(mp->mp_owner)) == NULL) {
9337c478bd9Sstevel@tonic-gate mp->mp_uid = root_uid;
9347c478bd9Sstevel@tonic-gate } else {
9357c478bd9Sstevel@tonic-gate mp->mp_uid = pw->pw_uid;
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate
9387c478bd9Sstevel@tonic-gate if (mp->mp_group == NULL ||
9397c478bd9Sstevel@tonic-gate strcmp(mp->mp_group, DEFAULT_DEV_GROUP) == 0 ||
9407c478bd9Sstevel@tonic-gate (gp = getgrnam(mp->mp_group)) == NULL) {
9417c478bd9Sstevel@tonic-gate mp->mp_gid = sys_gid;
9427c478bd9Sstevel@tonic-gate } else {
9437c478bd9Sstevel@tonic-gate mp->mp_gid = gp->gr_gid;
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate if (fclose(pfd) == EOF) {
9487c478bd9Sstevel@tonic-gate (*errcb)(MP_FCLOSE_ERR, errno);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate
9517c478bd9Sstevel@tonic-gate return (minor_perms);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate struct mperm *
devfs_read_minor_perm(void (* errcb)(minorperm_err_t,int))9557c478bd9Sstevel@tonic-gate devfs_read_minor_perm(void (*errcb)(minorperm_err_t, int))
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate return (i_devfs_read_minor_perm(NULL, errcb));
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate static struct mperm *
i_devfs_read_minor_perm_by_driver(char * drvname,void (* errcb)(minorperm_err_t mp_err,int key))9617c478bd9Sstevel@tonic-gate i_devfs_read_minor_perm_by_driver(char *drvname,
962*f00128d8SToomas Soome void (*errcb)(minorperm_err_t mp_err, int key))
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate return (i_devfs_read_minor_perm(drvname, errcb));
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate /*
9687c478bd9Sstevel@tonic-gate * Free mperm list of entries
9697c478bd9Sstevel@tonic-gate */
9707c478bd9Sstevel@tonic-gate void
devfs_free_minor_perm(struct mperm * mplist)9717c478bd9Sstevel@tonic-gate devfs_free_minor_perm(struct mperm *mplist)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate struct mperm *mp, *next;
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate for (mp = mplist; mp != NULL; mp = next) {
9767c478bd9Sstevel@tonic-gate next = mp->mp_next;
9777c478bd9Sstevel@tonic-gate
9787c478bd9Sstevel@tonic-gate if (mp->mp_drvname)
9797c478bd9Sstevel@tonic-gate free(mp->mp_drvname);
9807c478bd9Sstevel@tonic-gate if (mp->mp_minorname)
9817c478bd9Sstevel@tonic-gate free(mp->mp_minorname);
9827c478bd9Sstevel@tonic-gate if (mp->mp_owner)
9837c478bd9Sstevel@tonic-gate free(mp->mp_owner);
9847c478bd9Sstevel@tonic-gate if (mp->mp_group)
9857c478bd9Sstevel@tonic-gate free(mp->mp_group);
9867c478bd9Sstevel@tonic-gate free(mp);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate
9907c478bd9Sstevel@tonic-gate static int
i_devfs_add_perm_entry(nvlist_t * nvl,struct mperm * mp)9917c478bd9Sstevel@tonic-gate i_devfs_add_perm_entry(nvlist_t *nvl, struct mperm *mp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate int err;
9947c478bd9Sstevel@tonic-gate
9957c478bd9Sstevel@tonic-gate err = nvlist_add_string(nvl, mp->mp_drvname, mp->mp_minorname);
9967c478bd9Sstevel@tonic-gate if (err != 0)
9977c478bd9Sstevel@tonic-gate return (err);
9987c478bd9Sstevel@tonic-gate
9997c478bd9Sstevel@tonic-gate err = nvlist_add_int32(nvl, "mode", (int32_t)mp->mp_mode);
10007c478bd9Sstevel@tonic-gate if (err != 0)
10017c478bd9Sstevel@tonic-gate return (err);
10027c478bd9Sstevel@tonic-gate
1003811e5678Scasper err = nvlist_add_uint32(nvl, "uid", mp->mp_uid);
10047c478bd9Sstevel@tonic-gate if (err != 0)
10057c478bd9Sstevel@tonic-gate return (err);
10067c478bd9Sstevel@tonic-gate
1007811e5678Scasper err = nvlist_add_uint32(nvl, "gid", mp->mp_gid);
10087c478bd9Sstevel@tonic-gate return (err);
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate static nvlist_t *
i_devfs_minor_perm_nvlist(struct mperm * mplist,void (* errcb)(minorperm_err_t,int))10127c478bd9Sstevel@tonic-gate i_devfs_minor_perm_nvlist(struct mperm *mplist,
1013*f00128d8SToomas Soome void (*errcb)(minorperm_err_t, int))
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate int err;
10167c478bd9Sstevel@tonic-gate struct mperm *mp;
10177c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL;
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate if ((err = nvlist_alloc(&nvl, 0, 0)) != 0) {
10207c478bd9Sstevel@tonic-gate (*errcb)(MP_NVLIST_ERR, err);
10217c478bd9Sstevel@tonic-gate return (NULL);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate
10247c478bd9Sstevel@tonic-gate for (mp = mplist; mp != NULL; mp = mp->mp_next) {
10257c478bd9Sstevel@tonic-gate if ((err = i_devfs_add_perm_entry(nvl, mp)) != 0) {
10267c478bd9Sstevel@tonic-gate (*errcb)(MP_NVLIST_ERR, err);
10277c478bd9Sstevel@tonic-gate nvlist_free(nvl);
10287c478bd9Sstevel@tonic-gate return (NULL);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate
10327c478bd9Sstevel@tonic-gate return (nvl);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate
10357c478bd9Sstevel@tonic-gate /*
10367c478bd9Sstevel@tonic-gate * Load all minor perm entries into the kernel
10377c478bd9Sstevel@tonic-gate * Done at boot time via devfsadm
10387c478bd9Sstevel@tonic-gate */
10397c478bd9Sstevel@tonic-gate int
devfs_load_minor_perm(struct mperm * mplist,void (* errcb)(minorperm_err_t,int))1040*f00128d8SToomas Soome devfs_load_minor_perm(struct mperm *mplist, void (*errcb)(minorperm_err_t, int))
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate int err;
10437c478bd9Sstevel@tonic-gate char *buf = NULL;
10447c478bd9Sstevel@tonic-gate size_t buflen;
10457c478bd9Sstevel@tonic-gate nvlist_t *nvl;
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate nvl = i_devfs_minor_perm_nvlist(mplist, errcb);
10487c478bd9Sstevel@tonic-gate if (nvl == NULL)
10497c478bd9Sstevel@tonic-gate return (-1);
10507c478bd9Sstevel@tonic-gate
10517c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0) != 0) {
10527c478bd9Sstevel@tonic-gate nvlist_free(nvl);
10537c478bd9Sstevel@tonic-gate return (-1);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate
10567c478bd9Sstevel@tonic-gate err = modctl(MODLOADMINORPERM, buf, buflen);
10577c478bd9Sstevel@tonic-gate nvlist_free(nvl);
10587c478bd9Sstevel@tonic-gate free(buf);
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate return (err);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate * Add/remove minor perm entry for a driver
10657c478bd9Sstevel@tonic-gate */
10667c478bd9Sstevel@tonic-gate static int
i_devfs_update_minor_perm(char * drv,int ctl,void (* errcb)(minorperm_err_t,int))10677c478bd9Sstevel@tonic-gate i_devfs_update_minor_perm(char *drv, int ctl,
1068*f00128d8SToomas Soome void (*errcb)(minorperm_err_t, int))
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate int err;
10717c478bd9Sstevel@tonic-gate char *buf;
10727c478bd9Sstevel@tonic-gate size_t buflen;
10737c478bd9Sstevel@tonic-gate nvlist_t *nvl;
10747c478bd9Sstevel@tonic-gate struct mperm *mplist;
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate mplist = i_devfs_read_minor_perm_by_driver(drv, errcb);
10777c478bd9Sstevel@tonic-gate
10787c478bd9Sstevel@tonic-gate nvl = i_devfs_minor_perm_nvlist(mplist, errcb);
10797c478bd9Sstevel@tonic-gate if (nvl == NULL)
10807c478bd9Sstevel@tonic-gate return (-1);
10817c478bd9Sstevel@tonic-gate
10827c478bd9Sstevel@tonic-gate buf = NULL;
10837c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0) != 0) {
10847c478bd9Sstevel@tonic-gate nvlist_free(nvl);
10857c478bd9Sstevel@tonic-gate return (-1);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate
10887c478bd9Sstevel@tonic-gate err = modctl(ctl, buf, buflen);
10897c478bd9Sstevel@tonic-gate nvlist_free(nvl);
10907c478bd9Sstevel@tonic-gate devfs_free_minor_perm(mplist);
10917c478bd9Sstevel@tonic-gate free(buf);
10927c478bd9Sstevel@tonic-gate
10937c478bd9Sstevel@tonic-gate return (err);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate int
devfs_add_minor_perm(char * drv,void (* errcb)(minorperm_err_t,int))1097*f00128d8SToomas Soome devfs_add_minor_perm(char *drv, void (*errcb)(minorperm_err_t, int))
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate return (i_devfs_update_minor_perm(drv, MODADDMINORPERM, errcb));
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate int
devfs_rm_minor_perm(char * drv,void (* errcb)(minorperm_err_t,int))1103*f00128d8SToomas Soome devfs_rm_minor_perm(char *drv, void (*errcb)(minorperm_err_t, int))
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate return (i_devfs_update_minor_perm(drv, MODREMMINORPERM, errcb));
11067c478bd9Sstevel@tonic-gate }
11071ca93273Seota
11081ca93273Seota /*
11091ca93273Seota * is_blank() returns 1 (true) if a line specified is composed of
11101ca93273Seota * whitespace characters only. otherwise, it returns 0 (false).
11111ca93273Seota *
11121ca93273Seota * Note. the argument (line) must be null-terminated.
11131ca93273Seota */
11141ca93273Seota static int
is_blank(char * line)11151ca93273Seota is_blank(char *line)
11161ca93273Seota {
11171ca93273Seota for (/* nothing */; *line != '\0'; line++)
11181ca93273Seota if (!isspace(*line))
11191ca93273Seota return (0);
11201ca93273Seota return (1);
11211ca93273Seota }
1122