3 * $Id: fattr.c,v 1.2 1999/08/19 18:32:48 mdw Exp $
5 * Handling of file attributes
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/08/19 18:32:48 mdw
33 * Improve lexical analysis. In particular, `chmod' patterns don't have to
36 * Revision 1.1 1999/07/26 23:33:56 mdw
37 * Support code for new design.
41 /*----- Header files ------------------------------------------------------*/
51 #include <sys/types.h>
62 /*----- Global variables --------------------------------------------------*/
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @fattr_init@ --- *
70 * Arguments: @fattr *f@ = pointer to file attributes
74 * Use: Initializes a set of file attributes to default values.
77 void fattr_init(fattr *f)
79 unsigned um = umask(0);
86 /* --- @fattr_option@ --- *
88 * Arguments: @scanner *sc@ = pointer to scanner to read
89 * @fattr *f@ = pointer to file attributes to set
91 * Returns: Whether the option was claimed.
93 * Use: Reads file attributes from a scanner.
96 int fattr_option(scanner *sc, fattr *f)
98 CONF_BEGIN(sc, "fattr", "file attribute")
100 /* --- Read a file mode specification --- */
102 if (strcmp(sc->d.buf, "mode") == 0) {
105 /* --- Gobble an optional `=' sign --- */
112 if (sc->t != CTOK_WORD)
113 error(sc, "parse error, expected file mode");
116 /* --- If it looks digitlike, read as octal --- */
118 if (isdigit((unsigned char)*sc->d.buf))
119 mode = strtoul(sc->d.buf, 0, 8) & 07777;
121 /* --- Otherise read as chmod-like characters --- */
125 unsigned mask = 04700;
129 /* --- Set the default from the umask --- */
132 unsigned um = umask(0);
137 /* --- Parse the characters --- *
139 * This is a particularly lenient implementation of the usual chmod-
140 * style mode language.
143 for (p = sc->d.buf; *p; p++) {
147 case 'a': mask = (mask & state) | 07777; state = 07777; break;
148 case 'u': mask = (mask & state) | 04700; state = 07777; break;
149 case 'g': mask = (mask & state) | 02070; state = 07777; break;
150 case 'o': mask = (mask & state) | 01007; state = 07777; break;
152 case '=': mode &= ~mask; /* Drop through */
153 case '+': state = 0; or = 1; break;
154 case '-': state = 0; or = 0; break;
156 #define APPLY(m) if (or) mode |= ((m) & mask); else mode &= ~((m) & mask);
157 case 'r': state = 0; APPLY(00444); break;
158 case 'w': state = 0; APPLY(00222); break;
159 case 'x': state = 0; APPLY(00111); break;
160 case 's': state = 0; APPLY(06000); break;
161 case 't': state = 0; APPLY(01000); break;
164 default: error(sc, "unknown mode character `%c'", *p);
174 /* --- Read a file uid specification --- */
176 if (strcmp(sc->d.buf, "uid") == 0 ||
177 strcmp(sc->d.buf, "user") == 0 ||
178 strcmp(sc->d.buf, "owner") == 0) {
182 if (sc->t != CTOK_WORD)
183 error(sc, "parse error, expected user name or uid");
184 if (isdigit((unsigned char)*sc->d.buf))
185 f->uid = atoi(sc->d.buf);
187 struct passwd *pw = getpwnam(sc->d.buf);
189 error(sc, "unknown user name `%s'", sc->d.buf);
196 /* --- Read a file gid specification --- */
198 if (strcmp(sc->d.buf, "gid") == 0 ||
199 strcmp(sc->d.buf, "group") == 0) {
203 if (sc->t != CTOK_WORD)
204 error(sc, "parse error, expected group name or gid");
205 if (isdigit((unsigned char)*sc->d.buf))
206 f->gid = atoi(sc->d.buf);
208 struct group *gr = getgrnam(sc->d.buf);
210 error(sc, "unknown user name `%s'", sc->d.buf);
217 /* --- Nothing here for me --- */
222 /* --- @fattr_apply@ --- *
224 * Arguments: @const char *file@ = pointer to filename
225 * @fattr *f@ = pointer to attribute set
227 * Returns: @-1@ if it failed.
229 * Use: Applies file attributes to a file. For best results, try to
230 * create the file with the right permissions and so on. This
231 * call will fix everything up, but there are potential races
232 * which might catch you out if you're not careful.
235 int fattr_apply(const char *file, fattr *f)
237 if (chown(file, f->uid, f->gid) == -1 ||
238 chmod(file, f->mode) == -1)
243 /*----- That's all, folks -------------------------------------------------*/