From: mdw Date: Mon, 26 Jul 1999 23:33:56 +0000 (+0000) Subject: Support code for new design. X-Git-Tag: 1.1.0~9 X-Git-Url: https://git.distorted.org.uk/~mdw/fwd/commitdiff_plain/3b7defab20dcc7c6dabb33d3395234a99ef157f3 Support code for new design. --- diff --git a/fattr.c b/fattr.c new file mode 100644 index 0000000..0406a11 --- /dev/null +++ b/fattr.c @@ -0,0 +1,236 @@ +/* -*-c-*- + * + * $Id: fattr.c,v 1.1 1999/07/26 23:33:56 mdw Exp $ + * + * Handling of file attributes + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the `fw' port forwarder. + * + * `fw' is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * `fw' is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with `fw'; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: fattr.c,v $ + * Revision 1.1 1999/07/26 23:33:56 mdw + * Support code for new design. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "conf.h" +#include "fattr.h" +#include "scan.h" + +/*----- Global variables --------------------------------------------------*/ + +fattr fattr_global; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @fattr_init@ --- * + * + * Arguments: @fattr *f@ = pointer to file attributes + * + * Returns: --- + * + * Use: Initializes a set of file attributes to default values. + */ + +void fattr_init(fattr *f) +{ + unsigned um = umask(0); + umask(um); + f->mode = 0666 & ~um; + f->uid = -1; + f->gid = -1; +} + +/* --- @fattr_option@ --- * + * + * Arguments: @scanner *sc@ = pointer to scanner to read + * @fattr *f@ = pointer to file attributes to set + * + * Returns: Whether the option was claimed. + * + * Use: Reads file attributes from a scanner. + */ + +int fattr_option(scanner *sc, fattr *f) +{ + CONF_BEGIN(sc, "fattr", "file attribute") + + /* --- Read a file mode specification --- */ + + if (strcmp(sc->d.buf, "mode") == 0) { + unsigned mode = 0; + + /* --- Gobble an optional `=' sign --- */ + + token(sc); + if (sc->t == '=') + token(sc); + + if (sc->t != CTOK_WORD) + error(sc, "parse error, expected file mode"); + + /* --- If it looks digitlike, read as octal --- */ + + if (isdigit((unsigned char)*sc->d.buf)) + mode = strtoul(sc->d.buf, 0, 8) & 07777; + + /* --- Otherise read as chmod-like characters --- */ + + else { + const char *p; + unsigned mask = 07777; + unsigned or = 1; + + /* --- Set the default from the umask --- */ + + { + unsigned um = umask(0); + umask(um); + mode = 0666 & ~um; + } + + /* --- Parse the characters --- * + * + * This is a particularly lenient implementation of the usual chmod- + * style mode language. + */ + + for (p = sc->d.buf; *p; p++) { + switch (*p) { + case ',': break; + + case 'a': mask = 07777; break; + case 'u': mask = 04700; break; + case 'g': mask = 02070; break; + case 'o': mask = 01007; break; + + case '=': mode &= ~mask; break; + case '-': or = 0; break; + case '+': or = 1; break; + +#define APPLY(m) if (or) mode |= ((m) & mask); else mode &= ~((m) & mask); + case 'r': APPLY(00444); break; + case 'w': APPLY(00222); break; + case 'x': APPLY(00111); break; + case 's': APPLY(06000); break; + case 't': APPLY(01000); break; +#undef APPLY + + default: error(sc, "unknown mode character `%c'", *p); + } + } + } + + token(sc); + f->mode = mode; + CONF_ACCEPT; + } + + /* --- Read a file uid specification --- */ + + if (strcmp(sc->d.buf, "uid") == 0 || + strcmp(sc->d.buf, "user") == 0 || + strcmp(sc->d.buf, "owner") == 0) { + token(sc); + if (sc->t == '=') + token(sc); + if (sc->t != CTOK_WORD) + error(sc, "parse error, expected user name or uid"); + if (isdigit((unsigned char)*sc->d.buf)) + f->uid = atoi(sc->d.buf); + else { + struct passwd *pw = getpwnam(sc->d.buf); + if (!pw) + error(sc, "unknown user name `%s'", sc->d.buf); + f->uid = pw->pw_uid; + } + token(sc); + CONF_ACCEPT; + } + + /* --- Read a file gid specification --- */ + + if (strcmp(sc->d.buf, "gid") == 0 || + strcmp(sc->d.buf, "group") == 0) { + token(sc); + if (sc->t == '=') + token(sc); + if (sc->t != CTOK_WORD) + error(sc, "parse error, expected group name or gid"); + if (isdigit((unsigned char)*sc->d.buf)) + f->gid = atoi(sc->d.buf); + else { + struct group *gr = getgrnam(sc->d.buf); + if (!gr) + error(sc, "unknown user name `%s'", sc->d.buf); + f->gid = gr->gr_gid; + } + token(sc); + CONF_ACCEPT; + } + + /* --- Nothing here for me --- */ + + CONF_END; +} + +/* --- @fattr_apply@ --- * + * + * Arguments: @const char *file@ = pointer to filename + * @fattr *f@ = pointer to attribute set + * + * Returns: @-1@ if it failed. + * + * Use: Applies file attributes to a file. For best results, try to + * create the file with the right permissions and so on. This + * call will fix everything up, but there are potential races + * which might catch you out if you're not careful. + */ + +int fattr_apply(const char *file, fattr *f) +{ + if (chown(file, f->uid, f->gid) == -1 || + chmod(file, f->mode) == -1) + return (-1); + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/fattr.h b/fattr.h new file mode 100644 index 0000000..499e85a --- /dev/null +++ b/fattr.h @@ -0,0 +1,110 @@ +/* -*-c-*- + * + * $Id: fattr.h,v 1.1 1999/07/26 23:33:56 mdw Exp $ + * + * Handling of file attributes + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the `fw' port forwarder. + * + * `fw' is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * `fw' is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with `fw'; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: fattr.h,v $ + * Revision 1.1 1999/07/26 23:33:56 mdw + * Support code for new design. + * + */ + +#ifndef FATTR_H +#define FATTR_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#include "config.h" + +#include + +#include "scan.h" + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct fattr { + unsigned mode; + uid_t uid; + gid_t gid; +} fattr; + +/*----- Global variables --------------------------------------------------*/ + +extern fattr fattr_global; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @fattr_init@ --- * + * + * Arguments: @fattr *f@ = pointer to file attributes + * + * Returns: --- + * + * Use: Initializes a set of file attributes to default values. + */ + +extern void fattr_init(fattr */*f*/); + +/* --- @fattr_option@ --- * + * + * Arguments: @scanner *sc@ = pointer to scanner to read + * @fattr *f@ = pointer to file attributes to set + * + * Returns: Whether the option was clamed. + * + * Use: Reads file attributes from a scanner. + */ + +extern int fattr_option(scanner */*sc*/, fattr */*f*/); + +/* --- @fattr_apply@ --- * + * + * Arguments: @const char *file@ = pointer to filename + * @fattr *f@ = pointer to attribute set + * + * Returns: @-1@ if it failed. + * + * Use: Applies file attributes to a file. For best results, try to + * create the file with the right permissions and so on. This + * call will fix everything up, but there are potential races + * which might catch you out if you're not careful. + */ + +extern int fattr_apply(const char */*file*/, fattr */*f*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/rlimits.h b/rlimits.h new file mode 100644 index 0000000..e06f66a --- /dev/null +++ b/rlimits.h @@ -0,0 +1,83 @@ +/* -*-c-*- + * + * $Id: rlimits.h,v 1.1 1999/07/26 23:33:56 mdw Exp $ + * + * List of resource limits known to mankind + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the `fw' port forwarder. + * + * `fw' is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * `fw' is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with `fw'; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: rlimits.h,v $ + * Revision 1.1 1999/07/26 23:33:56 mdw + * Support code for new design. + * + */ + +/*----- Magic -------------------------------------------------------------*/ + +#ifndef R +# error "Don't try including rlimits.h unless you know what you're doing" +#endif + +#if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE) +# define RLIMIT_NOFILE RLIMIT_OFILE +#endif + +#ifdef RLIMIT_CPU + R(RLIMIT_CPU, cpu) +#endif +#ifdef RLIMIT_FSIZE + R(RLIMIT_FSIZE, fsize) +#endif +#ifdef RLIMIT_DATA + R(RLIMIT_DATA, data) +#endif +#ifdef RLIMIT_STACK + R(RLIMIT_STACK, stack) +#endif +#ifdef RLIMIT_CORE + R(RLIMIT_CORE, core) +#endif +#ifdef RLIMIT_RSS + R(RLIMIT_RSS, rss) +#endif +#ifdef RLIMIT_NPROC + R(RLIMIT_NPROC, nproc) +#endif +#ifdef RLIMIT_NOFILE + R(RLIMIT_NOFILE, nofile) +#endif +#ifdef RLIMIT_MEMLOCK + R(RLIMIT_MEMLOCK, memlock) +#endif +#ifdef RLIMIT_VMEM + R(RLIMIT_VMEM, vmem) +#endif +#ifdef RLIMIT_AS + R(RLIMIT_AS, as) +#endif + +#undef R + +/*----- That's all, folks -------------------------------------------------*/