28 #include <strings.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include "stdusers.h"
39
40
41 #define FILE_BUFF 40960
42
43 static int suppress = 0;
44
45 static void usage(void);
46 static void file_copy(char *src_file, char *dest_file);
47 static void chown_file(const char *file, const char *group, const char *owner);
48 static void formclosed(char *root, char *closedroot);
49 static char *find_basename(const char *str);
50 static int creatdir(char *fn);
51
52
53 void
54 usage(void)
55 {
56 (void) fprintf(stderr,
57 "usage: install [-sdO][-m mode][-g group][-u owner] "
58 "-f dir file ...\n");
59 }
60
61 void
62 file_copy(char *src_file, char *dest_file)
63 {
64 int src_fd;
65 int dest_fd;
66 int count;
67 static char file_buff[FILE_BUFF];
68
69 if ((src_fd = open(src_file, O_RDONLY)) == -1) {
70 (void) fprintf(stderr, "install:file_copy: %s failed "
71 "(%d): %s\n", src_file, errno, strerror(errno));
72 exit(1);
73 }
74
75 if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
76 (void) fprintf(stderr, "install:file_copy: %s failed "
77 "(%d): %s\n", dest_file, errno, strerror(errno));
108 (void) fprintf(stderr, "unknown group(%s)\n", group);
109 }
110
111 if (owner) {
112 own = stdfind(owner, usernames);
113 if (own < 0) {
114 (void) fprintf(stderr, "unknown owner(%s)\n", owner);
115 exit(1);
116 }
117
118 }
119
120 if (chown(file, own, grp) == -1) {
121 (void) fprintf(stderr, "install:chown_file: failed "
122 "(%d): %s\n", errno, strerror(errno));
123 exit(1);
124 }
125 }
126
127
128 void
129 formclosed(char *root, char *closedroot)
130 {
131 int wholelen, residlen;
132 char *temp;
133
134 wholelen = strlen(root);
135 temp = strstr(strstr(root, "proto/root_"), "/");
136 temp++;
137 temp = strstr(temp, "/");
138 residlen = strlen(temp);
139 (void) strlcpy(closedroot, root, wholelen - residlen + 1);
140 (void) strlcat(closedroot, "-closed", MAXPATHLEN);
141 (void) strlcat(closedroot, temp, MAXPATHLEN);
142 }
143
144
145 char *
146 find_basename(const char *str)
147 {
148 int i;
149 int len;
150
151 len = strlen(str);
152
153 for (i = len-1; i >= 0; i--)
154 if (str[i] == '/')
155 return ((char *)(str + i + 1));
156 return ((char *)str);
157 }
158
159 int
160 creatdir(char *fn) {
161
162 errno = 0;
163
164 if (mkdirp(fn, 0755) == -1) {
166 return (errno);
167 } else if (!suppress) {
168 (void) printf("directory %s created\n", fn);
169 }
170 return (0);
171 }
172
173
174 int
175 main(int argc, char **argv)
176 {
177 int c;
178 int errflg = 0;
179 int dirflg = 0;
180 char *group = NULL;
181 char *owner = NULL;
182 char *dirb = NULL;
183 char *ins_file = NULL;
184 int mode = -1;
185 char dest_file[MAXPATHLEN];
186 char shadow_dest[MAXPATHLEN];
187 char shadow_dirb[MAXPATHLEN];
188 int tonic = 0;
189 int rv = 0;
190
191 while ((c = getopt(argc, argv, "f:sm:du:g:O")) != EOF) {
192 switch (c) {
193 case 'f':
194 dirb = optarg;
195 break;
196 case 'g':
197 group = optarg;
198 break;
199 case 'u':
200 owner = optarg;
201 break;
202 case 'd':
203 dirflg = 1;
204 break;
205 case 'm':
206 mode = strtol(optarg, NULL, 8);
207 break;
208 case 's':
209 suppress = 1;
210 break;
211 case 'O':
212 tonic = 1;
213 break;
214 case '?':
215 errflg++;
216 break;
217 }
218 }
219
220 if (errflg) {
221 usage();
222 return (1);
223 }
224
225 if (argc == optind) {
226 usage();
227 return (1);
228 }
229
230 if (!dirflg && (dirb == NULL)) {
231 (void) fprintf(stderr,
232 "install: no destination directory specified.\n");
233 return (1);
234 }
235
236 for (c = optind; c < argc; c++) {
237 ins_file = argv[c];
238
239 if (dirflg) {
240 if (tonic) {
241 formclosed(ins_file, shadow_dest);
242 rv = creatdir(shadow_dest);
243 if (rv) {
244 (void) fprintf(stderr,
245 "install: tonic creatdir "
246 "%s (%d): (%s)\n",
247 shadow_dest, errno,
248 strerror(errno));
249 return (rv);
250 }
251 }
252 rv = creatdir(ins_file);
253 if (rv) {
254 (void) fprintf(stderr,
255 "install: creatdir %s (%d): %s\n",
256 ins_file, errno, strerror(errno));
257 return (rv);
258 }
259 (void) strlcpy(dest_file, ins_file, MAXPATHLEN);
260
261 } else {
262 (void) strcat(strcat(strcpy(dest_file, dirb), "/"),
263 find_basename(ins_file));
264 file_copy(ins_file, dest_file);
265
266 if (tonic) {
267 formclosed(dirb, shadow_dirb);
268 /*
269 * The standard directories in the proto
270 * area are created as part of "make setup",
271 * but that doesn't create them in the
272 * closed proto area. So if the target
273 * directory doesn't exist, we need to
274 * create it now.
275 */
276 rv = creatdir(shadow_dirb);
277 if (rv) {
278 (void) fprintf(stderr,
279 "install: tonic creatdir(f) "
280 "%s (%d): %s\n",
281 shadow_dirb, errno,
282 strerror(errno));
283 return (rv);
284 }
285 (void) strcat(strcat(strcpy(shadow_dest,
286 shadow_dirb), "/"),
287 find_basename(ins_file));
288 file_copy(ins_file, shadow_dest);
289 }
290 }
291
292 if (group || owner) {
293 chown_file(dest_file, group, owner);
294 if (tonic)
295 chown_file(shadow_dest, group, owner);
296 }
297 if (mode != -1) {
298 (void) umask(0);
299 if (chmod(dest_file, mode) == -1) {
300 (void) fprintf(stderr,
301 "install: chmod of %s to mode %o failed "
302 "(%d): %s\n",
303 dest_file, mode, errno, strerror(errno));
304 return (1);
305 }
306 if (tonic) {
307 if (chmod(shadow_dest, mode) == -1) {
308 (void) fprintf(stderr,
309 "install: tonic chmod of %s "
310 "to mode %o failed (%d): %s\n",
311 shadow_dest, mode,
312 errno, strerror(errno));
313 return (1);
314 }
315 }
316 }
317 }
318 return (0);
319 }
|
28 #include <strings.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <unistd.h>
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include "stdusers.h"
39
40
41 #define FILE_BUFF 40960
42
43 static int suppress = 0;
44
45 static void usage(void);
46 static void file_copy(char *src_file, char *dest_file);
47 static void chown_file(const char *file, const char *group, const char *owner);
48 static char *find_basename(const char *str);
49 static int creatdir(char *fn);
50
51
52 void
53 usage(void)
54 {
55 (void) fprintf(stderr,
56 "usage: install [-sd][-m mode][-g group][-u owner] "
57 "-f dir file ...\n");
58 }
59
60 void
61 file_copy(char *src_file, char *dest_file)
62 {
63 int src_fd;
64 int dest_fd;
65 int count;
66 static char file_buff[FILE_BUFF];
67
68 if ((src_fd = open(src_file, O_RDONLY)) == -1) {
69 (void) fprintf(stderr, "install:file_copy: %s failed "
70 "(%d): %s\n", src_file, errno, strerror(errno));
71 exit(1);
72 }
73
74 if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
75 (void) fprintf(stderr, "install:file_copy: %s failed "
76 "(%d): %s\n", dest_file, errno, strerror(errno));
107 (void) fprintf(stderr, "unknown group(%s)\n", group);
108 }
109
110 if (owner) {
111 own = stdfind(owner, usernames);
112 if (own < 0) {
113 (void) fprintf(stderr, "unknown owner(%s)\n", owner);
114 exit(1);
115 }
116
117 }
118
119 if (chown(file, own, grp) == -1) {
120 (void) fprintf(stderr, "install:chown_file: failed "
121 "(%d): %s\n", errno, strerror(errno));
122 exit(1);
123 }
124 }
125
126
127 char *
128 find_basename(const char *str)
129 {
130 int i;
131 int len;
132
133 len = strlen(str);
134
135 for (i = len-1; i >= 0; i--)
136 if (str[i] == '/')
137 return ((char *)(str + i + 1));
138 return ((char *)str);
139 }
140
141 int
142 creatdir(char *fn) {
143
144 errno = 0;
145
146 if (mkdirp(fn, 0755) == -1) {
148 return (errno);
149 } else if (!suppress) {
150 (void) printf("directory %s created\n", fn);
151 }
152 return (0);
153 }
154
155
156 int
157 main(int argc, char **argv)
158 {
159 int c;
160 int errflg = 0;
161 int dirflg = 0;
162 char *group = NULL;
163 char *owner = NULL;
164 char *dirb = NULL;
165 char *ins_file = NULL;
166 int mode = -1;
167 char dest_file[MAXPATHLEN];
168 int rv = 0;
169
170 while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
171 switch (c) {
172 case 'f':
173 dirb = optarg;
174 break;
175 case 'g':
176 group = optarg;
177 break;
178 case 'u':
179 owner = optarg;
180 break;
181 case 'd':
182 dirflg = 1;
183 break;
184 case 'm':
185 mode = strtol(optarg, NULL, 8);
186 break;
187 case 's':
188 suppress = 1;
189 break;
190 case '?':
191 errflg++;
192 break;
193 }
194 }
195
196 if (errflg) {
197 usage();
198 return (1);
199 }
200
201 if (argc == optind) {
202 usage();
203 return (1);
204 }
205
206 if (!dirflg && (dirb == NULL)) {
207 (void) fprintf(stderr,
208 "install: no destination directory specified.\n");
209 return (1);
210 }
211
212 for (c = optind; c < argc; c++) {
213 ins_file = argv[c];
214
215 if (dirflg) {
216 rv = creatdir(ins_file);
217 if (rv) {
218 (void) fprintf(stderr,
219 "install: creatdir %s (%d): %s\n",
220 ins_file, errno, strerror(errno));
221 return (rv);
222 }
223 (void) strlcpy(dest_file, ins_file, MAXPATHLEN);
224
225 } else {
226 (void) strcat(strcat(strcpy(dest_file, dirb), "/"),
227 find_basename(ins_file));
228 file_copy(ins_file, dest_file);
229 }
230
231 if (group || owner)
232 chown_file(dest_file, group, owner);
233
234 if (mode != -1) {
235 (void) umask(0);
236 if (chmod(dest_file, mode) == -1) {
237 (void) fprintf(stderr,
238 "install: chmod of %s to mode %o failed "
239 "(%d): %s\n",
240 dest_file, mode, errno, strerror(errno));
241 return (1);
242 }
243 }
244 }
245 return (0);
246 }
|