3 * $Id: fattr.c,v 1.4 2004/04/08 01:36:25 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 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
50 /*----- Global variables --------------------------------------------------*/
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @fattr_init@ --- *
58 * Arguments: @fattr *f@ = pointer to file attributes
62 * Use: Initializes a set of file attributes to default values.
65 void fattr_init(fattr
*f
)
67 unsigned um
= umask(0);
74 /* --- @fattr_option@ --- *
76 * Arguments: @scanner *sc@ = pointer to scanner to read
77 * @fattr *f@ = pointer to file attributes to set
79 * Returns: Whether the option was claimed.
81 * Use: Reads file attributes from a scanner.
84 int fattr_option(scanner
*sc
, fattr
*f
)
86 CONF_BEGIN(sc
, "fattr", "file attribute")
88 /* --- Read a file mode specification --- */
90 if (strcmp(sc
->d
.buf
, "mode") == 0) {
93 /* --- Gobble an optional `=' sign --- */
95 conf_undelim(sc
, 0, ",=+-");
100 if (sc
->t
!= CTOK_WORD
)
101 error(sc
, "parse error, expected file mode");
102 conf_undelim(sc
, 0, 0);
104 /* --- If it looks digitlike, read as octal --- */
106 if (isdigit((unsigned char)*sc
->d
.buf
))
107 mode
= strtoul(sc
->d
.buf
, 0, 8) & 07777;
109 /* --- Otherise read as chmod-like characters --- */
113 unsigned mask
= 04700;
117 /* --- Set the default from the umask --- */
120 unsigned um
= umask(0);
125 /* --- Parse the characters --- *
127 * This is a particularly lenient implementation of the usual chmod-
128 * style mode language.
131 for (p
= sc
->d
.buf
; *p
; p
++) {
135 case 'a': mask
= (mask
& state
) | 07777; state
= 07777; break;
136 case 'u': mask
= (mask
& state
) | 04700; state
= 07777; break;
137 case 'g': mask
= (mask
& state
) | 02070; state
= 07777; break;
138 case 'o': mask
= (mask
& state
) | 01007; state
= 07777; break;
140 case '=': mode
&= ~mask
; /* Drop through */
141 case '+': state
= 0; or = 1; break;
142 case '-': state
= 0; or = 0; break;
144 #define APPLY(m) if (or) mode |= ((m) & mask); else mode &= ~((m) & mask);
145 case 'r': state
= 0; APPLY(00444); break;
146 case 'w': state
= 0; APPLY(00222); break;
147 case 'x': state
= 0; APPLY(00111); break;
148 case 's': state
= 0; APPLY(06000); break;
149 case 't': state
= 0; APPLY(01000); break;
152 default: error(sc
, "unknown mode character `%c'", *p
);
162 /* --- Read a file uid specification --- */
164 if (strcmp(sc
->d
.buf
, "uid") == 0 ||
165 strcmp(sc
->d
.buf
, "user") == 0 ||
166 strcmp(sc
->d
.buf
, "owner") == 0) {
170 if (sc
->t
!= CTOK_WORD
)
171 error(sc
, "parse error, expected user name or uid");
172 if (isdigit((unsigned char)*sc
->d
.buf
))
173 f
->uid
= atoi(sc
->d
.buf
);
175 struct passwd
*pw
= getpwnam(sc
->d
.buf
);
177 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
184 /* --- Read a file gid specification --- */
186 if (strcmp(sc
->d
.buf
, "gid") == 0 ||
187 strcmp(sc
->d
.buf
, "group") == 0) {
191 if (sc
->t
!= CTOK_WORD
)
192 error(sc
, "parse error, expected group name or gid");
193 if (isdigit((unsigned char)*sc
->d
.buf
))
194 f
->gid
= atoi(sc
->d
.buf
);
196 struct group
*gr
= getgrnam(sc
->d
.buf
);
198 error(sc
, "unknown user name `%s'", sc
->d
.buf
);
205 /* --- Nothing here for me --- */
210 /* --- @fattr_apply@ --- *
212 * Arguments: @const char *file@ = pointer to filename
213 * @fattr *f@ = pointer to attribute set
215 * Returns: @-1@ if it failed.
217 * Use: Applies file attributes to a file. For best results, try to
218 * create the file with the right permissions and so on. This
219 * call will fix everything up, but there are potential races
220 * which might catch you out if you're not careful.
223 int fattr_apply(const char *file
, fattr
*f
)
225 if (chown(file
, f
->uid
, f
->gid
) == -1 ||
226 chmod(file
, f
->mode
) == -1)
231 /*----- That's all, folks -------------------------------------------------*/