Import of pkgsrc-2014Q1

This commit is contained in:
2014-04-17 16:38:45 +02:00
parent 785076ae39
commit 9a8c06dafb
19365 changed files with 828089 additions and 278039 deletions

View File

@@ -0,0 +1,2 @@
This package provides the BSD install(1) program for compatability to
systems which do not have a native version.

View File

@@ -0,0 +1,24 @@
# $NetBSD: Makefile,v 1.2 2013/09/05 12:19:49 jperkin Exp $
DISTNAME= bsdinstall-20130905
CATEGORIES= sysutils
MASTER_SITES= # empty
DISTFILES= # empty
MAINTAINER= pkgsrc-users@NetBSD.org
COMMENT= Portable version of the BSD install(1) program
BOOTSTRAP_PKG= yes
USE_BSD_MAKEFILE= yes
USE_FEATURES= nbcompat
INSTALLATION_DIRS= bin ${PKGMANDIR}/cat1 ${PKGMANDIR}/man1
do-extract:
@${CP} -R ${FILESDIR} ${WRKSRC}
CPPFLAGS+= -D_PATH_DEVNULL=\"/dev/null\"
CPPFLAGS+= -DTARGET_STRIP=\"${TOOLS_PLATFORM.strip:Q}\"
.include "../../mk/bsd.pkg.mk"

View File

@@ -0,0 +1,4 @@
@comment $NetBSD: PLIST,v 1.2 2013/09/05 12:19:49 jperkin Exp $
bin/bsdinstall
man/cat1/bsdinstall.0
man/man1/bsdinstall.1

View File

@@ -0,0 +1,15 @@
# $NetBSD: Makefile,v 1.1 2013/08/28 11:42:36 jperkin Exp $
# NetBSD: Makefile,v 1.21 2006/12/16 12:59:17 bouyer Exp
# @(#)Makefile 8.1 (Berkeley) 6/6/93
.include <bsd.own.mk>
PROG= bsdinstall
SRCS= bsdinstall.c setmode.c
MAN= bsdinstall.1
LDADD+= -lnbcompat
COPTS.xinstall.c += -Wno-format-nonliteral
.include <bsd.prog.mk>

View File

@@ -0,0 +1,339 @@
.\" $NetBSD: bsdinstall.1,v 1.1 2013/08/28 11:42:36 jperkin Exp $
.\" NetBSD: install.1,v 1.46 2012/03/22 07:58:20 wiz Exp
.\"
.\" Copyright (c) 1987, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)install.1 8.1 (Berkeley) 6/6/93
.\"
.Dd May 1, 2009
.Dt BSDINSTALL 1
.Os
.Sh NAME
.Nm bsdinstall
.Nd install binaries
.Sh SYNOPSIS
.Nm
.Op Fl bcprsU
.Op Fl a Ar command
.Op Fl B Ar suffix
.Op Fl D Ar destdir
.Op Fl f Ar flags
.Op Fl g Ar group
.Op Fl h Ar hash
.Op Fl l Ar linkflags
.Op Fl M Ar metalog
.Op Fl m Ar mode
.Op Fl N Ar dbdir
.Op Fl o Ar owner
.Op Fl S Ar stripflag
.Op Fl T Ar tags
.Ar file1 file2
.Nm
.Op Fl bcprsU
.Op Fl a Ar command
.Op Fl B Ar suffix
.Op Fl D Ar destdir
.Op Fl f Ar flags
.Op Fl g Ar group
.Op Fl h Ar hash
.Op Fl l Ar linkflags
.Op Fl M Ar metalog
.Op Fl m Ar mode
.Op Fl N Ar dbdir
.Op Fl o Ar owner
.Op Fl S Ar stripflag
.Op Fl T Ar tags
.Ar file1 ...\&
.Ar fileN directory
.Nm
.Fl d
.Op Fl pU
.Op Fl a Ar command
.Op Fl D Ar destdir
.Op Fl g Ar group
.Op Fl M Ar metalog
.Op Fl m Ar mode
.Op Fl N Ar dbdir
.Op Fl o Ar owner
.Op Fl T Ar tags
.Ar directory ...\&
.Sh DESCRIPTION
The file(s) are copied
(or linked if the
.Fl l
option is specified) to the target file or directory.
If the destination is a directory, then the
.Ar file
is copied into
.Ar directory
with its original filename.
If the target file already exists, it is
either renamed to
.Ar file.old
if the
.Fl b
option is given
or overwritten
if permissions allow; an alternate backup suffix may be specified via the
.Fl B
option's argument.
.Pp
.Bl -tag -width XsXXstripflagsXX
.It Fl a Ar command
Run
.Ar command
on the target after installation and stripping
.Pq Fl s ,
but before
ownership, permissions or timestamps are set and before renaming
.Pq Fl r
occurs.
.Ar command
is invoked via the
.Xr sh 1
shell, allowing a single
.Fl a
argument be to specified to
.Nm
which the shell can then tokenize.
.It Fl B Ar suffix
Use
.Ar suffix
as the backup suffix if
.Fl b
is given.
If
.Ar suffix
contains a '%' sign, a numbered backup will be performed, and the
%-pattern will be expanded using
.Xr sprintf 3 ,
given an integer counter as the backup number.
The counter used starts from 0, and the first available name resulting
from the expansion is used.
.It Fl b
Backup any existing files before overwriting them by renaming
them to
.Ar file.old . See
.Fl B
for specifying a different backup suffix.
.It Fl c
Copy the file.
This is the default behavior; the flag is maintained for backwards
compatibility only.
.It Fl D Ar destdir
Specify the
.Ev DESTDIR
(top of the file hierarchy) that the items are installed in to.
If
.Fl M Ar metalog
is in use, a leading string of
.Dq Ar destdir
will be removed from the file names logged to the
.Ar metalog .
This option does not affect where the actual files are installed.
.It Fl d
Create directories.
Missing parent directories are created as required.
.It Fl f Ar flags
Specify the target's file flags.
(See
.Xr chflags 1
for a list of possible flags and their meanings.)
.It Fl g Ar group
Specify a group.
.It Fl h Ar hash
When copying, calculate the digest of the files with
.Ar hash
to store in the
.Fl M Ar metalog .
Supported digests:
.Bl -tag -width rmd160 -offset indent
.It Sy none
No hash.
This is the default.
.It Sy md5
The MD5 cryptographic message digest.
.It Sy rmd160
The RMD-160 cryptographic message digest.
.It Sy sha1
The SHA-1 cryptographic message digest.
.It Sy sha256
The 256-bits
.Tn SHA-2
cryptographic message digest of the file.
.It Sy sha384
The 384-bits
.Tn SHA-2
cryptographic message digest of the file.
.It Sy sha512
The 512-bits
.Tn SHA-2
cryptographic message digest of the file.
.El
.It Fl l Ar linkflags
Instead of copying the file make a link to the source.
The type of the link is determined by the
.Ar linkflags
argument.
Valid
.Ar linkflags
are:
.Ar a
(absolute),
.Ar r
(relative),
.Ar h
(hard),
.Ar s
(symbolic),
.Ar m
(mixed).
Absolute and relative have effect only for symbolic links.
Mixed links
are hard links for files on the same filesystem, symbolic otherwise.
.It Fl M Ar metalog
Write the metadata associated with each item installed to
.Ar metalog
in an
.Xr mtree 8
.Dq full path
specification line.
The metadata includes: the file name and file type, and depending upon
other options, the owner, group, file flags, modification time, and tags.
.It Fl m Ar mode
Specify an alternative mode.
The default mode is set to rwxr-xr-x (0755).
The specified mode may be either an octal or symbolic value; see
.Xr chmod 1
for a description of possible mode values.
.It Fl N Ar dbdir
Use the user database text file
.Pa master.passwd
and group database text file
.Pa group
from
.Ar dbdir ,
rather than using the results from the system's
.Xr getpwnam 3
and
.Xr getgrnam 3
(and related) library calls.
.It Fl o Ar owner
Specify an owner.
.It Fl p
Preserve the source files access and modification times.
.It Fl r
Install to a temporary file and then rename the file to its final destination
name.
This can be used for precious files, to avoid truncation of the original
when error conditions (filesystem full etc.) occur.
.It Fl S Ar stripflags
.Nm
passes
.Ar stripflags
as option arguments to
.Xr strip 1 .
When
.Fl S
is used,
.Xr strip 1
is invoked via the
.Xr sh 1
shell, allowing a single
.Fl S
argument be to specified to
.Nm
which the shell can then tokenize.
Normally,
.Nm
invokes
.Xr strip 1
directly.
This flag implies
.Fl s .
.It Fl s
.Nm
exec's the command
.Xr strip 1
to strip binaries so that bsdinstall can be portable over a large
number of systems and binary types.
If the environment variable
.Ev STRIP
is set, it is used as the
.Xr strip 1
program.
.It Fl T Ar tags
Specify the
.Xr mtree 8
tags to write out for the file when using
.Fl M Ar metalog .
.It Fl U
Indicate that bsdinstall is running unprivileged, and that it should not
try to change the owner, the group, or the file flags of the destination.
The information that would have been updated can be stored in a log
file with
.Fl M Ar metalog .
.El
.Pp
By default,
.Nm
preserves all file flags, with the exception of the ``nodump'' flag.
.Pp
The
.Nm
utility attempts to prevent copying a file onto itself.
.Pp
Installing
.Pa /dev/null
creates an empty file.
.Sh ENVIRONMENT
.Bl -tag -width Fl
.It Ev STRIP
The program used to strip installed binaries when the
.Fl s
option is used.
If unspecified,
.Pa /usr/bin/strip
is used.
.El
.Sh EXIT STATUS
.Ex -std
.Sh SEE ALSO
.Xr chflags 1 ,
.Xr chgrp 1 ,
.Xr chmod 1 ,
.Xr cp 1 ,
.Xr mv 1 ,
.Xr strip 1 ,
.Xr chown 8 ,
.Xr mtree 8
.Sh HISTORY
The
.Nm
utility appeared in
.Bx 4.2 .

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,158 @@
BSDINSTALL(1) BSD General Commands Manual BSDINSTALL(1)
NNAAMMEE
bbssddiinnssttaallll -- install binaries
SSYYNNOOPPSSIISS
bbssddiinnssttaallll [--bbccpprrssUU] [--aa _c_o_m_m_a_n_d] [--BB _s_u_f_f_i_x] [--DD _d_e_s_t_d_i_r] [--ff _f_l_a_g_s]
[--gg _g_r_o_u_p] [--hh _h_a_s_h] [--ll _l_i_n_k_f_l_a_g_s] [--MM _m_e_t_a_l_o_g] [--mm _m_o_d_e]
[--NN _d_b_d_i_r] [--oo _o_w_n_e_r] [--SS _s_t_r_i_p_f_l_a_g] [--TT _t_a_g_s] _f_i_l_e_1 _f_i_l_e_2
bbssddiinnssttaallll [--bbccpprrssUU] [--aa _c_o_m_m_a_n_d] [--BB _s_u_f_f_i_x] [--DD _d_e_s_t_d_i_r] [--ff _f_l_a_g_s]
[--gg _g_r_o_u_p] [--hh _h_a_s_h] [--ll _l_i_n_k_f_l_a_g_s] [--MM _m_e_t_a_l_o_g] [--mm _m_o_d_e]
[--NN _d_b_d_i_r] [--oo _o_w_n_e_r] [--SS _s_t_r_i_p_f_l_a_g] [--TT _t_a_g_s] _f_i_l_e_1 _._._. _f_i_l_e_N
_d_i_r_e_c_t_o_r_y
bbssddiinnssttaallll --dd [--ppUU] [--aa _c_o_m_m_a_n_d] [--DD _d_e_s_t_d_i_r] [--gg _g_r_o_u_p] [--MM _m_e_t_a_l_o_g]
[--mm _m_o_d_e] [--NN _d_b_d_i_r] [--oo _o_w_n_e_r] [--TT _t_a_g_s] _d_i_r_e_c_t_o_r_y _._._.
DDEESSCCRRIIPPTTIIOONN
The file(s) are copied (or linked if the --ll option is specified) to the
target file or directory. If the destination is a directory, then the
_f_i_l_e is copied into _d_i_r_e_c_t_o_r_y with its original filename. If the target
file already exists, it is either renamed to _f_i_l_e_._o_l_d if the --bb option is
given or overwritten if permissions allow; an alternate backup suffix may
be specified via the --BB option's argument.
--aa _c_o_m_m_a_n_d Run _c_o_m_m_a_n_d on the target after installation and strip-
ping (--ss), but before ownership, permissions or time-
stamps are set and before renaming (--rr) occurs.
_c_o_m_m_a_n_d is invoked via the sh(1) shell, allowing a sin-
gle --aa argument be to specified to bbssddiinnssttaallll which the
shell can then tokenize.
--BB _s_u_f_f_i_x Use _s_u_f_f_i_x as the backup suffix if --bb is given. If
_s_u_f_f_i_x contains a '%' sign, a numbered backup will be
performed, and the %-pattern will be expanded using
sprintf(3), given an integer counter as the backup num-
ber. The counter used starts from 0, and the first
available name resulting from the expansion is used.
--bb Backup any existing files before overwriting them by
renaming them to _f_i_l_e_._o_l_d. _S_e_e --BB for specifying a dif-
ferent backup suffix.
--cc Copy the file. This is the default behavior; the flag
is maintained for backwards compatibility only.
--DD _d_e_s_t_d_i_r Specify the DESTDIR (top of the file hierarchy) that
the items are installed in to. If --MM _m_e_t_a_l_o_g is in
use, a leading string of ``_d_e_s_t_d_i_r'' will be removed
from the file names logged to the _m_e_t_a_l_o_g. This option
does not affect where the actual files are installed.
--dd Create directories. Missing parent directories are
created as required.
--ff _f_l_a_g_s Specify the target's file flags. (See chflags(1) for a
list of possible flags and their meanings.)
--gg _g_r_o_u_p Specify a group.
--hh _h_a_s_h When copying, calculate the digest of the files with
_h_a_s_h to store in the --MM _m_e_t_a_l_o_g. Supported digests:
nnoonnee No hash. This is the default.
mmdd55 The MD5 cryptographic message digest.
rrmmdd116600 The RMD-160 cryptographic message digest.
sshhaa11 The SHA-1 cryptographic message digest.
sshhaa225566 The 256-bits SHA-2 cryptographic message
digest of the file.
sshhaa338844 The 384-bits SHA-2 cryptographic message
digest of the file.
sshhaa551122 The 512-bits SHA-2 cryptographic message
digest of the file.
--ll _l_i_n_k_f_l_a_g_s Instead of copying the file make a link to the source.
The type of the link is determined by the _l_i_n_k_f_l_a_g_s
argument. Valid _l_i_n_k_f_l_a_g_s are: _a (absolute), _r (rela-
tive), _h (hard), _s (symbolic), _m (mixed). Absolute and
relative have effect only for symbolic links. Mixed
links are hard links for files on the same filesystem,
symbolic otherwise.
--MM _m_e_t_a_l_o_g Write the metadata associated with each item installed
to _m_e_t_a_l_o_g in an mtree(8) ``full path'' specification
line. The metadata includes: the file name and file
type, and depending upon other options, the owner,
group, file flags, modification time, and tags.
--mm _m_o_d_e Specify an alternative mode. The default mode is set
to rwxr-xr-x (0755). The specified mode may be either
an octal or symbolic value; see chmod(1) for a descrip-
tion of possible mode values.
--NN _d_b_d_i_r Use the user database text file _m_a_s_t_e_r_._p_a_s_s_w_d and group
database text file _g_r_o_u_p from _d_b_d_i_r, rather than using
the results from the system's getpwnam(3) and
getgrnam(3) (and related) library calls.
--oo _o_w_n_e_r Specify an owner.
--pp Preserve the source files access and modification
times.
--rr Install to a temporary file and then rename the file to
its final destination name. This can be used for pre-
cious files, to avoid truncation of the original when
error conditions (filesystem full etc.) occur.
--SS _s_t_r_i_p_f_l_a_g_s bbssddiinnssttaallll passes _s_t_r_i_p_f_l_a_g_s as option arguments to
strip(1). When --SS is used, strip(1) is invoked via the
sh(1) shell, allowing a single --SS argument be to speci-
fied to bbssddiinnssttaallll which the shell can then tokenize.
Normally, bbssddiinnssttaallll invokes strip(1) directly. This
flag implies --ss.
--ss bbssddiinnssttaallll exec's the command strip(1) to strip bina-
ries so that bsdinstall can be portable over a large
number of systems and binary types. If the environment
variable STRIP is set, it is used as the strip(1) pro-
gram.
--TT _t_a_g_s Specify the mtree(8) tags to write out for the file
when using --MM _m_e_t_a_l_o_g.
--UU Indicate that bsdinstall is running unprivileged, and
that it should not try to change the owner, the group,
or the file flags of the destination. The information
that would have been updated can be stored in a log
file with --MM _m_e_t_a_l_o_g.
By default, bbssddiinnssttaallll preserves all file flags, with the exception of
the ``nodump'' flag.
The bbssddiinnssttaallll utility attempts to prevent copying a file onto itself.
Installing _/_d_e_v_/_n_u_l_l creates an empty file.
EENNVVIIRROONNMMEENNTT
STRIP The program used to strip installed binaries when the --ss
option is used. If unspecified, _/_u_s_r_/_b_i_n_/_s_t_r_i_p is used.
EEXXIITT SSTTAATTUUSS
The bbssddiinnssttaallll utility exits 0 on success, and >0 if an error occurs.
SSEEEE AALLSSOO
chflags(1), chgrp(1), chmod(1), cp(1), mv(1), strip(1), chown(8),
mtree(8)
HHIISSTTOORRYY
The bbssddiinnssttaallll utility appeared in 4.2BSD.
BSD May 1, 2009 BSD

View File

@@ -0,0 +1,500 @@
/* $NetBSD: setmode.c,v 1.2 2013/08/30 16:54:46 joerg Exp $ */
/* NetBSD: setmode.c,v 1.33 2012/03/21 14:28:32 christos Exp */
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Dave Borman at Cray Research, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(HAVE_NBCOMPAT_H)
#include <nbcompat/config.h>
#include <nbcompat/cdefs.h>
#else
#include <sys/cdefs.h>
#endif
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
#else
__RCSID("NetBSD: setmode.c,v 1.33 2012/03/21 14:28:32 christos Exp");
#endif
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#ifdef SETMODE_DEBUG
#include <stdio.h>
#endif
#if 0
#ifdef __weak_alias
__weak_alias(getmode,_getmode)
__weak_alias(setmode,_setmode)
#endif
#endif
#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
typedef struct bitcmd {
char cmd;
char cmd2;
mode_t bits;
} BITCMD;
#define CMD2_CLR 0x01
#define CMD2_SET 0x02
#define CMD2_GBITS 0x04
#define CMD2_OBITS 0x08
#define CMD2_UBITS 0x10
static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
static void compress_mode(BITCMD *);
#ifdef SETMODE_DEBUG
static void dumpmode(BITCMD *);
#endif
#ifndef S_ISTXT
#define S_ISTXT S_ISVTX
#endif
#ifndef _DIAGASSERT
#define _DIAGASSERT assert
#endif
/*
* Given the old mode and an array of bitcmd structures, apply the operations
* described in the bitcmd structures to the old mode, and return the new mode.
* Note that there is no '=' command; a strict assignment is just a '-' (clear
* bits) followed by a '+' (set bits).
*/
mode_t
getmode(const void *bbox, mode_t omode)
{
const BITCMD *set;
mode_t clrval, newmode, value;
_DIAGASSERT(bbox != NULL);
set = (const BITCMD *)bbox;
newmode = omode;
for (value = 0;; set++)
switch(set->cmd) {
/*
* When copying the user, group or other bits around, we "know"
* where the bits are in the mode so that we can do shifts to
* copy them around. If we don't use shifts, it gets real
* grundgy with lots of single bit checks and bit sets.
*/
case 'u':
value = (newmode & S_IRWXU) >> 6;
goto common;
case 'g':
value = (newmode & S_IRWXG) >> 3;
goto common;
case 'o':
value = newmode & S_IRWXO;
common: if (set->cmd2 & CMD2_CLR) {
clrval =
(set->cmd2 & CMD2_SET) ? S_IRWXO : value;
if (set->cmd2 & CMD2_UBITS)
newmode &= ~((clrval<<6) & set->bits);
if (set->cmd2 & CMD2_GBITS)
newmode &= ~((clrval<<3) & set->bits);
if (set->cmd2 & CMD2_OBITS)
newmode &= ~(clrval & set->bits);
}
if (set->cmd2 & CMD2_SET) {
if (set->cmd2 & CMD2_UBITS)
newmode |= (value<<6) & set->bits;
if (set->cmd2 & CMD2_GBITS)
newmode |= (value<<3) & set->bits;
if (set->cmd2 & CMD2_OBITS)
newmode |= value & set->bits;
}
break;
case '+':
newmode |= set->bits;
break;
case '-':
newmode &= ~set->bits;
break;
case 'X':
if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
newmode |= set->bits;
break;
case '\0':
default:
#ifdef SETMODE_DEBUG
(void)printf("getmode:%04o -> %04o\n", omode, newmode);
#endif
return (newmode);
}
}
#define ADDCMD(a, b, c, d) do { \
if (set >= endset) { \
BITCMD *newset; \
setlen += SET_LEN_INCR; \
newset = realloc(saveset, sizeof(BITCMD) * setlen); \
if (newset == NULL) \
goto out; \
set = newset + (set - saveset); \
saveset = newset; \
endset = newset + (setlen - 2); \
} \
set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \
} while (/*CONSTCOND*/0)
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
void *
setmode(const char *p)
{
int serrno;
char op, *ep;
BITCMD *set, *saveset, *endset;
sigset_t signset, sigoset;
mode_t mask, perm, permXbits, who;
long lval;
int equalopdone = 0; /* pacify gcc */
int setlen;
if (!*p) {
errno = EINVAL;
return NULL;
}
/*
* Get a copy of the mask for the permissions that are mask relative.
* Flip the bits, we want what's not set. Since it's possible that
* the caller is opening files inside a signal handler, protect them
* as best we can.
*/
sigfillset(&signset);
(void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
(void)umask(mask = umask(0));
mask = ~mask;
(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
setlen = SET_LEN + 2;
if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
return (NULL);
saveset = set;
endset = set + (setlen - 2);
/*
* If an absolute number, get it and return; disallow non-octal digits
* or illegal bits.
*/
if (isdigit((unsigned char)*p)) {
errno = 0;
lval = strtol(p, &ep, 8);
if (*ep) {
errno = EINVAL;
goto out;
}
if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
goto out;
if (lval & ~(STANDARD_BITS|S_ISTXT)) {
errno = EINVAL;
goto out;
}
perm = (mode_t)lval;
ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
set->cmd = 0;
return (saveset);
}
/*
* Build list of structures to set/clear/copy bits as described by
* each clause of the symbolic mode.
*/
for (;;) {
/* First, find out which bits might be modified. */
for (who = 0;; ++p) {
switch (*p) {
case 'a':
who |= STANDARD_BITS;
break;
case 'u':
who |= S_ISUID|S_IRWXU;
break;
case 'g':
who |= S_ISGID|S_IRWXG;
break;
case 'o':
who |= S_IRWXO;
break;
default:
goto getop;
}
}
getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
errno = EINVAL;
goto out;
}
if (op == '=')
equalopdone = 0;
who &= ~S_ISTXT;
for (perm = 0, permXbits = 0;; ++p) {
switch (*p) {
case 'r':
perm |= S_IRUSR|S_IRGRP|S_IROTH;
break;
case 's':
/*
* If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO))
perm |= S_ISUID|S_ISGID;
break;
case 't':
/*
* If specific bits where requested and
* only "other" bits ignore set-id.
*/
if (who == 0 || (who & ~S_IRWXO)) {
who |= S_ISTXT;
perm |= S_ISTXT;
}
break;
case 'w':
perm |= S_IWUSR|S_IWGRP|S_IWOTH;
break;
case 'X':
permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 'x':
perm |= S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 'u':
case 'g':
case 'o':
/*
* When ever we hit 'u', 'g', or 'o', we have
* to flush out any partial mode that we have,
* and then do the copying of the mode bits.
*/
if (perm) {
ADDCMD(op, who, perm, mask);
perm = 0;
}
if (op == '=')
equalopdone = 1;
if (op == '+' && permXbits) {
ADDCMD('X', who, permXbits, mask);
permXbits = 0;
}
ADDCMD(*p, who, op, mask);
break;
default:
/*
* Add any permissions that we haven't already
* done.
*/
if (perm || (op == '=' && !equalopdone)) {
if (op == '=')
equalopdone = 1;
ADDCMD(op, who, perm, mask);
perm = 0;
}
if (permXbits) {
ADDCMD('X', who, permXbits, mask);
permXbits = 0;
}
goto apply;
}
}
apply: if (!*p)
break;
if (*p != ',')
goto getop;
++p;
}
set->cmd = 0;
#ifdef SETMODE_DEBUG
(void)printf("Before compress_mode()\n");
dumpmode(saveset);
#endif
compress_mode(saveset);
#ifdef SETMODE_DEBUG
(void)printf("After compress_mode()\n");
dumpmode(saveset);
#endif
return (saveset);
out:
serrno = errno;
free(saveset);
errno = serrno;
return NULL;
}
static BITCMD *
addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
{
_DIAGASSERT(set != NULL);
switch (op) {
case '=':
set->cmd = '-';
set->bits = who ? who : STANDARD_BITS;
set++;
op = '+';
/* FALLTHROUGH */
case '+':
case '-':
case 'X':
set->cmd = op;
set->bits = (who ? who : mask) & oparg;
break;
case 'u':
case 'g':
case 'o':
set->cmd = op;
if (who) {
set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
((who & S_IRGRP) ? CMD2_GBITS : 0) |
((who & S_IROTH) ? CMD2_OBITS : 0);
set->bits = (mode_t)~0;
} else {
set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
set->bits = mask;
}
if (oparg == '+')
set->cmd2 |= CMD2_SET;
else if (oparg == '-')
set->cmd2 |= CMD2_CLR;
else if (oparg == '=')
set->cmd2 |= CMD2_SET|CMD2_CLR;
break;
}
return (set + 1);
}
#ifdef SETMODE_DEBUG
static void
dumpmode(BITCMD *set)
{
_DIAGASSERT(set != NULL);
for (; set->cmd; ++set)
(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
set->cmd2 & CMD2_CLR ? " CLR" : "",
set->cmd2 & CMD2_SET ? " SET" : "",
set->cmd2 & CMD2_UBITS ? " UBITS" : "",
set->cmd2 & CMD2_GBITS ? " GBITS" : "",
set->cmd2 & CMD2_OBITS ? " OBITS" : "");
}
#endif
/*
* Given an array of bitcmd structures, compress by compacting consecutive
* '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
* 'g' and 'o' commands continue to be separate. They could probably be
* compacted, but it's not worth the effort.
*/
static void
compress_mode(BITCMD *set)
{
BITCMD *nset;
int setbits, clrbits, Xbits, op;
_DIAGASSERT(set != NULL);
for (nset = set;;) {
/* Copy over any 'u', 'g' and 'o' commands. */
while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
*set++ = *nset++;
if (!op)
return;
}
for (setbits = clrbits = Xbits = 0;; nset++) {
if ((op = nset->cmd) == '-') {
clrbits |= nset->bits;
setbits &= ~nset->bits;
Xbits &= ~nset->bits;
} else if (op == '+') {
setbits |= nset->bits;
clrbits &= ~nset->bits;
Xbits &= ~nset->bits;
} else if (op == 'X')
Xbits |= nset->bits & ~setbits;
else
break;
}
if (clrbits) {
set->cmd = '-';
set->cmd2 = 0;
set->bits = clrbits;
set++;
}
if (setbits) {
set->cmd = '+';
set->cmd2 = 0;
set->bits = setbits;
set++;
}
if (Xbits) {
set->cmd = 'X';
set->cmd2 = 0;
set->bits = Xbits;
set++;
}
}
}