xref: /titanic_51/usr/src/tools/install.bin/install.bin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <sys/param.h>
34 #include <fcntl.h>
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <libgen.h>
42 
43 
44 #define	FILE_BUFF	40960
45 
46 int supress = 0;
47 
48 
49 void
50 usage(void)
51 {
52 	(void) fprintf(stderr,
53 	    "usage: install [-sd][-m mode][-g group][-u owner] "
54 	    "-f dir file ...\n");
55 }
56 
57 void
58 file_copy(char *src_file, char *dest_file)
59 {
60 	int	src_fd;
61 	int	dest_fd;
62 	int	count;
63 	static char file_buff[FILE_BUFF];
64 
65 	if ((src_fd = open(src_file, O_RDONLY))  == -1) {
66 		perror(src_file);
67 		exit(1);
68 	}
69 
70 	if ((dest_fd = open(dest_file, O_CREAT|O_WRONLY|O_TRUNC, 0755)) == -1) {
71 		perror(dest_file);
72 		exit(1);
73 	}
74 
75 	while ((count = read(src_fd, file_buff, FILE_BUFF)) > 0) {
76 		write(dest_fd, file_buff, count);
77 	}
78 
79 	if (count == -1) {
80 		perror("file_copy(read)");
81 		exit(1);
82 	}
83 
84 	if (!supress)
85 		(void) printf("%s installed as %s\n", src_file, dest_file);
86 
87 	close(src_fd);
88 	close(dest_fd);
89 }
90 
91 
92 void
93 chown_file(const char *file, const char *group, const char *owner)
94 {
95 	gid_t	grp;
96 	gid_t	own;
97 
98 	if (group) {
99 		if (strcmp(group, "bin") == 0)
100 			grp = 2;
101 		else if (strcmp(group, "sys") == 0)
102 			grp = 3;
103 		else if (strcmp(group, "adm") == 0)
104 			grp = 4;
105 		else if (strcmp(group, "uucp") == 0)
106 			grp = 5;
107 		else if (strcmp(group, "mail") == 0)
108 			grp = 6;
109 		else if (strcmp(group, "tty") == 0)
110 			grp = 7;
111 		else if (strcmp(group, "lp") == 0)
112 			grp = 8;
113 		else if (strcmp(group, "other") == 0)
114 			grp = 1;
115 		else if (strcmp(group, "nuucp") == 0)
116 			grp = 9;
117 		else if (strcmp(group, "staff") == 0)
118 			grp = 10;
119 		else if (strcmp(group, "daemon") == 0)
120 			grp = 12;
121 		else if (strcmp(group, "root") == 0)
122 			grp = 0;
123 		else if (strcmp(group, "sysadmin") == 0)
124 			grp = 14;
125 		else if (strcmp(group, "smmsp") == 0)
126 			grp = 25;
127 		else if (strcmp(group, "nobody") == 0)
128 			grp = 60001;
129 		else if (strcmp(group, "noaccess") == 0)
130 			grp = 60002;
131 		else if (strcmp(group, "svvs_grp") == 0)
132 			grp = 101;
133 		else
134 			(void) fprintf(stderr, "unknown group(%s)\n", group);
135 	} else {
136 		grp = -1;
137 	}
138 
139 	if (owner) {
140 		if (strcmp(owner, "bin") == 0)
141 			own = 2;
142 		else if (strcmp(owner, "daemon") == 0)
143 			own = 1;
144 		else if (strcmp(owner, "sys") == 0)
145 			own = 3;
146 		else if (strcmp(owner, "adm") == 0)
147 			own = 4;
148 		else if (strcmp(owner, "lp") == 0)
149 			own = 71;
150 		else if (strcmp(owner, "root") == 0)
151 			own = 0;
152 		else if (strcmp(owner, "uucp") == 0)
153 			own = 5;
154 		else if (strcmp(owner, "nuucp") == 0)
155 			own = 9;
156 		else if (strcmp(owner, "smmsp") == 0)
157 			own = 25;
158 		else if (strcmp(owner, "listen") == 0)
159 			own = 37;
160 		else if (strcmp(owner, "nobody") == 0)
161 			own = 60001;
162 		else if (strcmp(owner, "noaccess") == 0)
163 			own = 60002;
164 		/*
165 		 * This is a temporary HACK that should be removed!!!
166 		 */
167 		else if (strcmp(owner, "sysadm") == 0)
168 			own = 0;
169 		else {
170 			(void) fprintf(stderr, "unknown owner(%s)\n", owner);
171 			exit(1);
172 		}
173 
174 	} else {
175 		own = -1;
176 	}
177 
178 	if (chown(file, own, grp) == -1) {
179 		perror("chown");
180 		exit(1);
181 	}
182 }
183 
184 char *
185 find_basename(const char *str)
186 {
187 	int	i;
188 	int	len;
189 
190 	len = strlen(str);
191 
192 	for (i = len-1; i >= 0; i--)
193 		if (str[i] == '/')
194 			return ((char *)(str + i + 1));
195 	return ((char *)str);
196 }
197 
198 
199 int
200 main(int argc, char **argv)
201 {
202 	int	c;
203 	int	errflg = 0;
204 	int	dirflg = 0;
205 	char	*group = NULL;
206 	char	*owner = NULL;
207 	char	*dirb = NULL;
208 	char	*ins_file = NULL;
209 	int	mode = -1;
210 	char	dest_file[MAXPATHLEN];
211 
212 
213 	while ((c = getopt(argc, argv, "f:sm:du:g:")) != EOF) {
214 		switch (c) {
215 		case 'f':
216 			dirb = optarg;
217 			break;
218 		case 'g':
219 			group = optarg;
220 			break;
221 		case 'u':
222 			owner = optarg;
223 			break;
224 		case 'd':
225 			dirflg = 1;
226 			break;
227 		case 'm':
228 			mode = strtol(optarg, NULL, 8);
229 			break;
230 		case 's':
231 			supress = 1;
232 			break;
233 		case '?':
234 			errflg++;
235 			break;
236 		}
237 	}
238 
239 	if (errflg) {
240 		usage();
241 		return (1);
242 	}
243 
244 	if (argc == optind) {
245 		usage();
246 		return (1);
247 	}
248 
249 	if (!dirflg && (dirb == NULL)) {
250 		(void) fprintf(stderr,
251 		    "install: no destination directory specified.\n");
252 		return (1);
253 	}
254 
255 
256 	for (c = optind; c < argc; c++) {
257 		ins_file = argv[c];
258 
259 		if (dirflg) {
260 			struct stat buf;
261 
262 			if (stat(ins_file, &buf) == 0) {
263 				if ((buf.st_mode & S_IFMT) == S_IFDIR)
264 					continue;
265 			} else {
266 				if (errno != ENOENT) {
267 					perror("install: stat");
268 					return (1);
269 				}
270 			}
271 
272 			(void) strcpy(dest_file, ins_file);
273 
274 			if (mkdirp(dest_file, 0755) == -1) {
275 				if (!supress) {
276 					(void) printf(
277 					    "install: mkdirp of %s failed\n",
278 					    dest_file);
279 				}
280 			} else if (!supress) {
281 				(void) printf("directory %s created\n",
282 				    dest_file);
283 			}
284 		} else {
285 			(void) strcat(strcat(strcpy(dest_file, dirb), "/"),
286 			    find_basename(ins_file));
287 			file_copy(ins_file, dest_file);
288 		}
289 
290 		if (group || owner)
291 			chown_file(dest_file, group, owner);
292 
293 		if (mode != -1) {
294 			umask(0);
295 			if (chmod(dest_file, mode) == -1) {
296 				perror("chmod");
297 				return (1);
298 			}
299 		}
300 	}
301 	return (0);
302 }
303