logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

chown.c (6986B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. // NetBSD <10 hides fdopendir behind _NETBSD_SOURCE
  6. #if __NetBSD_Version__ < 1000000000
  7. #define _NETBSD_SOURCE
  8. #endif
  9. #include "../lib/fs.h"
  10. #include "../lib/user_group_parse.h"
  11. #include <assert.h>
  12. #include <dirent.h> // fdopendir, readdir, closedir
  13. #include <errno.h>
  14. #include <fcntl.h> // AT_FDCWD, fchownat
  15. #include <limits.h> // PATH_MAX
  16. #include <stdbool.h>
  17. #include <stdio.h> // fprintf
  18. #include <stdlib.h> // abort, exit
  19. #include <string.h> // strerror, strcmp
  20. #include <sys/stat.h> // fstatat, S_ISDIR
  21. #include <unistd.h> // getopt
  22. #ifdef HAS_GETOPT_LONG
  23. #include <getopt.h>
  24. #endif
  25. const char *argv0 = NULL;
  26. static uid_t user = (uid_t)-1;
  27. static uid_t group = (uid_t)-1;
  28. static bool opt_v = false, opt_R = false;
  29. enum chown_follow_symlinks
  30. {
  31. CHOWN_FOLLOW_UNK_SYMLINKS,
  32. CHOWN_FOLLOW_ALL_SYMLINKS,
  33. CHOWN_FOLLOW_NO_SYMLINK,
  34. CHOWN_FOLLOW_ONE_SYMLINK,
  35. };
  36. static int
  37. do_fchownat(int fd, char *name, char *acc_path, enum chown_follow_symlinks follow_symlinks)
  38. {
  39. struct stat stats;
  40. int err = 0;
  41. int stat_opts = (follow_symlinks == CHOWN_FOLLOW_NO_SYMLINK) ? AT_SYMLINK_NOFOLLOW : 0;
  42. int chown_opts = (follow_symlinks == CHOWN_FOLLOW_NO_SYMLINK) ? AT_SYMLINK_NOFOLLOW : 0;
  43. assert(errno == 0);
  44. if(fstatat(fd, name, &stats, stat_opts) != 0)
  45. {
  46. fprintf(stderr, "%s: Failed getting status for '%s': %s\n", argv0, acc_path, strerror(errno));
  47. errno = 0;
  48. return 1;
  49. }
  50. bool change = false;
  51. if(user != (uid_t)-1 && stats.st_uid != user) change = true;
  52. if(group != (uid_t)-1 && stats.st_gid != group) change = true;
  53. if(change)
  54. {
  55. assert(errno == 0);
  56. if(fchownat(fd, name, user, group, chown_opts) != 0)
  57. {
  58. fprintf(stderr,
  59. "%s: Failed setting ownership to '%d:%d' for '%s': %s\n",
  60. argv0,
  61. user,
  62. group,
  63. acc_path,
  64. strerror(errno));
  65. errno = 0;
  66. return 1;
  67. }
  68. if(opt_v)
  69. printf("%s: Ownership changed from %d:%d to %d:%d for '%s'\n",
  70. argv0,
  71. stats.st_uid,
  72. stats.st_gid,
  73. user,
  74. group,
  75. acc_path);
  76. }
  77. else
  78. {
  79. if(opt_v)
  80. printf("%s: Ownership already set to %d:%d for '%s'\n",
  81. argv0,
  82. stats.st_uid,
  83. stats.st_gid,
  84. acc_path);
  85. }
  86. if(opt_R && S_ISDIR(stats.st_mode))
  87. {
  88. assert(errno == 0);
  89. int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
  90. if(dir == -1)
  91. {
  92. fprintf(
  93. stderr, "%s: Couldn't open '%s' as directory: %s\n", argv0, acc_path, strerror(errno));
  94. errno = 0;
  95. return 1;
  96. }
  97. assert(errno == 0);
  98. DIR *dirp = fdopendir(dir);
  99. if(dirp == NULL)
  100. {
  101. fprintf(stderr,
  102. "%s: Couldn't get DIR entry for opened '%s': %s\n",
  103. argv0,
  104. acc_path,
  105. strerror(errno));
  106. errno = 0;
  107. return 1;
  108. }
  109. while(true)
  110. {
  111. assert(errno == 0);
  112. struct dirent *dp = readdir(dirp);
  113. if(dp == NULL)
  114. {
  115. if(errno == 0) break;
  116. fprintf(
  117. stderr, "%s: Failed reading directory '%s': %s\n", argv0, acc_path, strerror(errno));
  118. closedir(dirp); // FIXME: unhandled error
  119. errno = 0;
  120. return 1;
  121. }
  122. if(strcmp(dp->d_name, ".") == 0) continue;
  123. if(strcmp(dp->d_name, "..") == 0) continue;
  124. assert(errno == 0);
  125. char new_path[PATH_MAX] = "";
  126. if(snprintf(new_path, PATH_MAX, "%s/%s", acc_path, dp->d_name) < 0)
  127. {
  128. fprintf(stderr,
  129. "%s: Couldn't concatenate '%s' into parent '%s', skipping to next entry: %s",
  130. argv0,
  131. name,
  132. acc_path,
  133. strerror(errno));
  134. err++;
  135. errno = 0;
  136. continue;
  137. }
  138. enum chown_follow_symlinks child_follow_symlinks =
  139. (follow_symlinks == CHOWN_FOLLOW_ONE_SYMLINK) ? CHOWN_FOLLOW_NO_SYMLINK : follow_symlinks;
  140. // No depth counter for now, unlikely to be a problem as symlinks aren't followed
  141. int ret = do_fchownat(dir, dp->d_name, new_path, child_follow_symlinks);
  142. if(ret != 0) return ret;
  143. }
  144. // fdopendir allocates memory for DIR, needs closedir
  145. assert(errno == 0);
  146. if(closedir(dirp) != 0)
  147. {
  148. fprintf(stderr,
  149. "%s: Deallocating directory entry for '%s' failed: %s\n",
  150. argv0,
  151. acc_path,
  152. strerror(errno));
  153. errno = 0;
  154. return 1;
  155. }
  156. }
  157. return err;
  158. }
  159. static void
  160. usage(void)
  161. {
  162. if(strcmp(argv0, "chown") == 0)
  163. fprintf(stderr, "Usage: %s [-h | -R [-H|-L|-P]] owner[:group] file...\n", argv0);
  164. else if(strcmp(argv0, "chgrp") == 0)
  165. fprintf(stderr, "Usage: %s [-h | -R [-H|-L|-P]] group file...\n", argv0);
  166. else
  167. {
  168. fprintf(stderr, "%s: Unknown utility '%s' expected either chown or chgrp\n", argv0, argv0);
  169. exit(1);
  170. }
  171. }
  172. int
  173. main(int argc, char *argv[])
  174. {
  175. argv0 = static_basename(argv[0]);
  176. enum chown_follow_symlinks follow_symlinks = CHOWN_FOLLOW_UNK_SYMLINKS;
  177. int c = -1;
  178. #ifdef HAS_GETOPT_LONG
  179. // Strictly for GNUisms compatibility so no long-only options
  180. // clang-format off
  181. static struct option opts[] = {
  182. {"no-dereference", no_argument, 0, 'h'},
  183. {"recursive", no_argument, 0, 'R'},
  184. {"verbose", no_argument, 0, 'v'},
  185. {0, 0, 0, 0},
  186. };
  187. // clang-format on
  188. // Need + as first character to get POSIX-style option parsing
  189. while((c = getopt_long(argc, argv, "+:hRHLPv", opts, NULL)) != -1)
  190. #else
  191. while((c = getopt(argc, argv, ":hRHLPv")) != -1)
  192. #endif
  193. {
  194. switch(c)
  195. {
  196. case 'h':
  197. follow_symlinks = CHOWN_FOLLOW_NO_SYMLINK;
  198. break;
  199. case 'R':
  200. opt_R = true;
  201. break;
  202. case 'H':
  203. follow_symlinks = CHOWN_FOLLOW_ONE_SYMLINK;
  204. break;
  205. case 'L':
  206. follow_symlinks = CHOWN_FOLLOW_ALL_SYMLINKS;
  207. break;
  208. case 'P':
  209. follow_symlinks = CHOWN_FOLLOW_NO_SYMLINK;
  210. break;
  211. case 'v':
  212. opt_v = true;
  213. break;
  214. case ':':
  215. fprintf(stderr, "%s: Error: Missing operand for option: '-%c'\n", argv0, optopt);
  216. usage();
  217. return 1;
  218. case '?':
  219. fprintf(stderr, "%s: Error: Unrecognised option: '-%c'\n", argv0, optopt);
  220. usage();
  221. return 1;
  222. default:
  223. abort();
  224. }
  225. }
  226. argc -= optind;
  227. argv += optind;
  228. if(follow_symlinks == CHOWN_FOLLOW_UNK_SYMLINKS)
  229. follow_symlinks = opt_R ? CHOWN_FOLLOW_NO_SYMLINK : CHOWN_FOLLOW_ALL_SYMLINKS;
  230. if(argc < 2)
  231. {
  232. fprintf(stderr, "%s: Expects >=2 arguments, %d given\n", argv0, argc);
  233. usage();
  234. return 1;
  235. }
  236. if(strcmp(argv0, "chown") == 0)
  237. {
  238. char *gname = strchr(argv[0], ':');
  239. if(gname != NULL)
  240. {
  241. gname[0] = 0;
  242. gname++;
  243. if(parse_group(gname, &group) < 0) return 1;
  244. }
  245. if(parse_user(argv[0], &user) < 0) return 1;
  246. }
  247. else if(strcmp(argv0, "chgrp") == 0)
  248. {
  249. if(parse_group(argv[0], &group) < 0) return 1;
  250. }
  251. else
  252. {
  253. fprintf(stderr, "%s: Unknown utility '%s' expected either chown or chgrp\n", argv0, argv0);
  254. return 1;
  255. }
  256. for(int i = 1; i < argc; i++)
  257. {
  258. int ret = do_fchownat(AT_FDCWD, argv[i], argv[i], follow_symlinks);
  259. if(ret != 0) return ret;
  260. }
  261. return 0;
  262. }