1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #include <unistd.h>
29*0Sstevel@tonic-gate #include "bart.h"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate static int compare_manifests(FILE *rulesfile, char *control, char *test,
32*0Sstevel@tonic-gate boolean_t prog_fmt, uint_t flags);
33*0Sstevel@tonic-gate static void extract_fname_ftype(char *line, char *fname, char *type);
34*0Sstevel@tonic-gate static int report_add(char *fname, char *type);
35*0Sstevel@tonic-gate static int report_delete(char *fname, char *type);
36*0Sstevel@tonic-gate static int evaluate_differences(char *control_line, char *test_line,
37*0Sstevel@tonic-gate boolean_t prog_fmt, int flags);
38*0Sstevel@tonic-gate static void report_error(char *fname, char *type, char *ctrl_val,
39*0Sstevel@tonic-gate char *test_val, boolean_t prog_fmt);
40*0Sstevel@tonic-gate static int read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
41*0Sstevel@tonic-gate char **line, char *fname);
42*0Sstevel@tonic-gate static void parse_line(char *line, char *fname, char *type, char *size,
43*0Sstevel@tonic-gate char *mode, char *acl, char *mtime, char *uid, char *gid, char *contents,
44*0Sstevel@tonic-gate char *devnode, char *dest);
45*0Sstevel@tonic-gate static void init_default_flags(uint_t *flags);
46*0Sstevel@tonic-gate static void get_token(char *line, int *curr_pos, int line_len, char *buf,
47*0Sstevel@tonic-gate int buf_size);
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate int
bart_compare(int argc,char ** argv)50*0Sstevel@tonic-gate bart_compare(int argc, char **argv)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate char *control_fname, *test_fname;
53*0Sstevel@tonic-gate int c;
54*0Sstevel@tonic-gate FILE *rules_fd = NULL;
55*0Sstevel@tonic-gate uint_t glob_flags;
56*0Sstevel@tonic-gate boolean_t prog_fmt = B_FALSE;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate init_default_flags(&glob_flags);
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "pr:i:")) != EOF) {
61*0Sstevel@tonic-gate switch (c) {
62*0Sstevel@tonic-gate case 'p':
63*0Sstevel@tonic-gate prog_fmt = B_TRUE;
64*0Sstevel@tonic-gate break;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate case 'r':
67*0Sstevel@tonic-gate if (optarg == NULL)
68*0Sstevel@tonic-gate usage();
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate if (strcmp(optarg, "-") == 0)
71*0Sstevel@tonic-gate rules_fd = stdin;
72*0Sstevel@tonic-gate else
73*0Sstevel@tonic-gate rules_fd = fopen(optarg, "r");
74*0Sstevel@tonic-gate if (rules_fd == NULL) {
75*0Sstevel@tonic-gate perror(optarg);
76*0Sstevel@tonic-gate usage();
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate break;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate case 'i':
81*0Sstevel@tonic-gate process_glob_ignores(optarg, &glob_flags);
82*0Sstevel@tonic-gate break;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate case '?':
85*0Sstevel@tonic-gate default:
86*0Sstevel@tonic-gate usage();
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /* Make sure we have the right number of args */
91*0Sstevel@tonic-gate if ((optind + 2) != argc)
92*0Sstevel@tonic-gate usage();
93*0Sstevel@tonic-gate argv += optind;
94*0Sstevel@tonic-gate control_fname = argv[0];
95*0Sstevel@tonic-gate test_fname = argv[1];
96*0Sstevel@tonic-gate /* At this point, the filenames are sane, so do the comparison */
97*0Sstevel@tonic-gate return (compare_manifests(rules_fd, control_fname, test_fname,
98*0Sstevel@tonic-gate prog_fmt, glob_flags));
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate static int
compare_manifests(FILE * rulesfile,char * control,char * test,boolean_t prog_fmt,uint_t flags)102*0Sstevel@tonic-gate compare_manifests(FILE *rulesfile, char *control, char *test,
103*0Sstevel@tonic-gate boolean_t prog_fmt, uint_t flags)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate FILE *control_fd, *test_fd;
106*0Sstevel@tonic-gate char *control_line, *test_line, control_buf[BUF_SIZE],
107*0Sstevel@tonic-gate test_buf[BUF_SIZE], control_fname[PATH_MAX],
108*0Sstevel@tonic-gate control_type[TYPE_SIZE], test_fname[PATH_MAX],
109*0Sstevel@tonic-gate test_type[TYPE_SIZE];
110*0Sstevel@tonic-gate int control_pos, test_pos, ret, fname_cmp, return_status;
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate return_status = EXIT;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate return_status = read_rules(rulesfile, "", flags, 0);
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate control_fd = fopen(control, "r");
117*0Sstevel@tonic-gate if (control_fd == NULL) {
118*0Sstevel@tonic-gate perror(control);
119*0Sstevel@tonic-gate return (FATAL_EXIT);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate test_fd = fopen(test, "r");
123*0Sstevel@tonic-gate if (test_fd == NULL) {
124*0Sstevel@tonic-gate perror(test);
125*0Sstevel@tonic-gate return (FATAL_EXIT);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate control_pos = read_manifest_line(control_fd, control_buf,
129*0Sstevel@tonic-gate BUF_SIZE, 0, &control_line, control);
130*0Sstevel@tonic-gate test_pos = read_manifest_line(test_fd, test_buf, BUF_SIZE, 0,
131*0Sstevel@tonic-gate &test_line, test);
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate while ((control_pos != -1) && (test_pos != -1)) {
134*0Sstevel@tonic-gate ret = strcmp(control_line, test_line);
135*0Sstevel@tonic-gate if (ret == 0) {
136*0Sstevel@tonic-gate /* Lines compare OK, just read the next lines.... */
137*0Sstevel@tonic-gate control_pos = read_manifest_line(control_fd,
138*0Sstevel@tonic-gate control_buf, BUF_SIZE, control_pos, &control_line,
139*0Sstevel@tonic-gate control);
140*0Sstevel@tonic-gate test_pos = read_manifest_line(test_fd, test_buf,
141*0Sstevel@tonic-gate BUF_SIZE, test_pos, &test_line, test);
142*0Sstevel@tonic-gate continue;
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate * Something didn't compare properly.
147*0Sstevel@tonic-gate */
148*0Sstevel@tonic-gate extract_fname_ftype(control_line, control_fname, control_type);
149*0Sstevel@tonic-gate extract_fname_ftype(test_line, test_fname, test_type);
150*0Sstevel@tonic-gate fname_cmp = strcmp(control_fname, test_fname);
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate if (fname_cmp == 0) {
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate * Filenames were the same, see what was
155*0Sstevel@tonic-gate * different and continue.
156*0Sstevel@tonic-gate */
157*0Sstevel@tonic-gate if (evaluate_differences(control_line, test_line,
158*0Sstevel@tonic-gate prog_fmt, flags) != 0)
159*0Sstevel@tonic-gate return_status = WARNING_EXIT;
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate control_pos = read_manifest_line(control_fd,
162*0Sstevel@tonic-gate control_buf, BUF_SIZE, control_pos, &control_line,
163*0Sstevel@tonic-gate control);
164*0Sstevel@tonic-gate test_pos = read_manifest_line(test_fd, test_buf,
165*0Sstevel@tonic-gate BUF_SIZE, test_pos, &test_line, test);
166*0Sstevel@tonic-gate } else if (fname_cmp > 0) {
167*0Sstevel@tonic-gate /* Filenames were different, a files was ADDED */
168*0Sstevel@tonic-gate if (report_add(test_fname, test_type)) {
169*0Sstevel@tonic-gate report_error(test_fname, ADD_KEYWORD, NULL,
170*0Sstevel@tonic-gate NULL, prog_fmt);
171*0Sstevel@tonic-gate return_status = WARNING_EXIT;
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate test_pos = read_manifest_line(test_fd, test_buf,
174*0Sstevel@tonic-gate BUF_SIZE, test_pos, &test_line, test);
175*0Sstevel@tonic-gate } else if (fname_cmp < 0) {
176*0Sstevel@tonic-gate /* Filenames were different, a files was DELETED */
177*0Sstevel@tonic-gate if (report_delete(control_fname, control_type)) {
178*0Sstevel@tonic-gate report_error(control_fname, DELETE_KEYWORD,
179*0Sstevel@tonic-gate NULL, NULL, prog_fmt);
180*0Sstevel@tonic-gate return_status = WARNING_EXIT;
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate control_pos = read_manifest_line(control_fd,
183*0Sstevel@tonic-gate control_buf, BUF_SIZE, control_pos, &control_line,
184*0Sstevel@tonic-gate control);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate * Entering this while loop means files were DELETED from the test
190*0Sstevel@tonic-gate * manifest.
191*0Sstevel@tonic-gate */
192*0Sstevel@tonic-gate while (control_pos != -1) {
193*0Sstevel@tonic-gate (void) sscanf(control_line, "%1023s", control_fname);
194*0Sstevel@tonic-gate if (report_delete(control_fname, control_type)) {
195*0Sstevel@tonic-gate report_error(control_fname, DELETE_KEYWORD, NULL,
196*0Sstevel@tonic-gate NULL, prog_fmt);
197*0Sstevel@tonic-gate return_status = WARNING_EXIT;
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate control_pos = read_manifest_line(control_fd, control_buf,
200*0Sstevel@tonic-gate BUF_SIZE, control_pos, &control_line, control);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate * Entering this while loop means files were ADDED to the test
205*0Sstevel@tonic-gate * manifest.
206*0Sstevel@tonic-gate */
207*0Sstevel@tonic-gate while (test_pos != -1) {
208*0Sstevel@tonic-gate (void) sscanf(test_line, "%1023s", test_fname);
209*0Sstevel@tonic-gate if (report_add(test_fname, test_type)) {
210*0Sstevel@tonic-gate report_error(test_fname, ADD_KEYWORD, NULL,
211*0Sstevel@tonic-gate NULL, prog_fmt);
212*0Sstevel@tonic-gate return_status = WARNING_EXIT;
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate test_pos = read_manifest_line(test_fd, test_buf,
215*0Sstevel@tonic-gate BUF_SIZE, test_pos, &test_line, test);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate (void) fclose(control_fd);
219*0Sstevel@tonic-gate (void) fclose(test_fd);
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate /* For programmatic mode, add a newline for cosmetic reasons */
222*0Sstevel@tonic-gate if (prog_fmt && (return_status != 0))
223*0Sstevel@tonic-gate (void) printf("\n");
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate return (return_status);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate static void
parse_line(char * line,char * fname,char * type,char * size,char * mode,char * acl,char * mtime,char * uid,char * gid,char * contents,char * devnode,char * dest)229*0Sstevel@tonic-gate parse_line(char *line, char *fname, char *type, char *size, char *mode,
230*0Sstevel@tonic-gate char *acl, char *mtime, char *uid, char *gid, char *contents, char *devnode,
231*0Sstevel@tonic-gate char *dest)
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate int pos, line_len;
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate line_len = strlen(line);
236*0Sstevel@tonic-gate pos = 0;
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate get_token(line, &pos, line_len, fname, PATH_MAX);
239*0Sstevel@tonic-gate get_token(line, &pos, line_len, type, TYPE_SIZE);
240*0Sstevel@tonic-gate get_token(line, &pos, line_len, size, MISC_SIZE);
241*0Sstevel@tonic-gate get_token(line, &pos, line_len, mode, MISC_SIZE);
242*0Sstevel@tonic-gate get_token(line, &pos, line_len, acl, ACL_SIZE);
243*0Sstevel@tonic-gate get_token(line, &pos, line_len, mtime, MISC_SIZE);
244*0Sstevel@tonic-gate get_token(line, &pos, line_len, uid, MISC_SIZE);
245*0Sstevel@tonic-gate get_token(line, &pos, line_len, gid, MISC_SIZE);
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate /* Reset these fields... */
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate *contents = NULL;
250*0Sstevel@tonic-gate *devnode = '\0';
251*0Sstevel@tonic-gate *dest = '\0';
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate /* Handle filetypes which have a last field..... */
254*0Sstevel@tonic-gate if (type[0] == 'F')
255*0Sstevel@tonic-gate get_token(line, &pos, line_len, contents, PATH_MAX);
256*0Sstevel@tonic-gate else if ((type[0] == 'B') || (type[0] == 'C'))
257*0Sstevel@tonic-gate get_token(line, &pos, line_len, devnode, PATH_MAX);
258*0Sstevel@tonic-gate else if (type[0] == 'L')
259*0Sstevel@tonic-gate get_token(line, &pos, line_len, dest, PATH_MAX);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate static void
get_token(char * line,int * curr_pos,int line_len,char * buf,int buf_size)263*0Sstevel@tonic-gate get_token(char *line, int *curr_pos, int line_len, char *buf, int buf_size)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate int cnt = 0;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate while (isspace(line[*curr_pos]) && (*curr_pos < line_len))
268*0Sstevel@tonic-gate (*curr_pos)++;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate while (!isspace(line[*curr_pos]) &&
271*0Sstevel@tonic-gate (*curr_pos < line_len) && (cnt < (buf_size-1))) {
272*0Sstevel@tonic-gate buf[cnt] = line[*curr_pos];
273*0Sstevel@tonic-gate (*curr_pos)++;
274*0Sstevel@tonic-gate cnt++;
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate buf[cnt] = '\0';
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate /*
280*0Sstevel@tonic-gate * Utility function: extract fname and type from this line
281*0Sstevel@tonic-gate */
282*0Sstevel@tonic-gate static void
extract_fname_ftype(char * line,char * fname,char * type)283*0Sstevel@tonic-gate extract_fname_ftype(char *line, char *fname, char *type)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate int line_len, pos;
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate pos = 0;
288*0Sstevel@tonic-gate line_len = strlen(line);
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate get_token(line, &pos, line_len, fname, PATH_MAX);
291*0Sstevel@tonic-gate get_token(line, &pos, line_len, type, TYPE_SIZE);
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate * Utility function: tells us whether or not this addition should be reported
296*0Sstevel@tonic-gate *
297*0Sstevel@tonic-gate * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
298*0Sstevel@tonic-gate * reported.
299*0Sstevel@tonic-gate */
300*0Sstevel@tonic-gate static int
report_add(char * fname,char * type)301*0Sstevel@tonic-gate report_add(char *fname, char *type)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate struct rule *rule_ptr;
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate rule_ptr = check_rules(fname, type[0]);
306*0Sstevel@tonic-gate if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_ADD))
307*0Sstevel@tonic-gate return (1);
308*0Sstevel@tonic-gate else
309*0Sstevel@tonic-gate return (0);
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate * Utility function: tells us whether or not this deletion should be reported
314*0Sstevel@tonic-gate *
315*0Sstevel@tonic-gate * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
316*0Sstevel@tonic-gate * reported.
317*0Sstevel@tonic-gate */
318*0Sstevel@tonic-gate static int
report_delete(char * fname,char * type)319*0Sstevel@tonic-gate report_delete(char *fname, char *type)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate struct rule *rule_ptr;
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate rule_ptr = check_rules(fname, type[0]);
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_DELETE))
326*0Sstevel@tonic-gate return (1);
327*0Sstevel@tonic-gate else
328*0Sstevel@tonic-gate return (0);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate
331*0Sstevel@tonic-gate /*
332*0Sstevel@tonic-gate * This function takes in the two entries, which have been flagged as
333*0Sstevel@tonic-gate * different, breaks them up and reports discrepancies. Note, discrepancies
334*0Sstevel@tonic-gate * are affected by the 'CHECK' and 'IGNORE' stanzas which may apply to
335*0Sstevel@tonic-gate * these entries.
336*0Sstevel@tonic-gate *
337*0Sstevel@tonic-gate * Returns the number of discrepancies reported.
338*0Sstevel@tonic-gate */
339*0Sstevel@tonic-gate static int
evaluate_differences(char * control_line,char * test_line,boolean_t prog_fmt,int flags)340*0Sstevel@tonic-gate evaluate_differences(char *control_line, char *test_line,
341*0Sstevel@tonic-gate boolean_t prog_fmt, int flags)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate char ctrl_fname[PATH_MAX], test_fname[PATH_MAX],
344*0Sstevel@tonic-gate ctrl_type[TYPE_SIZE], test_type[TYPE_SIZE],
345*0Sstevel@tonic-gate ctrl_size[MISC_SIZE], ctrl_mode[MISC_SIZE],
346*0Sstevel@tonic-gate ctrl_acl[ACL_SIZE], ctrl_mtime[MISC_SIZE],
347*0Sstevel@tonic-gate ctrl_uid[MISC_SIZE], ctrl_gid[MISC_SIZE],
348*0Sstevel@tonic-gate ctrl_dest[PATH_MAX], ctrl_contents[PATH_MAX],
349*0Sstevel@tonic-gate ctrl_devnode[PATH_MAX], test_size[MISC_SIZE],
350*0Sstevel@tonic-gate test_mode[MISC_SIZE], test_acl[ACL_SIZE],
351*0Sstevel@tonic-gate test_mtime[MISC_SIZE], test_uid[MISC_SIZE],
352*0Sstevel@tonic-gate test_gid[MISC_SIZE], test_dest[PATH_MAX],
353*0Sstevel@tonic-gate test_contents[PATH_MAX], test_devnode[PATH_MAX],
354*0Sstevel@tonic-gate *tag;
355*0Sstevel@tonic-gate int ret_val;
356*0Sstevel@tonic-gate struct rule *rule_ptr;
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gate ret_val = 0;
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate parse_line(control_line, ctrl_fname, ctrl_type, ctrl_size, ctrl_mode,
361*0Sstevel@tonic-gate ctrl_acl, ctrl_mtime, ctrl_uid, ctrl_gid, ctrl_contents,
362*0Sstevel@tonic-gate ctrl_devnode, ctrl_dest);
363*0Sstevel@tonic-gate
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate * Now we know the fname and type, let's get the rule that matches this
366*0Sstevel@tonic-gate * manifest entry. If there is a match, make sure to setup the
367*0Sstevel@tonic-gate * correct reporting flags.
368*0Sstevel@tonic-gate */
369*0Sstevel@tonic-gate rule_ptr = check_rules(ctrl_fname, ctrl_type[0]);
370*0Sstevel@tonic-gate if (rule_ptr != NULL)
371*0Sstevel@tonic-gate flags = rule_ptr->attr_list;
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate parse_line(test_line, test_fname, test_type, test_size, test_mode,
374*0Sstevel@tonic-gate test_acl, test_mtime, test_uid, test_gid, test_contents,
375*0Sstevel@tonic-gate test_devnode, test_dest);
376*0Sstevel@tonic-gate
377*0Sstevel@tonic-gate /*
378*0Sstevel@tonic-gate * Report the errors based upon which keywords have been set by
379*0Sstevel@tonic-gate * the user.
380*0Sstevel@tonic-gate */
381*0Sstevel@tonic-gate if ((flags & ATTR_TYPE) && (ctrl_type[0] != test_type[0])) {
382*0Sstevel@tonic-gate report_error(ctrl_fname, TYPE_KEYWORD, ctrl_type,
383*0Sstevel@tonic-gate test_type, prog_fmt);
384*0Sstevel@tonic-gate ret_val++;
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate
387*0Sstevel@tonic-gate if ((flags & ATTR_SIZE) && (strcmp(ctrl_size, test_size) != 0)) {
388*0Sstevel@tonic-gate report_error(ctrl_fname, SIZE_KEYWORD, ctrl_size,
389*0Sstevel@tonic-gate test_size, prog_fmt);
390*0Sstevel@tonic-gate ret_val++;
391*0Sstevel@tonic-gate }
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate if ((flags & ATTR_MODE) && (strcmp(ctrl_mode, test_mode) != 0)) {
394*0Sstevel@tonic-gate report_error(ctrl_fname, MODE_KEYWORD, ctrl_mode,
395*0Sstevel@tonic-gate test_mode, prog_fmt);
396*0Sstevel@tonic-gate ret_val++;
397*0Sstevel@tonic-gate }
398*0Sstevel@tonic-gate
399*0Sstevel@tonic-gate if ((flags & ATTR_ACL) && (strcmp(ctrl_acl, test_acl) != 0)) {
400*0Sstevel@tonic-gate report_error(ctrl_fname, ACL_KEYWORD, ctrl_acl,
401*0Sstevel@tonic-gate test_acl, prog_fmt);
402*0Sstevel@tonic-gate ret_val++;
403*0Sstevel@tonic-gate }
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate if ((flags & ATTR_MTIME) && (ctrl_type[0] == test_type[0])) {
406*0Sstevel@tonic-gate if (strcmp(ctrl_mtime, test_mtime) != 0) {
407*0Sstevel@tonic-gate switch (ctrl_type[0]) {
408*0Sstevel@tonic-gate case 'D':
409*0Sstevel@tonic-gate tag = "dirmtime";
410*0Sstevel@tonic-gate break;
411*0Sstevel@tonic-gate case 'L':
412*0Sstevel@tonic-gate tag = "lnmtime";
413*0Sstevel@tonic-gate break;
414*0Sstevel@tonic-gate default:
415*0Sstevel@tonic-gate tag = "mtime";
416*0Sstevel@tonic-gate break;
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate if (flags == 0) {
419*0Sstevel@tonic-gate report_error(ctrl_fname, tag, ctrl_mtime,
420*0Sstevel@tonic-gate test_mtime, prog_fmt);
421*0Sstevel@tonic-gate ret_val++;
422*0Sstevel@tonic-gate }
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gate if ((ctrl_type[0] == 'F') && (flags & ATTR_MTIME) &&
426*0Sstevel@tonic-gate (strcmp(ctrl_mtime, test_mtime) != 0)) {
427*0Sstevel@tonic-gate report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime, test_mtime,
428*0Sstevel@tonic-gate prog_fmt);
429*0Sstevel@tonic-gate ret_val++;
430*0Sstevel@tonic-gate }
431*0Sstevel@tonic-gate
432*0Sstevel@tonic-gate if ((ctrl_type[0] == 'D') && (flags & ATTR_DIRMTIME) &&
433*0Sstevel@tonic-gate (strcmp(ctrl_mtime, test_mtime) != 0)) {
434*0Sstevel@tonic-gate report_error(ctrl_fname, DIRMTIME_KEYWORD, ctrl_mtime,
435*0Sstevel@tonic-gate test_mtime, prog_fmt);
436*0Sstevel@tonic-gate ret_val++;
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate if ((ctrl_type[0] == 'L') && (flags & ATTR_LNMTIME) &&
440*0Sstevel@tonic-gate (strcmp(ctrl_mtime, test_mtime) != 0)) {
441*0Sstevel@tonic-gate report_error(ctrl_fname, LNMTIME_KEYWORD, ctrl_mtime,
442*0Sstevel@tonic-gate test_mtime, prog_fmt);
443*0Sstevel@tonic-gate ret_val++;
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate } else if ((flags & ATTR_MTIME) &&
446*0Sstevel@tonic-gate (strcmp(ctrl_mtime, test_mtime) != 0)) {
447*0Sstevel@tonic-gate report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime,
448*0Sstevel@tonic-gate test_mtime, prog_fmt);
449*0Sstevel@tonic-gate ret_val++;
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate
452*0Sstevel@tonic-gate if ((flags & ATTR_UID) && (strcmp(ctrl_uid, test_uid) != 0)) {
453*0Sstevel@tonic-gate report_error(ctrl_fname, UID_KEYWORD, ctrl_uid,
454*0Sstevel@tonic-gate test_uid, prog_fmt);
455*0Sstevel@tonic-gate ret_val++;
456*0Sstevel@tonic-gate }
457*0Sstevel@tonic-gate
458*0Sstevel@tonic-gate if ((flags & ATTR_GID) && (strcmp(ctrl_gid, test_gid) != 0)) {
459*0Sstevel@tonic-gate report_error(ctrl_fname, GID_KEYWORD, ctrl_gid,
460*0Sstevel@tonic-gate test_gid, prog_fmt);
461*0Sstevel@tonic-gate ret_val++;
462*0Sstevel@tonic-gate }
463*0Sstevel@tonic-gate
464*0Sstevel@tonic-gate if ((flags & ATTR_DEVNODE) &&
465*0Sstevel@tonic-gate (strcmp(ctrl_devnode, test_devnode) != 0)) {
466*0Sstevel@tonic-gate report_error(ctrl_fname, DEVNODE_KEYWORD, ctrl_devnode,
467*0Sstevel@tonic-gate test_devnode, prog_fmt);
468*0Sstevel@tonic-gate ret_val++;
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate
471*0Sstevel@tonic-gate if ((flags & ATTR_DEST) && (strcmp(ctrl_dest, test_dest) != 0)) {
472*0Sstevel@tonic-gate report_error(ctrl_fname, DEST_KEYWORD, ctrl_dest,
473*0Sstevel@tonic-gate test_dest, prog_fmt);
474*0Sstevel@tonic-gate ret_val++;
475*0Sstevel@tonic-gate }
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate if ((flags & ATTR_CONTENTS) &&
478*0Sstevel@tonic-gate (strcmp(ctrl_contents, test_contents)) != 0) {
479*0Sstevel@tonic-gate report_error(ctrl_fname, CONTENTS_KEYWORD, ctrl_contents,
480*0Sstevel@tonic-gate test_contents, prog_fmt);
481*0Sstevel@tonic-gate ret_val++;
482*0Sstevel@tonic-gate }
483*0Sstevel@tonic-gate
484*0Sstevel@tonic-gate return (ret_val);
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate /*
488*0Sstevel@tonic-gate * Function responsible for reporting errors.
489*0Sstevel@tonic-gate */
490*0Sstevel@tonic-gate static void
report_error(char * fname,char * type,char * ctrl_val,char * test_val,boolean_t prog_fmt)491*0Sstevel@tonic-gate report_error(char *fname, char *type, char *ctrl_val, char *test_val,
492*0Sstevel@tonic-gate boolean_t prog_fmt)
493*0Sstevel@tonic-gate {
494*0Sstevel@tonic-gate static char last_fname[PATH_MAX] = "";
495*0Sstevel@tonic-gate
496*0Sstevel@tonic-gate if (!prog_fmt) {
497*0Sstevel@tonic-gate /* Verbose mode */
498*0Sstevel@tonic-gate if (strcmp(fname, last_fname) != 0) {
499*0Sstevel@tonic-gate (void) printf("%s:\n", fname);
500*0Sstevel@tonic-gate (void) strlcpy(last_fname, fname, sizeof (last_fname));
501*0Sstevel@tonic-gate }
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gate if (strcmp(type, ADD_KEYWORD) == 0 ||
504*0Sstevel@tonic-gate strcmp(type, DELETE_KEYWORD) == 0)
505*0Sstevel@tonic-gate (void) printf(" %s\n", type);
506*0Sstevel@tonic-gate else
507*0Sstevel@tonic-gate (void) printf(" %s control:%s test:%s\n", type,
508*0Sstevel@tonic-gate ctrl_val, test_val);
509*0Sstevel@tonic-gate } else {
510*0Sstevel@tonic-gate /* Programmatic mode */
511*0Sstevel@tonic-gate if (strcmp(fname, last_fname) != 0) {
512*0Sstevel@tonic-gate /* Ensure a line is not printed for the initial case */
513*0Sstevel@tonic-gate if (strlen(last_fname) != 0)
514*0Sstevel@tonic-gate (void) printf("\n");
515*0Sstevel@tonic-gate (void) strlcpy(last_fname, fname, sizeof (last_fname));
516*0Sstevel@tonic-gate (void) printf("%s ", fname);
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate (void) printf("%s ", type);
520*0Sstevel@tonic-gate if (strcmp(type, ADD_KEYWORD) != 0 &&
521*0Sstevel@tonic-gate strcmp(type, DELETE_KEYWORD) != 0) {
522*0Sstevel@tonic-gate (void) printf("%s ", ctrl_val);
523*0Sstevel@tonic-gate (void) printf("%s ", test_val);
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate }
527*0Sstevel@tonic-gate
528*0Sstevel@tonic-gate /*
529*0Sstevel@tonic-gate * Function responsible for reading in a line from the manifest.
530*0Sstevel@tonic-gate * Takes in the file ptr and a buffer, parses the buffer and sets the 'line'
531*0Sstevel@tonic-gate * ptr correctly. In the case when the buffer is fully parsed, this function
532*0Sstevel@tonic-gate * reads more data from the file ptr and refills the buffer.
533*0Sstevel@tonic-gate */
534*0Sstevel@tonic-gate static int
read_manifest_line(FILE * fd,char * buf,int buf_size,int start_pos,char ** line,char * fname)535*0Sstevel@tonic-gate read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
536*0Sstevel@tonic-gate char **line, char *fname)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate int end_pos, len, iscomment = 0, filepos;
539*0Sstevel@tonic-gate
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate * Initialization case: make sure the manifest version is OK
542*0Sstevel@tonic-gate */
543*0Sstevel@tonic-gate if (start_pos == 0) {
544*0Sstevel@tonic-gate end_pos = 0;
545*0Sstevel@tonic-gate buf[0] = '\0';
546*0Sstevel@tonic-gate filepos = ftell(fd);
547*0Sstevel@tonic-gate (void) fread((void *) buf, (size_t)buf_size, (size_t)1, fd);
548*0Sstevel@tonic-gate
549*0Sstevel@tonic-gate *line = buf;
550*0Sstevel@tonic-gate
551*0Sstevel@tonic-gate if (filepos == 0) {
552*0Sstevel@tonic-gate if (strncmp(buf, MANIFEST_VER,
553*0Sstevel@tonic-gate strlen(MANIFEST_VER)) != 0)
554*0Sstevel@tonic-gate (void) fprintf(stderr, MISSING_VER, fname);
555*0Sstevel@tonic-gate if ((*line[0] == '!') || (*line[0] == '#'))
556*0Sstevel@tonic-gate iscomment++;
557*0Sstevel@tonic-gate
558*0Sstevel@tonic-gate while (iscomment) {
559*0Sstevel@tonic-gate while ((buf[end_pos] != '\n') &&
560*0Sstevel@tonic-gate (buf[end_pos] != '\0') &&
561*0Sstevel@tonic-gate (end_pos < buf_size))
562*0Sstevel@tonic-gate end_pos++;
563*0Sstevel@tonic-gate
564*0Sstevel@tonic-gate if (end_pos >= buf_size)
565*0Sstevel@tonic-gate return (-1);
566*0Sstevel@tonic-gate
567*0Sstevel@tonic-gate end_pos++;
568*0Sstevel@tonic-gate *line = &(buf[end_pos]);
569*0Sstevel@tonic-gate iscomment = 0;
570*0Sstevel@tonic-gate if ((*line[0] == '!') || (*line[0] == '#'))
571*0Sstevel@tonic-gate iscomment++;
572*0Sstevel@tonic-gate }
573*0Sstevel@tonic-gate }
574*0Sstevel@tonic-gate
575*0Sstevel@tonic-gate while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
576*0Sstevel@tonic-gate (end_pos < buf_size))
577*0Sstevel@tonic-gate end_pos++;
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate if (end_pos < buf_size) {
580*0Sstevel@tonic-gate if (buf[end_pos] == '\n') {
581*0Sstevel@tonic-gate buf[end_pos] = '\0';
582*0Sstevel@tonic-gate return (end_pos);
583*0Sstevel@tonic-gate }
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate if (buf[end_pos] == '\0')
586*0Sstevel@tonic-gate return (-1);
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate (void) fprintf(stderr, MANIFEST_ERR);
590*0Sstevel@tonic-gate exit(FATAL_EXIT);
591*0Sstevel@tonic-gate }
592*0Sstevel@tonic-gate
593*0Sstevel@tonic-gate end_pos = (start_pos+1);
594*0Sstevel@tonic-gate *line = &(buf[end_pos]);
595*0Sstevel@tonic-gate
596*0Sstevel@tonic-gate /* Read the buffer until EOL or the buffer is empty */
597*0Sstevel@tonic-gate while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
598*0Sstevel@tonic-gate (end_pos < buf_size))
599*0Sstevel@tonic-gate end_pos++;
600*0Sstevel@tonic-gate
601*0Sstevel@tonic-gate if (end_pos < buf_size) {
602*0Sstevel@tonic-gate /* Found the end of the line, normal exit */
603*0Sstevel@tonic-gate if (buf[end_pos] == '\n') {
604*0Sstevel@tonic-gate buf[end_pos] = '\0';
605*0Sstevel@tonic-gate return (end_pos);
606*0Sstevel@tonic-gate }
607*0Sstevel@tonic-gate
608*0Sstevel@tonic-gate /* No more input to read */
609*0Sstevel@tonic-gate if (buf[end_pos] == '\0')
610*0Sstevel@tonic-gate return (-1);
611*0Sstevel@tonic-gate }
612*0Sstevel@tonic-gate
613*0Sstevel@tonic-gate /*
614*0Sstevel@tonic-gate * The following code takes the remainder of the buffer and
615*0Sstevel@tonic-gate * puts it at the beginning. The space after the remainder, which
616*0Sstevel@tonic-gate * is now at the beginning, is blanked.
617*0Sstevel@tonic-gate * At this point, read in more data and continue to find the EOL....
618*0Sstevel@tonic-gate */
619*0Sstevel@tonic-gate len = end_pos - (start_pos + 1);
620*0Sstevel@tonic-gate (void) memcpy(buf, &(buf[start_pos+1]), (size_t)len);
621*0Sstevel@tonic-gate (void) memset(&buf[len], '\0', (buf_size - len));
622*0Sstevel@tonic-gate (void) fread((void *) &buf[len], (size_t)(buf_size-len), (size_t)1, fd);
623*0Sstevel@tonic-gate *line = buf;
624*0Sstevel@tonic-gate end_pos = len;
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gate /* Read the buffer until EOL or the buffer is empty */
627*0Sstevel@tonic-gate while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
628*0Sstevel@tonic-gate (end_pos < buf_size))
629*0Sstevel@tonic-gate end_pos++;
630*0Sstevel@tonic-gate
631*0Sstevel@tonic-gate if (end_pos < buf_size) {
632*0Sstevel@tonic-gate /* Found the end of the line, normal exit */
633*0Sstevel@tonic-gate if (buf[end_pos] == '\n') {
634*0Sstevel@tonic-gate buf[end_pos] = '\0';
635*0Sstevel@tonic-gate return (end_pos);
636*0Sstevel@tonic-gate }
637*0Sstevel@tonic-gate
638*0Sstevel@tonic-gate /* No more input to read */
639*0Sstevel@tonic-gate if (buf[end_pos] == '\0')
640*0Sstevel@tonic-gate return (-1);
641*0Sstevel@tonic-gate }
642*0Sstevel@tonic-gate
643*0Sstevel@tonic-gate (void) fprintf(stderr, MANIFEST_ERR);
644*0Sstevel@tonic-gate exit(FATAL_EXIT);
645*0Sstevel@tonic-gate
646*0Sstevel@tonic-gate /* NOTREACHED */
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gate static void
init_default_flags(uint_t * flags)650*0Sstevel@tonic-gate init_default_flags(uint_t *flags)
651*0Sstevel@tonic-gate {
652*0Sstevel@tonic-gate /* Default behavior: everything is checked *except* dirmtime */
653*0Sstevel@tonic-gate *flags = ATTR_ALL & ~(ATTR_DIRMTIME);
654*0Sstevel@tonic-gate }
655