libaura: Several fixes to fs predicates
[dragonfly.git] / usr.sbin / installer / libaura / fspred.c
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Chris Pressey <cpressey@catseye.mine.nu>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35
36 /*
37  * fspred.c
38  * Filesystem predicates.
39  */
40
41 #include <sys/stat.h>
42 #include <sys/param.h>
43 #include <sys/ucred.h>
44 #include <sys/mount.h>
45
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "fspred.h"
53
54 /** PREDICATES **/
55
56 static void
57 vstatmod(mode_t *m, int *error, const char *fmt, ...)
58 {
59         char *filename;
60         struct stat sb;
61         va_list vap;
62
63         memset(&sb, 0, sizeof(sb));
64
65         va_start(vap, fmt);
66         vasprintf(&filename, fmt, vap);
67         va_end(vap);
68
69         *error = stat(filename, &sb);
70         free(filename);
71
72         if (*error)
73                 *m = 0; /* Do not leak fake mode */
74         else
75                 *m = sb.st_mode;
76 }
77
78 int
79 is_dir(const char *fmt, ...)
80 {
81         va_list vap;
82         int error;
83         mode_t m;
84
85         va_start(vap, fmt);
86         vstatmod(&m, &error, fmt, vap);
87         va_end(vap);
88
89         if (error == 0)
90                 return(S_ISDIR(m));
91         else
92                 return(0);
93 }
94
95 int
96 is_file(const char *fmt, ...)
97 {
98         va_list vap;
99         int error;
100         mode_t m;
101
102         va_start(vap, fmt);
103         vstatmod(&m, &error, fmt, vap);
104         va_end(vap);
105
106         if (error == 0)
107                 return(S_ISREG(m) );
108         else
109                 return(0);
110
111 }
112
113 int
114 is_program(const char *fmt, ...)
115 {
116         char *filename;
117         struct stat sb;
118         va_list vap;
119         int error;
120         uid_t uid;
121         gid_t gid;
122
123         va_start(vap, fmt);
124         vasprintf(&filename, fmt, vap);
125         va_end(vap);
126
127         error = stat(filename, &sb);
128         free(filename);
129
130         uid = getuid();
131         gid = getgid();
132
133         /* Try to be more precise when identifying executable programs.
134          * Still this is subject to race conditions where the regular file
135          * might have its permissions/ownership changed during the test and
136          * thus provide inaccurate results.
137          * Also, effective uid/gid is not being checked.
138          */
139         if ((S_ISREG(sb.st_mode)) &&
140             ((sb.st_uid == uid && sb.st_mode & S_IXUSR) ||
141                 (sb.st_gid == gid && sb.st_mode & S_IXGRP) ||
142                 (sb.st_mode & S_IXOTH))) {
143                 return 1;
144         } else {
145                 return 0;
146         }
147 }
148
149 int
150 is_device(const char *fmt, ...)
151 {
152         va_list vap;
153         int error;
154         mode_t m;
155
156         va_start(vap, fmt);
157         vstatmod(&m, &error, fmt, vap);
158         va_end(vap);
159
160         if (error == 0)
161                 return(S_ISBLK(m) || S_ISCHR(m));
162         else
163                 return(0);
164 }
165
166 int
167 is_named_pipe(const char *fmt, ...)
168 {
169         va_list vap;
170         int error;
171         mode_t m;
172
173         va_start(vap, fmt);
174         vstatmod(&m, &error, fmt, vap);
175         va_end(vap);
176
177         if (error == 0)
178                 return(S_ISFIFO(m));
179         else
180                 return(0);
181 }
182
183 int
184 is_mountpoint_mounted(const char *mtpt)
185 {
186         struct statfs *mt_array, *mt_ptr;
187         int count;
188
189         count = getmntinfo(&mt_array, MNT_WAIT);
190         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
191                 if (strcmp(mt_ptr->f_mntonname, mtpt) == 0)
192                         return(1);
193         }
194         return(0);
195 }
196
197 int
198 is_device_mounted(const char *device)
199 {
200         struct statfs *mt_array, *mt_ptr;
201         int count;
202
203         count = getmntinfo(&mt_array, MNT_WAIT);
204         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
205                 if (strcmp(mt_ptr->f_mntfromname, device) == 0)
206                         return(1);
207         }
208         return(0);
209 }
210
211 int
212 is_any_slice_mounted(const char *diskdev)
213 {
214         struct statfs *mt_array, *mt_ptr;
215         int count;
216
217         count = getmntinfo(&mt_array, MNT_WAIT);
218         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
219                 if (strstr(mt_ptr->f_mntfromname, diskdev) ==
220                     mt_ptr->f_mntfromname)
221                         return(1);
222         }
223         return(0);
224 }