17aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley/* id.c - print real and effective user and group IDs 295c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley * 395c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley * Copyright 2012 Sony Network Entertainment, Inc. 495c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley * 595c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley * by Tim Bird <tim.bird@am.sony.com> 695c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley * 7f91b7c89bc852868692b9518185421ebb52d67b3Rob Landley * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html 895c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley 90cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob LandleyUSE_ID(NEWTOY(id, ">1"USE_ID_Z("Z")"nGgru[!"USE_ID_Z("Z")"Ggu]", TOYFLAG_USR|TOYFLAG_BIN)) 10f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob LandleyUSE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN)) 111dd3704c5ffea926f61a96bb7de7d9dbee52fa44Paul BarkerUSE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_USR|TOYFLAG_BIN)) 121dd3704c5ffea926f61a96bb7de7d9dbee52fa44Paul BarkerUSE_WHOAMI(OLDTOY(whoami, logname, TOYFLAG_USR|TOYFLAG_BIN)) 1395c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley 1495c5099f29e946da0c211b9f0d50a29cb8245887Rob Landleyconfig ID 157aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley bool "id" 167aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley default y 177aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley help 187aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley usage: id [-nGgru] 197aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 207aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley Print user and group ID. 217aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 227aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley -n print names instead of numeric IDs (to be used with -Ggu) 237aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley -G Show only the group IDs 247aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley -g Show only the effective group ID 257aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley -r Show real ID instead of effective ID 267aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley -u Show only the effective user ID 2738aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten 280cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landleyconfig ID_Z 297e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes bool 307e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes default y 310cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley depends on ID && !TOYBOX_LSM_NONE 327e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes help 337e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes usage: id [-Z] 347e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes 350cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley -Z Show only security context 367e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes 3730de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landleyconfig GROUPS 3838aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten bool "groups" 3938aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten default y 4038aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten help 4138aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten usage: groups [user] 4238aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten 4338aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten Print the groups a user is in. 4438aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten 4530de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landleyconfig LOGNAME 4680a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham bool "logname" 4780a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham default y 4880a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham help 4980a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham usage: logname 5080a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham 5180a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham Print the current user name. 5280a438dd35782db8d3e0bca81ca78dea128f9c37Isaac Dunham 5330de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landleyconfig WHOAMI 5430de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley bool "whoami" 5530de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley default y 5630de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley help 5730de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley usage: whoami 5830de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley 5930de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley Print the current user name. 6095c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley*/ 6195c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley 62c0e56edaf256adb6c60c5a052525a1ffbb927901Rob Landley#define FOR_id 630cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley#define FORCE_FLAGS 6495c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley#include "toys.h" 6595c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley 6630de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob LandleyGLOBALS( 670cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley int is_groups; 6830de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley) 6930de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley 707fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landleystatic void s_or_u(char *s, unsigned u, int done) 718206db524ca681a973d30eda2098cb943ba2f056Daniel Walter{ 720cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (toys.optflags&FLAG_n) printf("%s", s); 737aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley else printf("%u", u); 747aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (done) { 757aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley xputc('\n'); 767aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley exit(0); 777aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 787fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landley} 797fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landley 807fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landleystatic void showid(char *header, unsigned u, char *s) 817fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landley{ 827aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley printf("%s%u(%s)", header, u, s); 837fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landley} 847fa9a05de2b79efe5e9c10986cbf1cb8f3abc068Rob Landley 857a3f53ba446ae2600763ee37b7f8dcc91de3ec5fRob Landleystatic void do_id(char *username) 8695c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley{ 8730de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley int flags, i, ngroups; 887aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley struct passwd *pw; 897aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley struct group *grp; 907aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley uid_t uid = getuid(), euid = geteuid(); 917aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley gid_t gid = getgid(), egid = getegid(), *groups; 927aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 9338aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten flags = toys.optflags; 9438aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten 957aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley // check if a username is given 961c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten if (username) { 975ec4ab3113dcc813b6040d7ded38e297df99dc0eRob Landley pw = xgetpwnam(username); 987aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley uid = euid = pw->pw_uid; 997aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley gid = egid = pw->pw_gid; 10030de5eff5976ac9e6dc5e4c4f05aeee5adce5650Rob Landley if (TT.is_groups) printf("%s : ", pw->pw_name); 1017aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1027aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 10338aa170897534e31afcb1e940b13d103e40cd1c4Ivo van poorten i = flags & FLAG_r; 1047aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley pw = xgetpwuid(i ? uid : euid); 1050cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (toys.optflags&FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1); 1067aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 1077aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley grp = xgetgrgid(i ? gid : egid); 1087aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1); 1097aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 1100cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (!(toys.optflags&(FLAG_g|FLAG_Z))) { 1117aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley showid("uid=", pw->pw_uid, pw->pw_name); 1127aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley showid(" gid=", grp->gr_gid, grp->gr_name); 1137aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 1147aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (!i) { 1157aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (uid != euid) { 1167aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley pw = xgetpwuid(euid); 1177aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley showid(" euid=", pw->pw_uid, pw->pw_name); 1187aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1197aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (gid != egid) { 1207aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley grp = xgetgrgid(egid); 1217aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley showid(" egid=", grp->gr_gid, grp->gr_name); 1227aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1237aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1247aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 1257aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley showid(" groups=", grp->gr_gid, grp->gr_name); 1267aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1277aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 1280cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (!(toys.optflags&FLAG_Z)) { 1297e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes groups = (gid_t *)toybuf; 1307e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes i = sizeof(toybuf)/sizeof(gid_t); 1317e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes ngroups = username ? getgrouplist(username, gid, groups, &i) 1327e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes : getgroups(i, groups); 1337e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes if (ngroups<0) perror_exit(0); 1347e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes 1350cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley int show_separator = !(toys.optflags&FLAG_G); 1367e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes for (i = 0; i<ngroups; i++) { 1370cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (show_separator) xputc((toys.optflags&FLAG_G) ? ' ' : ','); 1387e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes show_separator = 1; 1397e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes if (!(grp = getgrgid(groups[i]))) perror_msg(0); 1400cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley else if (toys.optflags&FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0); 1417e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name); 1427e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes else show_separator = 0; // Because we didn't show anything this time. 1437e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes } 1440cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (toys.optflags&FLAG_G) { 1457e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes xputc('\n'); 1467e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes exit(0); 1477e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes } 1487e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes } 1497e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes 1500cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (!CFG_TOYBOX_LSM_NONE) { 1510cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (lsm_enabled()) { 1520cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley char *context = lsm_context(); 1533b915991f8c8648f939498777f4d76aad04f2817Rob Landley 1540cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley printf(" context=%s"+!!(toys.optflags&FLAG_Z), context); 1550cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley if (CFG_TOYBOX_FREE) free(context); 1560cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley } else if (toys.optflags&FLAG_Z) error_exit("%s disabled", lsm_name()); 1577aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } 1587e2af1c7489cbf974a57d482bb9d23c884d2aa49Elliott Hughes 1597aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley xputc('\n'); 16095c5099f29e946da0c211b9f0d50a29cb8245887Rob Landley} 1611c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten 1621c1db048ad86067de70305a6e737802cf96ea302Ivo van Poortenvoid id_main(void) 1631c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten{ 1641c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++); 1651c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten else do_id(NULL); 1661c1db048ad86067de70305a6e737802cf96ea302Ivo van Poorten} 167f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley 168f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landleyvoid groups_main(void) 169f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley{ 170f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley TT.is_groups = 1; 1710cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley toys.optflags = FLAG_G|FLAG_n; 172f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley id_main(); 173f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley} 174f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley 175f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landleyvoid logname_main(void) 176f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley{ 1770cb5b70fa4ef093b08e59db813e73f3be6ecef4bRob Landley toys.optflags = FLAG_u|FLAG_n; 178f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley id_main(); 179f3e56f4e4ff773de95fa2c9daf979734d826fc33Rob Landley} 180