8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / gss_mechs / mech_krb5 / krb5 / os / safechown.c
blob72ccf1ac7731230b2c0b1a4b81e66b2a83578dfb
1 /*
2 * Copyright (c) 1998, by Sun Microsystems, Inc.
3 * All rights reserved.
4 */
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
17 * safechown changes the owner ship of src to uid. If the mode parameter
18 * does not equal -1 changes the mode of src as well.
20 * return -1 on failure and 0 on success.
23 int
24 safechown(const char *src, uid_t uid, gid_t gid, int mode)
26 int fd;
27 struct stat fdbuf;
28 struct stat lbuf;
30 if ((fd = open(src, O_RDONLY, 0)) == -1)
31 return (-1);
33 if (fstat(fd, &fdbuf)) {
34 close(fd);
35 return (-1);
38 /* Make sure non directories are not hard links */
39 if (!S_ISDIR(fdbuf.st_mode) && fdbuf.st_nlink != 1) {
40 close(fd);
41 return (-1);
44 if (lstat(src, &lbuf)) {
45 close(fd);
46 return (-1);
49 /* Make sure file is not a symlink */
50 if (fdbuf.st_ino != lbuf.st_ino || fdbuf.st_dev != lbuf.st_dev ||
51 fdbuf.st_mode != lbuf.st_mode) {
53 close(fd);
54 return (-1);
57 /* we should probably get the primary group id for uid here */
58 if (fchown(fd, uid, gid)) {
59 close(fd);
60 return (-1);
63 if (mode != -1) {
64 if (fchmod(fd, (mode_t)mode)) {
65 close(fd);
66 return (-1);
70 close(fd);
72 return (0);
75 #ifdef TEST
76 void
77 usage(char *prg)
79 fprintf(stderr, "Usage %s [-u uid] [-m mode] source\n", prg);
80 exit(1);
83 main(int argc, char *argv[])
85 int opt;
86 int mode = -1;
87 uid_t uid = 0;
89 while ((opt = getopt(argc, argv, "m:u:")) != EOF) {
90 switch (opt) {
91 case 'm':
92 mode = strtol(optarg, 0, 8);
93 break;
94 case 'u':
95 uid = atoi(optarg);
96 break;
97 default:
98 usage(argv[0]);
102 if (argc - optind != 1)
103 usage(argv[0]);
105 if (safechown(argv[optind], uid, getgid(), mode)) {
106 perror("safechown");
107 exit(1);
110 return (0);
113 #endif /* TEST */