logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

id.c (6050B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. #define _POSIX_C_SOURCE 200809L
  5. #include <grp.h> // getgrgid, getgroups
  6. #include <pwd.h> // getpwuid
  7. #include <stdbool.h> // bool
  8. #include <stdio.h> // printf, perror
  9. #include <stdlib.h> // malloc, free
  10. #include <sys/types.h> // uid_t
  11. #include <unistd.h> // getuid, getgid, getopt, opt*
  12. bool name_flag = false;
  13. static int
  14. simple_list_groups(struct passwd *pw)
  15. {
  16. int ngroups_max = (int)sysconf(_SC_NGROUPS_MAX) + 1;
  17. gid_t *groups = malloc(sizeof(gid_t) * ngroups_max);
  18. if(groups == NULL)
  19. {
  20. perror("groups malloc");
  21. return 1;
  22. }
  23. int ngroups = getgroups(ngroups_max, groups);
  24. if(ngroups < 0)
  25. {
  26. perror("getgroups");
  27. goto failure;
  28. }
  29. for(int i = 0; i < ngroups; i++)
  30. {
  31. if(name_flag)
  32. {
  33. struct group *lgr = getgrgid(groups[i]);
  34. if(lgr == NULL)
  35. {
  36. goto failure;
  37. }
  38. int sep = ' ';
  39. if(i == ngroups - 1)
  40. {
  41. sep = '\n';
  42. }
  43. int ret = printf("%s%c", lgr->gr_name, sep);
  44. if(ret < 0)
  45. {
  46. goto failure;
  47. }
  48. }
  49. else
  50. {
  51. int sep = ' ';
  52. if(i == ngroups - 1)
  53. {
  54. sep = '\n';
  55. }
  56. int ret = printf("%u%c", groups[i], sep);
  57. if(ret < 0)
  58. {
  59. goto failure;
  60. }
  61. }
  62. }
  63. free(groups);
  64. return 0;
  65. failure:
  66. free(groups);
  67. return 1;
  68. }
  69. static int
  70. list_groups(struct passwd *pw)
  71. {
  72. int ngroups_max = (int)sysconf(_SC_NGROUPS_MAX) + 1;
  73. gid_t *groups = malloc(sizeof(gid_t) * ngroups_max);
  74. if(groups == NULL)
  75. {
  76. perror("groups malloc");
  77. return 1;
  78. }
  79. int ngroups = getgroups(ngroups_max, groups);
  80. if(ngroups < 0)
  81. {
  82. perror("getgroups");
  83. goto failure;
  84. }
  85. else
  86. {
  87. printf(" groups=");
  88. for(int i = 0; i < ngroups; i++)
  89. {
  90. struct group *lgr = getgrgid(groups[i]);
  91. if(name_flag)
  92. {
  93. if(lgr == NULL)
  94. {
  95. goto failure;
  96. }
  97. int sep = ' ';
  98. if(i == ngroups - 1)
  99. {
  100. sep = '\0';
  101. }
  102. int ret = printf("%s%c", lgr->gr_name, sep);
  103. if(ret < 0)
  104. {
  105. goto failure;
  106. }
  107. }
  108. else
  109. {
  110. int ret = printf("%u", groups[i]);
  111. if(ret < 0)
  112. {
  113. goto failure;
  114. }
  115. if(lgr != NULL)
  116. {
  117. int ret = printf("(%s)", lgr->gr_name);
  118. if(ret < 0)
  119. {
  120. goto failure;
  121. }
  122. }
  123. if(i != ngroups - 1)
  124. {
  125. printf(",");
  126. }
  127. }
  128. }
  129. }
  130. free(groups);
  131. return 0;
  132. failure:
  133. free(groups);
  134. return 1;
  135. }
  136. int
  137. print_gid(char *field, struct group *gr, gid_t gid)
  138. {
  139. if(gr && gr->gr_name)
  140. {
  141. if(name_flag)
  142. {
  143. return printf("%s=%s", field, gr->gr_name);
  144. }
  145. return printf("%s=%u(%s)", field, gid, gr->gr_name);
  146. }
  147. else
  148. {
  149. if(name_flag)
  150. {
  151. return -1;
  152. }
  153. return printf("%s=%u", field, gid);
  154. }
  155. }
  156. int
  157. print_uid(char *field, struct passwd *pw, uid_t uid)
  158. {
  159. if(pw && pw->pw_name)
  160. {
  161. if(name_flag)
  162. {
  163. return printf("%s=%s", field, pw->pw_name);
  164. }
  165. return printf("%s=%u(%s)", field, uid, pw->pw_name);
  166. }
  167. else
  168. {
  169. if(name_flag)
  170. {
  171. return -1;
  172. }
  173. return printf("%s=%u", field, uid);
  174. }
  175. }
  176. void
  177. safe_getpwuid(uid_t uid, struct passwd *res)
  178. {
  179. struct passwd *pw = getpwuid(uid);
  180. if(pw != NULL)
  181. {
  182. *res = *pw;
  183. }
  184. }
  185. enum id_modes
  186. {
  187. ID_NORMAL,
  188. ID_GROUPS,
  189. ID_GID,
  190. ID_UID,
  191. };
  192. void
  193. usage()
  194. {
  195. fprintf(stderr, "Usage: id [-Ggu] [-nr] [user]\n");
  196. }
  197. int
  198. main(int argc, char *argv[])
  199. {
  200. int ret, c;
  201. enum id_modes mode = ID_NORMAL;
  202. bool real_flag = false;
  203. // geteuid, getuid, getegid, getgid shall always be successful
  204. uid_t uid = getuid();
  205. uid_t euid = geteuid();
  206. gid_t gid = getgid();
  207. gid_t egid = getegid();
  208. struct passwd pw = {.pw_uid = uid, .pw_gid = gid};
  209. struct passwd epw = {.pw_uid = euid, .pw_gid = egid};
  210. /* flawfinder: ignore. Old implementations of getopt should fix themselves */
  211. while((c = getopt(argc, argv, ":Ggunr")) != EOF)
  212. {
  213. switch(c)
  214. {
  215. case 'G':
  216. mode = ID_GROUPS;
  217. break;
  218. case 'u':
  219. mode = ID_UID;
  220. break;
  221. case 'g':
  222. mode = ID_GID;
  223. break;
  224. case 'n':
  225. name_flag = true;
  226. break;
  227. case 'r':
  228. real_flag = true;
  229. break;
  230. default:
  231. usage();
  232. return 1;
  233. }
  234. }
  235. argc -= optind;
  236. argv += optind;
  237. if(argc == 0)
  238. {
  239. safe_getpwuid(uid, &pw);
  240. safe_getpwuid(euid, &epw);
  241. }
  242. else if(argc == 1)
  243. {
  244. struct passwd *pw_n = getpwnam(argv[0]);
  245. if(pw_n == NULL)
  246. {
  247. return 1;
  248. }
  249. pw = *pw_n;
  250. epw = *pw_n;
  251. uid = pw.pw_uid;
  252. euid = epw.pw_uid;
  253. gid = pw.pw_gid;
  254. egid = epw.pw_gid;
  255. }
  256. else
  257. {
  258. usage();
  259. }
  260. struct group *gr = getgrgid(gid);
  261. struct group *egr = getgrgid(egid);
  262. if(mode == ID_GID)
  263. {
  264. if(!real_flag)
  265. {
  266. gid = egid;
  267. }
  268. if(!name_flag)
  269. {
  270. ret = printf("%u\n", gid);
  271. }
  272. else
  273. {
  274. if(gr == NULL || gr->gr_name == NULL)
  275. {
  276. ret--;
  277. fprintf(stderr, "id: cannot find name for group ID %u\n", gid);
  278. printf("%u\n", gid);
  279. }
  280. else
  281. {
  282. ret = printf("%s\n", gr->gr_name);
  283. }
  284. }
  285. if(ret < 0)
  286. {
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. if(mode == ID_UID)
  292. {
  293. if(!real_flag)
  294. {
  295. uid = euid;
  296. }
  297. if(!name_flag)
  298. {
  299. ret = printf("%u\n", uid);
  300. }
  301. else
  302. {
  303. if(pw.pw_name == NULL)
  304. {
  305. ret--;
  306. fprintf(stderr, "id: cannot find name for user ID %u\n", uid);
  307. printf("%u\n", uid);
  308. }
  309. else
  310. {
  311. ret = printf("%s\n", pw.pw_name);
  312. }
  313. }
  314. if(ret < 0)
  315. {
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. if(mode == ID_GROUPS)
  321. {
  322. if(real_flag)
  323. {
  324. ret = simple_list_groups(&pw);
  325. }
  326. else
  327. {
  328. ret = simple_list_groups(&epw);
  329. }
  330. if(ret != 0)
  331. {
  332. goto failure;
  333. }
  334. return 0;
  335. }
  336. ret = print_uid("uid", &pw, uid);
  337. if(ret < 0)
  338. {
  339. return 1;
  340. }
  341. if(euid != uid)
  342. {
  343. ret = print_uid(" euid", &epw, euid);
  344. if(ret < 0)
  345. {
  346. goto failure;
  347. }
  348. }
  349. ret = print_gid(" gid", gr, gid);
  350. if(ret < 0)
  351. {
  352. goto failure;
  353. }
  354. if(egid != gid)
  355. {
  356. ret = print_gid(" egid", egr, egid);
  357. if(ret < 0)
  358. {
  359. goto failure;
  360. }
  361. }
  362. if(list_groups(&pw) != 0)
  363. {
  364. goto failure;
  365. }
  366. ret = printf("\n");
  367. if(ret < 0)
  368. {
  369. return 1;
  370. }
  371. return 0;
  372. failure:
  373. printf("\n");
  374. return 1;
  375. }