3 * Handling of file attributes
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the `fw' port forwarder.
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Global variables --------------------------------------------------*/
33 /*----- Main code ---------------------------------------------------------*/
35 /* --- @fattr_init@ --- *
37 * Arguments: @fattr *f@ = pointer to file attributes
41 * Use: Initializes a set of file attributes to default values.
44 void fattr_init(fattr *f)
46 unsigned um = umask(0);
53 /* --- @fattr_option@ --- *
55 * Arguments: @scanner *sc@ = pointer to scanner to read
56 * @fattr *f@ = pointer to file attributes to set
58 * Returns: Whether the option was claimed.
60 * Use: Reads file attributes from a scanner.
63 int fattr_option(scanner *sc, fattr *f)
65 CONF_BEGIN(sc, "fattr", "file attribute")
67 /* --- Read a file mode specification --- */
69 if (strcmp(sc->d.buf, "mode") == 0) {
72 /* --- Gobble an optional `=' sign --- */
74 conf_undelim(sc, 0, ",=+-");
79 if (sc->t != CTOK_WORD)
80 error(sc, "parse error, expected file mode");
81 conf_undelim(sc, 0, 0);
83 /* --- If it looks digitlike, read as octal --- */
85 if (isdigit((unsigned char)*sc->d.buf))
86 mode = strtoul(sc->d.buf, 0, 8) & 07777;
88 /* --- Otherise read as chmod-like characters --- */
92 unsigned mask = 04700;
96 /* --- Set the default from the umask --- */
99 unsigned um = umask(0);
104 /* --- Parse the characters --- *
106 * This is a particularly lenient implementation of the usual chmod-
107 * style mode language.
110 for (p = sc->d.buf; *p; p++) {
114 case 'a': mask = (mask & state) | 07777; state = 07777; break;
115 case 'u': mask = (mask & state) | 04700; state = 07777; break;
116 case 'g': mask = (mask & state) | 02070; state = 07777; break;
117 case 'o': mask = (mask & state) | 01007; state = 07777; break;
119 case '=': mode &= ~mask; /* Drop through */
120 case '+': state = 0; or = 1; break;
121 case '-': state = 0; or = 0; break;
123 #define APPLY(m) if (or) mode |= ((m) & mask); else mode &= ~((m) & mask);
124 case 'r': state = 0; APPLY(00444); break;
125 case 'w': state = 0; APPLY(00222); break;
126 case 'x': state = 0; APPLY(00111); break;
127 case 's': state = 0; APPLY(06000); break;
128 case 't': state = 0; APPLY(01000); break;
131 default: error(sc, "unknown mode character `%c'", *p);
141 /* --- Read a file uid specification --- */
143 if (strcmp(sc->d.buf, "uid") == 0 ||
144 strcmp(sc->d.buf, "user") == 0 ||
145 strcmp(sc->d.buf, "owner") == 0) {
149 if (sc->t != CTOK_WORD)
150 error(sc, "parse error, expected user name or uid");
151 if (isdigit((unsigned char)*sc->d.buf))
152 f->uid = atoi(sc->d.buf);
154 struct passwd *pw = getpwnam(sc->d.buf);
156 error(sc, "unknown user name `%s'", sc->d.buf);
163 /* --- Read a file gid specification --- */
165 if (strcmp(sc->d.buf, "gid") == 0 ||
166 strcmp(sc->d.buf, "group") == 0) {
170 if (sc->t != CTOK_WORD)
171 error(sc, "parse error, expected group name or gid");
172 if (isdigit((unsigned char)*sc->d.buf))
173 f->gid = atoi(sc->d.buf);
175 struct group *gr = getgrnam(sc->d.buf);
177 error(sc, "unknown user name `%s'", sc->d.buf);
184 /* --- Nothing here for me --- */
189 /* --- @fattr_apply@ --- *
191 * Arguments: @const char *file@ = pointer to filename
192 * @fattr *f@ = pointer to attribute set
194 * Returns: @-1@ if it failed.
196 * Use: Applies file attributes to a file. For best results, try to
197 * create the file with the right permissions and so on. This
198 * call will fix everything up, but there are potential races
199 * which might catch you out if you're not careful.
202 int fattr_apply(const char *file, fattr *f)
204 if (chown(file, f->uid, f->gid) == -1 ||
205 chmod(file, f->mode) == -1)
210 /*----- That's all, folks -------------------------------------------------*/