X-Git-Url: https://git.distorted.org.uk/~mdw/fwd/blobdiff_plain/48bb1f76fd805b19aa32a374a8a9d8106ab7f420..3b7defab20dcc7c6dabb33d3395234a99ef157f3:/fattr.c 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 -------------------------------------------------*/