int main(int argc,char *argv[]) { uid_t u; gid_t g; char *uname; char *gname; struct passwd *pwd; u = userIdFromName("root"); printf("uid=%ld,username=%s\n",u,"root"); uname = userNameFromId(u); printf("username=%s,uid=%ld\n",uname,u); pwd = getpwnam("root"); if (pwd == NULL) { errExit("getpwnam"); } g = pwd->pw_gid; gname = groupNameFromId(pwd->pw_gid); printf("groupname=%s,gid=%ld\n",gname,g); g = groupIdFromName(gname); printf("gid=%ld,groupname=%s\n",g,gname); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { uid_t uid; gid_t gid; int j; Boolean errFnd; if (argc < 3 || strcmp(argv[1], "--help") == 0) usageErr("%s owner group [file...]\n" " owner or group can be '-', " "meaning leave unchanged\n", argv[0]); if (strcmp(argv[1], "-") == 0) { /* "-" ==> don't change owner */ uid = -1; } else { /* Turn user name into UID */ uid = userIdFromName(argv[1]); if (uid == -1) fatal("No such user (%s)", argv[1]); } if (strcmp(argv[2], "-") == 0) { /* "-" ==> don't change group */ gid = -1; } else { /* Turn group name into GID */ gid = groupIdFromName(argv[2]); if (gid == -1) fatal("No group user (%s)", argv[1]); } /* Change ownership of all files named in remaining arguments */ errFnd = FALSE; for (j = 3; j < argc; j++) { if (chown(argv[j], uid, gid) == -1) { errMsg("chown: %s", argv[j]); errFnd = TRUE; } } exit(errFnd ? EXIT_FAILURE : EXIT_SUCCESS); }
static Boolean parseEntrySpec(char *entryStr, struct AccessControlEntry *ace, Boolean permsReqd) { char *colon1, *colon2; Boolean hasQual; /* Is optional qualifier present? */ Boolean hasPerms; /* Are permissions specified? */ colon1 = strchr(entryStr, ':'); if (colon1 == NULL) { fprintf(stderr, "Missing initial colon in ACL entry: %s\n", entryStr); return FALSE; } hasQual = *(colon1 + 1) != '\0' && *(colon1 + 1) != ':'; *colon1 = '\0'; /* Add terminator to tag type */ colon2 = strchr(colon1 + 1, ':'); hasPerms = colon2 != NULL && *(colon2 + 1) != '\0'; if (hasPerms && !permsReqd) { fprintf(stderr, "Cannot specify permissions here\n"); return FALSE; } if (!hasPerms && permsReqd) { fprintf(stderr, "Must specify permissions\n"); return FALSE; } /* Determine tag type, depending on tag string and presence of qualifier after the first ':' */ if (strcmp(entryStr, "u") == 0 || strcmp(entryStr, "user") == 0) ace->tag = hasQual ? ACL_USER : ACL_USER_OBJ; else if (strcmp(entryStr, "g") == 0 || strcmp(entryStr, "group") == 0) ace->tag = hasQual ? ACL_GROUP : ACL_GROUP_OBJ; else if (strcmp(entryStr, "o") == 0 || strcmp(entryStr, "other") == 0) ace->tag = ACL_OTHER; else if (strcmp(entryStr, "m") == 0 || strcmp(entryStr, "mask") == 0) ace->tag = ACL_MASK; else { fprintf(stderr, "Bad tag: %s\n", entryStr); return FALSE; } /* For ACL_USER and ACL_GROUP tags, extract a UID / GID from qualifier */ if (colon2 != NULL) *colon2 = '\0'; /* Add terminator to qualifier */ ace->qual = 0; if (ace->tag == ACL_USER) { ace->qual = userIdFromName(colon1 + 1); if (ace->qual == -1) { fprintf(stderr, "Bad user ID: %s\n", colon1 + 1); return FALSE; } } else if (ace->tag == ACL_GROUP) { ace->qual = groupIdFromName(colon1 + 1); if (ace->qual == -1) { fprintf(stderr, "Bad group ID: %s\n", colon1 + 1); return FALSE; } } /* If a permissions string was present, return it as a bit mask */ if (hasPerms) { char *p; ace->perms = 0; /* We're not too thorough here -- we don't check for multiple instances of [-rwx], or check if the permissions string is longer than three characters... */ for (p = colon2 + 1; *p != '\0'; p++) { if (*p == 'r') ace->perms |= ACL_READ; else if (*p == 'w') ace->perms |= ACL_WRITE; else if (*p == 'x') ace->perms |= ACL_EXECUTE; else if (*p != '-') { fprintf(stderr, "Bad character in permissions " "string: %c\n", *p); return FALSE; } } } return TRUE; }
int main (int argc, char *argv[]) { if (argc != 4) { usage(argv[0]); } char mode; if (strcmp(argv[1], "u") == 0) { mode = 'u'; } else if (strcmp(argv[1], "g") == 0) { mode = 'g'; } else { usage(argv[0]); } uid_t uid; gid_t gid; if (mode == 'u') { uid = userIdFromName(argv[2]); if (uid == -1) { errExit("couldn't find user %s\n", argv[2]); } } else { // mode == 'g' gid = groupIdFromName(argv[2]); if (gid == -1) { errExit("couldn't find group %s\n", argv[2]); } } char *filepath = argv[3]; struct stat stats; if (stat(filepath, &stats) == -1) { errExit("stat\n"); } acl_t acl = acl_get_file(filepath, ACL_TYPE_ACCESS); if (acl == NULL) { errExit("acl_get_file\n"); } acl_entry_t entry; acl_tag_t tag; int entryId; int mask_found = 0; acl_entry_t mask; for (entryId = ACL_FIRST_ENTRY; ; entryId = ACL_NEXT_ENTRY) { if (acl_get_entry(acl, entryId, &entry) == 1) { break; } if ((tag = acl_get_tag_type(entry, &tag)) == -1) { errExit("acl_get_tag_type\n"); } if (tag == ACL_MASK) { mask_found = 1; mask = entry; break; } } acl_entry_t needle; uid_t *uid_p; gid_t *gid_p; for (entryId = ACL_FIRST_ENTRY; ; entryId = ACL_NEXT_ENTRY) { if (acl_get_entry(acl, entryId, &entry) != 1) { errExit( "couldn't find an entry for the specified %s\n", mode == 'u' ? "user" : "group" ); } if (acl_get_tag_type(entry, &tag) == -1) { errExit("acl_get_tag_type\n"); } if (mode == 'u') { if (uid == stats.st_uid && tag == ACL_USER_OBJ) { needle = entry; break; } if (tag != ACL_USER) { continue; } uid_p = acl_get_qualifier(entry); if (uid_p == NULL) { errExit("acl_get_qualifier\n"); } if (*uid_p == uid) { needle = entry; break; } } if (mode == 'g') { if (gid == stats.st_gid && tag == ACL_GROUP_OBJ) { needle = entry; break; } if (tag != ACL_GROUP) { continue; } gid_p = acl_get_qualifier(entry); if (gid_p == NULL) { errExit("acl_get_qualifier\n"); } if (*gid_p == gid) { needle = entry; break; } } } acl_permset_t needle_perms; if (acl_get_permset(needle, &needle_perms) == -1) { errExit("acl_get_permset\n"); } printPerms(needle_perms); printf("\n"); if (mask_found && !(mode == 'u' && uid == stats.st_uid && tag == ACL_USER_OBJ)) { acl_permset_t mask_perms; if (acl_get_permset(mask, &mask_perms) == -1) { errExit("acl_get_permset\n"); } printf("Effective permissions: "); if (maskPermset(needle_perms, mask_perms) == -1) { errExit("maskPermset\n"); } printPerms(needle_perms); printf("\n"); } exit(EXIT_SUCCESS); }