/* Translates the username or groupname defined by Name to Id. Returns 0 on success, -1 if translation fails. */ int AL_NameToId(IN const char *Name, OUT int *Id) { LogMsg(1, AL_DebugLevel, stdout, "in AL_NameToId(%s)", Name); if(Name == NULL) return -1; PDB_lookupByName(Name, (int32_t *) Id); return (*Id == 0)?-1:0; }
void tool_newDefGroup(int argc, char **argv) { int32_t ownerid, id; char *colon = NULL; if (check_args_num(argc, 2)) { printf( "Usage: ng name\nname\t\t" "owner:name - name of new group\n"); return; } colon = strchr(argv[1], ':'); if (!colon) { printf("Name must be of the form: owner:group\n"); return; } *colon = '\0'; PDB_lookupByName(argv[1], &ownerid); *colon = ':'; if (!ownerid) { printf("Owner must exist!\n"); return; } PDB_createGroup(argv[1], ownerid, &id); printf("Created %s with id %d\n", argv[1], id); }
/* LOOK UP A USER OR GROUP NAME */ void tool_lookup(int argc, char *argv[]) { int32_t id; if (check_args_num(argc, 2)) { printf("Usage: l id\nid\t\tid number of user/group\n"); return; } PDB_lookupByName(argv[1], &id); printf("%s's id is %d\n", argv[1], id); }
/* Convert a name or id argument into a numerical id value */ int32_t get_id(char *n) { int32_t id; /* try to interpret the passed argument as a numeric id */ id = atoi(n); if (id != 0) return id; /* Attempt to find the argument as a name */ PDB_lookupByName(n, &id); return id; }
void PDB_bugfixes(void) { PDB_HANDLE h; /* fixups for old bugs */ int32_t id; pdb_array_off off; int rc, n; PDB_profile p, r; h = PDB_db_open(O_RDWR); while ( (rc = PDB_db_nextkey(h, &id)) ) { if ( rc == -1 ) continue; PDB_readProfile(h, id, &p); CODA_ASSERT(p.id != 0); if (PDB_ISGROUP(p.id)) { /* BUG: forgot to update owner_id when changing a user's uid */ PDB_lookupByName(p.owner_name, &id); if (p.owner_id != id) { fprintf(stderr, "Group %d owner name %s didn't match owner id %d, FIXED\n", p.id, p.owner_name, p.owner_id); p.owner_id = id; PDB_writeProfile(h, &p); } /* BUG: we added userid's to a group's member_of list */ again: id = pdb_array_head(&p.member_of, &off); while(id != 0) { if (PDB_ISUSER(id)) { pdb_array_del(&p.member_of, id); PDB_updateCps(h, &p); fprintf(stderr, "Group %d was listed as a member of userid %d, FIXED\n", p.id, id); goto again; } id = pdb_array_next(&p.member_of, &off); } /* BUG: we forgot to change userid's in a group's groups_or_members * list (fix part 1, removes non-existing or non-member * userids) */ again2: id = pdb_array_head(&p.groups_or_members, &off); while(id != 0) { if (PDB_ISUSER(id)) { PDB_readProfile(h, id, &r); CODA_ASSERT(r.id != 0); n = pdb_array_search(&r.member_of, p.id); if (n == -1) { pdb_array_del(&p.groups_or_members, id); PDB_updateCps(h, &p); PDB_freeProfile(&r); fprintf(stderr, "Group %d had nonexisting member %d, FIXED\n", p.id, id); goto again2; } PDB_freeProfile(&r); } id = pdb_array_next(&p.groups_or_members, &off); } } else /* PDB_ISUSER(p.id) */ { /* BUG: we forgot to change userid's in a group's groups_or_members * list (fix part 2, adds missing members to groups)*/ id = pdb_array_head(&p.member_of, &off); while (id != 0) { if (PDB_ISGROUP(id)) { PDB_readProfile(h, id, &r); CODA_ASSERT(r.id != 0); n = pdb_array_search(&r.groups_or_members, p.id); if (n == -1) { pdb_array_add(&r.groups_or_members, p.id); PDB_updateCps(h, &r); fprintf(stderr, "Group %d was missing member %d, FIXED\n", id, p.id); } PDB_freeProfile(&r); } id = pdb_array_next(&p.member_of, &off); } } PDB_freeProfile(&p); } PDB_db_close(h); /* iterate through the whole database again and this time make sure that * all the CPS arrays are consistent. */ h = PDB_db_open(O_RDWR); while ( (rc = PDB_db_nextkey(h, &id)) ) { if ( rc == -1 ) continue; PDB_readProfile(h, id, &p); if (p.id == 0) continue; PDB_updateCps(h, &p); PDB_freeProfile(&p); } PDB_db_close(h); }
void tool_import(int argc, char *argv[]) { FILE *userfile, *groupfile; char user[64], group[64], owner[64], member[64], *s; int32_t user_id, group_id, owner_id, member_id, create_id; int rc; if (check_args_num(argc, 3)) { fprintf(stderr, "Usage: import <userfile> <groupfile>\n"); return; } userfile = fopen(argv[1], "r"); if (!userfile) { fprintf(stderr, "Can't open userfile '%s'\n", argv[1]); return; } groupfile = fopen(argv[2], "r"); if (!groupfile) { fprintf(stderr, "Can't open groupfile '%s'\n", argv[2]); return; } /* recreate all users */ while (1) { rc = fscanf(userfile, "%[^:]:%*[^:]:%d:%*[^\n]\n", user, &user_id); if (rc != 2) break; if (user_id == 0) { printf("Userid 0 must be avoided, skipping entry for %s\n", user); continue; } if (user_id < 0) { printf("Skipping user %s with a negative id %d\n", user, user_id); continue; } /* create user */ PDB_lookupById(user_id, &s); if (s) { printf("Duplicate user for id %d, found both %s and %s\n", user_id, s, user); free(s); continue; } PDB_createUser(user, &create_id); PDB_changeId(create_id, user_id); printf("Created user %s, id %d\n", user, user_id); } fclose(userfile); /* recreate groups */ while (1) { int c; rc = fscanf(groupfile, "%[^:]:%*[^:]:%d:%[^,\n]", group, &group_id, owner); if (rc != 3) break; /* skip to the next newline */ do { c = fgetc(groupfile); } while (c != EOF && c != (int)'\n'); if (group_id == 0) { printf("Groupid 0 must be avoided, skipping entry for %s\n", group); continue; } /* restore the :'s in the group name */ s = group; while ((s = strchr(s, '%')) != NULL) *s = ':'; /* negate positive group ids, Coda groups are negative numbers */ if (group_id > 0) { group_id = -group_id; /* assuming this is the /etc/group file, force owner to System */ strcpy(owner, "System"); } /* create group */ PDB_lookupByName(owner, &owner_id); if (owner_id == 0) { printf("Group %s's owner %s cannot be found\n", group, owner); continue; } if (!PDB_ISUSER(owner_id)) { printf("Group %s's owner %s is a group but should be a user\n", group, owner); continue; } PDB_lookupById(group_id, &s); if (s) { printf("Duplicate group for id %d, found both %s and %s\n", group_id, s, group); free(s); continue; } PDB_createGroup(group, owner_id, &create_id); PDB_changeId(create_id, group_id); printf("Created group %s, id %d, owner %s\n", group, group_id, owner); } /* Add group members. Groups can be members of another group, so that is * why we needed to create all the groups first */ rewind(groupfile); while (1) { rc = fscanf(groupfile, "%[^:]:%*[^:]:%d:", group, &group_id); if (rc != 2) break; if (group_id == 0) continue; /* restore the :'s in the group name */ s = group; while ((s = strchr(s, '%')) != NULL) *s = ':'; if (group_id > 0) { group_id = -group_id; } else { /* skip the owner when the group_id is negative */ fscanf(groupfile, "%[^,\n]%*[,]", owner); } /* add group members */ printf("Adding members to %s\n\t", group); while (fscanf(groupfile, "%[^,\n]%*[,]", member) == 1) { /* restore the :'s in the name */ s = member; while ((s = strchr(s, '%')) != NULL) *s = ':'; PDB_lookupByName(member, &member_id); if (member_id == 0) { printf("\nGroup %s's member %s cannot be found\n\t", group, member); continue; } PDB_addToGroup(member_id, group_id); printf(" %s", member); } fgetc(groupfile); /* eat the '\n' */ printf("\n"); } fclose(groupfile); }