1*9f988b79SJean-Baptiste Boric /* $NetBSD: fattr.c,v 1.10 2009/06/19 12:55:45 stacktic Exp $ */
2*9f988b79SJean-Baptiste Boric
3*9f988b79SJean-Baptiste Boric /*-
4*9f988b79SJean-Baptiste Boric * Copyright (c) 2000 The NetBSD Foundation, Inc.
5*9f988b79SJean-Baptiste Boric * All rights reserved.
6*9f988b79SJean-Baptiste Boric *
7*9f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
8*9f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
9*9f988b79SJean-Baptiste Boric * are met:
10*9f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
11*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
12*9f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
13*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
14*9f988b79SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
15*9f988b79SJean-Baptiste Boric *
16*9f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*9f988b79SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*9f988b79SJean-Baptiste Boric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*9f988b79SJean-Baptiste Boric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*9f988b79SJean-Baptiste Boric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*9f988b79SJean-Baptiste Boric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*9f988b79SJean-Baptiste Boric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*9f988b79SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*9f988b79SJean-Baptiste Boric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*9f988b79SJean-Baptiste Boric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*9f988b79SJean-Baptiste Boric * POSSIBILITY OF SUCH DAMAGE.
27*9f988b79SJean-Baptiste Boric */
28*9f988b79SJean-Baptiste Boric
29*9f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
30*9f988b79SJean-Baptiste Boric #ifndef lint
31*9f988b79SJean-Baptiste Boric __RCSID("$NetBSD: fattr.c,v 1.10 2009/06/19 12:55:45 stacktic Exp $");
32*9f988b79SJean-Baptiste Boric #endif /* not lint */
33*9f988b79SJean-Baptiste Boric
34*9f988b79SJean-Baptiste Boric #include <sys/param.h>
35*9f988b79SJean-Baptiste Boric #include <sys/mount.h>
36*9f988b79SJean-Baptiste Boric #include <sys/stat.h>
37*9f988b79SJean-Baptiste Boric #include <err.h>
38*9f988b79SJean-Baptiste Boric #include <grp.h>
39*9f988b79SJean-Baptiste Boric #include <pwd.h>
40*9f988b79SJean-Baptiste Boric #include <stdio.h>
41*9f988b79SJean-Baptiste Boric #include <stdlib.h>
42*9f988b79SJean-Baptiste Boric #include <string.h>
43*9f988b79SJean-Baptiste Boric #include <unistd.h>
44*9f988b79SJean-Baptiste Boric
45*9f988b79SJean-Baptiste Boric #include "mountprog.h"
46*9f988b79SJean-Baptiste Boric
47*9f988b79SJean-Baptiste Boric int
a_num(const char * s,const char * id_type)48*9f988b79SJean-Baptiste Boric a_num(const char *s, const char *id_type)
49*9f988b79SJean-Baptiste Boric {
50*9f988b79SJean-Baptiste Boric int id;
51*9f988b79SJean-Baptiste Boric char *ep;
52*9f988b79SJean-Baptiste Boric
53*9f988b79SJean-Baptiste Boric id = strtol(s, &ep, 0);
54*9f988b79SJean-Baptiste Boric if (*ep || s == ep || id < 0)
55*9f988b79SJean-Baptiste Boric errx(1, "unknown %s id: %s", id_type, s);
56*9f988b79SJean-Baptiste Boric return id;
57*9f988b79SJean-Baptiste Boric }
58*9f988b79SJean-Baptiste Boric
59*9f988b79SJean-Baptiste Boric gid_t
a_gid(const char * s)60*9f988b79SJean-Baptiste Boric a_gid(const char *s)
61*9f988b79SJean-Baptiste Boric {
62*9f988b79SJean-Baptiste Boric struct group *gr;
63*9f988b79SJean-Baptiste Boric
64*9f988b79SJean-Baptiste Boric if ((gr = getgrnam(s)) != NULL)
65*9f988b79SJean-Baptiste Boric return gr->gr_gid;
66*9f988b79SJean-Baptiste Boric return a_num(s, "group");
67*9f988b79SJean-Baptiste Boric }
68*9f988b79SJean-Baptiste Boric
69*9f988b79SJean-Baptiste Boric uid_t
a_uid(const char * s)70*9f988b79SJean-Baptiste Boric a_uid(const char *s)
71*9f988b79SJean-Baptiste Boric {
72*9f988b79SJean-Baptiste Boric struct passwd *pw;
73*9f988b79SJean-Baptiste Boric
74*9f988b79SJean-Baptiste Boric if ((pw = getpwnam(s)) != NULL)
75*9f988b79SJean-Baptiste Boric return pw->pw_uid;
76*9f988b79SJean-Baptiste Boric return a_num(s, "user");
77*9f988b79SJean-Baptiste Boric }
78*9f988b79SJean-Baptiste Boric
79*9f988b79SJean-Baptiste Boric mode_t
a_mask(const char * s)80*9f988b79SJean-Baptiste Boric a_mask(const char *s)
81*9f988b79SJean-Baptiste Boric {
82*9f988b79SJean-Baptiste Boric int rv;
83*9f988b79SJean-Baptiste Boric char *ep;
84*9f988b79SJean-Baptiste Boric
85*9f988b79SJean-Baptiste Boric rv = strtol(s, &ep, 8);
86*9f988b79SJean-Baptiste Boric if (s == ep || *ep || rv < 0)
87*9f988b79SJean-Baptiste Boric errx(1, "invalid file mode: %s", s);
88*9f988b79SJean-Baptiste Boric return rv;
89*9f988b79SJean-Baptiste Boric }
90