2009-10-27 19:27:47 +03:00
|
|
|
/* $NetBSD: paxctl.c,v 1.12 2009/10/27 16:27:47 christos Exp $ */
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Elad Efrat <elad@NetBSD.org>
|
2008-08-11 14:58:02 +04:00
|
|
|
* Copyright (c) 2008 Christos Zoulas <christos@NetBSD.org>
|
2006-05-16 04:08:24 +04:00
|
|
|
* 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.
|
2007-01-09 15:49:36 +03:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2006-05-16 04:08:24 +04:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
2006-11-10 19:31:58 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2006-11-11 00:35:10 +03:00
|
|
|
#ifdef __RCSID
|
2009-10-27 19:27:47 +03:00
|
|
|
__RCSID("$NetBSD: paxctl.c,v 1.12 2009/10/27 16:27:47 christos Exp $");
|
2006-11-11 00:35:10 +03:00
|
|
|
#endif
|
2006-11-10 19:31:58 +03:00
|
|
|
#endif /* not lint */
|
2006-05-16 04:08:24 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-11-22 18:08:47 +03:00
|
|
|
#ifdef HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "../../sys/sys/exec_elf.h"
|
|
|
|
#else
|
2006-05-16 04:08:24 +04:00
|
|
|
#include <elf.h>
|
2006-11-22 18:08:47 +03:00
|
|
|
#endif
|
2006-05-16 04:08:24 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-12-15 22:44:37 +03:00
|
|
|
static void usage(void) __dead;
|
2008-08-11 14:58:02 +04:00
|
|
|
static uint32_t pax_flag(char);
|
|
|
|
static int pax_flags_sane(uint32_t);
|
|
|
|
static int pax_haveflags(uint32_t);
|
|
|
|
static void pax_printflags(const char *, int, uint32_t);
|
2006-05-16 06:37:24 +04:00
|
|
|
|
2007-06-25 00:35:36 +04:00
|
|
|
#ifndef ELF_NOTE_TYPE_PAX_TAG
|
|
|
|
/* NetBSD-specific note type: PaX. There should be 1 NOTE per executable.
|
|
|
|
section. desc is a 32 bit bitmask */
|
|
|
|
#define ELF_NOTE_TYPE_PAX_TAG 3
|
2007-12-24 23:05:24 +03:00
|
|
|
#define ELF_NOTE_PAX_MPROTECT 0x01 /* Force enable MPROTECT */
|
|
|
|
#define ELF_NOTE_PAX_NOMPROTECT 0x02 /* Force disable MPROTECT */
|
|
|
|
#define ELF_NOTE_PAX_GUARD 0x04 /* Force enable Segvguard */
|
|
|
|
#define ELF_NOTE_PAX_NOGUARD 0x08 /* Force disable Segvguard */
|
2007-12-27 01:16:31 +03:00
|
|
|
#define ELF_NOTE_PAX_ASLR 0x10 /* Force enable ASLR */
|
|
|
|
#define ELF_NOTE_PAX_NOASLR 0x20 /* Force disable ASLR */
|
2007-06-25 00:35:36 +04:00
|
|
|
#define ELF_NOTE_PAX_NAMESZ 4
|
|
|
|
#define ELF_NOTE_PAX_NAME "PaX\0"
|
|
|
|
#define ELF_NOTE_PAX_DESCSZ 4
|
2006-11-22 05:02:51 +03:00
|
|
|
#endif
|
2006-11-11 00:35:10 +03:00
|
|
|
#ifndef __arraycount
|
|
|
|
#define __arraycount(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static const struct paxflag {
|
2006-05-16 04:08:24 +04:00
|
|
|
char mark;
|
2006-11-10 19:31:58 +03:00
|
|
|
const char *name;
|
2008-08-11 14:58:02 +04:00
|
|
|
uint32_t bits;
|
2006-05-16 04:08:24 +04:00
|
|
|
} flags[] = {
|
2007-12-27 01:16:31 +03:00
|
|
|
{ 'A', "ASLR, explicit enable",
|
|
|
|
ELF_NOTE_PAX_ASLR },
|
|
|
|
{ 'a', "ASLR, explicit disable",
|
|
|
|
ELF_NOTE_PAX_NOASLR },
|
2007-06-25 00:35:36 +04:00
|
|
|
{ 'G', "Segvguard, explicit enable",
|
|
|
|
ELF_NOTE_PAX_GUARD },
|
|
|
|
{ 'g', "Segvguard, explicit disable",
|
|
|
|
ELF_NOTE_PAX_NOGUARD },
|
|
|
|
{ 'M', "mprotect(2) restrictions, explicit enable",
|
|
|
|
ELF_NOTE_PAX_MPROTECT },
|
|
|
|
{ 'm', "mprotect(2) restrictions, explicit disable",
|
|
|
|
ELF_NOTE_PAX_NOMPROTECT },
|
2006-05-16 04:08:24 +04:00
|
|
|
};
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static void
|
2006-05-16 04:08:24 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
2007-12-27 01:16:31 +03:00
|
|
|
(void)fprintf(stderr, "Usage: %s [ <-|+><A|a|G|g|M|m> ] <file> ...\n",
|
2006-11-11 00:35:10 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
"paxctl"
|
|
|
|
#else
|
|
|
|
getprogname()
|
|
|
|
#endif
|
|
|
|
);
|
2006-11-10 19:31:58 +03:00
|
|
|
exit(1);
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
static uint32_t
|
|
|
|
pax_flag(char s)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
if (s == '\0')
|
|
|
|
return (uint32_t)-1;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2008-08-11 14:58:02 +04:00
|
|
|
if (s == flags[i].mark)
|
2006-11-10 19:31:58 +03:00
|
|
|
return flags[i].bits;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
return (uint32_t)-1;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static int
|
2008-08-11 14:58:02 +04:00
|
|
|
pax_flags_sane(uint32_t f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags) - 1; i += 2) {
|
2009-01-18 13:01:34 +03:00
|
|
|
uint32_t g = flags[i].bits | flags[i+1].bits;
|
2006-11-10 19:31:58 +03:00
|
|
|
if ((f & g) == g)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
return 1;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static int
|
2008-08-11 14:58:02 +04:00
|
|
|
pax_haveflags(uint32_t f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2006-05-16 04:08:24 +04:00
|
|
|
if (f & flags[i].bits)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
static void
|
2008-08-11 14:58:02 +04:00
|
|
|
pax_printflags(const char *name, int many, uint32_t f)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-10 19:31:58 +03:00
|
|
|
size_t i;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
for (i = 0; i < __arraycount(flags); i++)
|
2007-12-08 22:56:54 +03:00
|
|
|
if (f & flags[i].bits) {
|
|
|
|
if (many)
|
|
|
|
(void)printf("%s: ", name);
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)printf(" %c: %s\n",
|
|
|
|
flags[i].mark, flags[i].name);
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
static int
|
2008-08-11 14:58:02 +04:00
|
|
|
process_one(const char *name, uint32_t add_flags, uint32_t del_flags,
|
|
|
|
int list, int many)
|
2006-05-16 04:08:24 +04:00
|
|
|
{
|
2006-11-11 00:35:10 +03:00
|
|
|
union {
|
|
|
|
Elf32_Ehdr h32;
|
|
|
|
Elf64_Ehdr h64;
|
|
|
|
} e;
|
|
|
|
union {
|
2009-10-27 19:27:47 +03:00
|
|
|
Elf32_Shdr h32;
|
|
|
|
Elf64_Shdr h64;
|
|
|
|
} s;
|
2007-06-25 00:35:36 +04:00
|
|
|
union {
|
|
|
|
Elf32_Nhdr h32;
|
|
|
|
Elf64_Nhdr h64;
|
|
|
|
} n;
|
2008-08-11 14:58:02 +04:00
|
|
|
#define SWAP(a) (swap == 0 ? (a) : \
|
|
|
|
/*LINTED*/(sizeof(a) == 1 ? (a) : \
|
|
|
|
/*LINTED*/(sizeof(a) == 2 ? bswap16(a) : \
|
|
|
|
/*LINTED*/(sizeof(a) == 4 ? bswap32(a) : \
|
|
|
|
/*LINTED*/(sizeof(a) == 8 ? bswap64(a) : (abort(), (a)))))))
|
|
|
|
#define EH(field) (size == 32 ? SWAP(e.h32.field) : SWAP(e.h64.field))
|
2009-10-27 19:27:47 +03:00
|
|
|
#define SH(field) (size == 32 ? SWAP(s.h32.field) : SWAP(s.h64.field))
|
2008-08-11 14:58:02 +04:00
|
|
|
#define NH(field) (size == 32 ? SWAP(n.h32.field) : SWAP(n.h64.field))
|
2009-10-27 19:27:47 +03:00
|
|
|
#define SHSIZE (size == 32 ? sizeof(s.h32) : sizeof(s.h64))
|
2007-06-25 00:35:36 +04:00
|
|
|
#define NHSIZE (size == 32 ? sizeof(n.h32) : sizeof(n.h64))
|
|
|
|
struct {
|
|
|
|
char name[ELF_NOTE_PAX_NAMESZ];
|
|
|
|
uint32_t flags;
|
|
|
|
} pax_tag;
|
2009-05-02 20:19:36 +04:00
|
|
|
int fd, size, ok = 0, flagged = 0, swap, error = 1;
|
2009-01-18 13:01:34 +03:00
|
|
|
size_t i;
|
2006-11-11 00:35:10 +03:00
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
fd = open(name, list ? O_RDONLY: O_RDWR, 0);
|
2006-05-16 04:08:24 +04:00
|
|
|
if (fd == -1) {
|
2007-12-08 22:56:54 +03:00
|
|
|
warn("Can't open `%s'", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
return error;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
if (read(fd, &e, sizeof(e)) != sizeof(e)) {
|
|
|
|
warn("Can't read ELF header from `%s'", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
if (memcmp(e.h32.e_ident, ELFMAG, SELFMAG) != 0) {
|
2008-08-11 14:58:02 +04:00
|
|
|
warnx("Bad ELF magic from `%s' (maybe it's not an ELF?)", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
if (e.h32.e_ehsize == sizeof(e.h32)) {
|
|
|
|
size = 32;
|
|
|
|
swap = 0;
|
|
|
|
} else if (e.h64.e_ehsize == sizeof(e.h64)) {
|
|
|
|
size = 64;
|
|
|
|
swap = 0;
|
|
|
|
} else if (bswap16(e.h32.e_ehsize) == sizeof(e.h32)) {
|
2006-11-11 00:35:10 +03:00
|
|
|
size = 32;
|
2008-08-11 14:58:02 +04:00
|
|
|
swap = 1;
|
|
|
|
} else if (bswap16(e.h64.e_ehsize) == sizeof(e.h64)) {
|
2006-11-11 00:35:10 +03:00
|
|
|
size = 64;
|
2008-08-11 14:58:02 +04:00
|
|
|
swap = 1;
|
|
|
|
} else {
|
|
|
|
warnx("Bad ELF size %d from `%s' (maybe it's not an ELF?)",
|
2007-12-08 22:56:54 +03:00
|
|
|
(int)e.h32.e_ehsize, name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-11-11 00:35:10 +03:00
|
|
|
|
2009-10-27 19:27:47 +03:00
|
|
|
for (i = 0; i < EH(e_shnum); i++) {
|
|
|
|
if ((size_t)pread(fd, &s, SHSIZE,
|
|
|
|
(off_t)EH(e_shoff) + i * SHSIZE) != SHSIZE) {
|
|
|
|
warn("Can't read section header data from `%s'", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2009-10-27 19:27:47 +03:00
|
|
|
if (SH(sh_type) != SHT_NOTE)
|
2006-11-10 19:31:58 +03:00
|
|
|
continue;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2009-10-27 19:27:47 +03:00
|
|
|
if (pread(fd, &n, NHSIZE, (off_t)SH(sh_offset)) != NHSIZE) {
|
2007-12-08 22:56:54 +03:00
|
|
|
warn("Can't read note header from `%s'", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2007-06-25 00:35:36 +04:00
|
|
|
if (NH(n_type) != ELF_NOTE_TYPE_PAX_TAG ||
|
|
|
|
NH(n_descsz) != ELF_NOTE_PAX_DESCSZ ||
|
|
|
|
NH(n_namesz) != ELF_NOTE_PAX_NAMESZ)
|
|
|
|
continue;
|
2009-10-27 19:27:47 +03:00
|
|
|
if (pread(fd, &pax_tag, sizeof(pax_tag), SH(sh_offset) + NHSIZE)
|
2007-12-08 22:56:54 +03:00
|
|
|
!= sizeof(pax_tag)) {
|
|
|
|
warn("Can't read pax_tag from `%s'", name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2007-06-25 00:35:36 +04:00
|
|
|
if (memcmp(pax_tag.name, ELF_NOTE_PAX_NAME,
|
2007-12-08 22:56:54 +03:00
|
|
|
sizeof(pax_tag.name)) != 0) {
|
|
|
|
warn("Unknown pax_tag name `%*.*s' from `%s'",
|
|
|
|
ELF_NOTE_PAX_NAMESZ, ELF_NOTE_PAX_NAMESZ,
|
|
|
|
pax_tag.name, name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-11-10 19:31:58 +03:00
|
|
|
ok = 1;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2006-11-10 19:31:58 +03:00
|
|
|
if (list) {
|
2008-08-11 14:58:02 +04:00
|
|
|
if (!pax_haveflags(SWAP(pax_tag.flags)))
|
2006-11-10 19:31:58 +03:00
|
|
|
break;
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
if (!pax_flags_sane(SWAP(pax_tag.flags)))
|
|
|
|
warnx("Current flags 0x%x don't make sense",
|
|
|
|
(uint32_t)SWAP(pax_tag.flags));
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
if (many)
|
|
|
|
(void)printf("%s: ", name);
|
2006-11-10 19:31:58 +03:00
|
|
|
(void)printf("PaX flags:\n");
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
pax_printflags(name, many, SWAP(pax_tag.flags));
|
2006-11-10 19:31:58 +03:00
|
|
|
flagged = 1;
|
|
|
|
break;
|
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
pax_tag.flags |= SWAP(add_flags);
|
|
|
|
pax_tag.flags &= SWAP(~del_flags);
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2008-08-11 14:58:02 +04:00
|
|
|
if (!pax_flags_sane(SWAP(pax_tag.flags))) {
|
|
|
|
warnx("New flags 0x%x don't make sense",
|
|
|
|
(uint32_t)SWAP(pax_tag.flags));
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2007-06-25 00:35:36 +04:00
|
|
|
if (pwrite(fd, &pax_tag, sizeof(pax_tag),
|
2009-10-27 19:27:47 +03:00
|
|
|
(off_t)SH(sh_offset) + NHSIZE) != sizeof(pax_tag))
|
2007-12-08 22:56:54 +03:00
|
|
|
warn("Can't modify flags on `%s'", name);
|
2006-11-10 19:31:58 +03:00
|
|
|
break;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
|
|
|
|
2007-12-08 22:56:54 +03:00
|
|
|
if (!ok) {
|
2009-10-27 19:27:47 +03:00
|
|
|
warnx("Could not find an ELF PaX SHT_NOTE section in `%s'",
|
2008-08-11 14:58:02 +04:00
|
|
|
name);
|
2009-05-02 20:19:36 +04:00
|
|
|
goto out;
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2006-05-16 04:08:24 +04:00
|
|
|
|
2009-05-02 20:19:36 +04:00
|
|
|
error = 0;
|
2007-12-08 22:56:54 +03:00
|
|
|
if (list && !flagged) {
|
|
|
|
if (many)
|
|
|
|
(void)printf("%s: ", name);
|
2006-09-28 00:01:50 +04:00
|
|
|
(void)printf("No PaX flags.\n");
|
2007-12-08 22:56:54 +03:00
|
|
|
}
|
2009-05-02 20:19:36 +04:00
|
|
|
out:
|
|
|
|
(void)close(fd);
|
|
|
|
return error;
|
2006-05-16 04:08:24 +04:00
|
|
|
}
|
2007-12-08 22:56:54 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *opt;
|
2008-08-11 14:58:02 +04:00
|
|
|
int i, list = 0, bad = 0, many, minus;
|
|
|
|
uint32_t add_flags = 0, del_flags = 0;
|
|
|
|
|
|
|
|
setprogname(argv[0]);
|
2007-12-08 22:56:54 +03:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
opt = argv[i];
|
|
|
|
|
|
|
|
if (*opt == '-' || *opt == '+') {
|
2008-08-11 14:58:02 +04:00
|
|
|
uint32_t t;
|
|
|
|
minus = 0;
|
|
|
|
|
|
|
|
while (*opt) {
|
|
|
|
switch (*opt) {
|
|
|
|
case '+':
|
|
|
|
minus = 0;
|
|
|
|
opt++;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
minus = 1;
|
|
|
|
opt++;
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
opt++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
t = pax_flag(*opt++);
|
|
|
|
if (t == (uint32_t)-1)
|
|
|
|
usage();
|
|
|
|
if (minus)
|
|
|
|
del_flags |= t;
|
|
|
|
else
|
|
|
|
add_flags |= t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-08 22:56:54 +03:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (add_flags || del_flags) {
|
|
|
|
if (list)
|
|
|
|
usage();
|
|
|
|
} else
|
|
|
|
list = 1;
|
|
|
|
|
|
|
|
many = i != argc - 1;
|
|
|
|
for (; i < argc; i++)
|
|
|
|
bad |= process_one(argv[i], add_flags, del_flags, list, many);
|
|
|
|
|
|
|
|
return bad ? EXIT_FAILURE : 0;
|
|
|
|
}
|