static int group(int argc, char *argv[]) { struct group *gr; unsigned long id; int i, rv; assert(argc > 1); assert(argv != NULL); #define GROUPPRINT printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \ gr->gr_name, gr->gr_passwd, gr->gr_gid) setgroupent(1); rv = RV_OK; if (argc == 2) { while ((gr = getgrent()) != NULL) GROUPPRINT; } else { for (i = 2; i < argc; i++) { if (parsenum(argv[i], &id)) gr = getgrgid((gid_t)id); else gr = getgrnam(argv[i]); if (gr != NULL) GROUPPRINT; else { rv = RV_NOTFOUND; break; } } } endgrent(); return rv; }
static int group(int argc, char *argv[]) { int i, rv = RV_OK; struct group *gr; setgroupent(1); if (argc == 2) { while ((gr = getgrent()) != NULL) GROUPPRINT; } else { for (i = 2; i < argc; i++) { const char *err; long long id = strtonum(argv[i], 0, UINT_MAX, &err); if (!err) gr = getgrgid((gid_t)id); else gr = getgrnam(argv[i]); if (gr != NULL) GROUPPRINT; else { rv = RV_NOTFOUND; break; } } } endgrent(); return rv; }
const char * group_from_gid(gid_t gid, int nogroup) { static struct ncache { gid_t gid; int found; char name[UT_NAMESIZE + 1]; } c_gid[NCACHE]; static int gropen; struct group *gr; struct ncache *cp; cp = c_gid + (gid & MASK); if (cp->gid != gid || !*cp->name) { if (gropen == 0) { setgroupent(1); gropen = 1; } gr = getgrgid(gid); cp->gid = gid; if (gr != NULL) { cp->found = 1; strncpy(cp->name, gr->gr_name, UT_NAMESIZE); cp->name[UT_NAMESIZE] = '\0'; } else { cp->found = 0; snprintf(cp->name, UT_NAMESIZE, "%u", gid); if (nogroup) return (NULL); } } return ((nogroup && !cp->found) ? NULL : cp->name); }
void setgrent(void) { int saved_errno; saved_errno = errno; setgroupent(0); errno = saved_errno; }
char * name_gid(gid_t gid, int frc) { struct group *gr; GIDC *ptr; if ((gidtb == NULL) && (gidtb_start() < 0)) return(""); /* * see if we have this gid cached */ ptr = gidtb[gid % GID_SZ]; if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { /* * have an entry for this gid */ if (frc || (ptr->valid == VALID)) return(ptr->name); return(""); } /* * No entry for this gid, we will add it */ if (!gropn) { setgroupent(1); ++gropn; } if (ptr == NULL) ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC)); if ((gr = getgrgid(gid)) == NULL) { /* * no match for this gid in the local group file, put in * a string that is the gid in numberic format */ if (ptr == NULL) return(""); ptr->gid = gid; ptr->valid = INVALID; (void)snprintf(ptr->name, sizeof(ptr->name), "%lu", (unsigned long)gid); if (frc == 0) return(""); } else { /* * there is an entry for this group in the group file */ if (ptr == NULL) return(gr->gr_name); ptr->gid = gid; (void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name)); ptr->valid = VALID; } return(ptr->name); }
int gid_name(char *name, gid_t *gid) { struct group *gr; GIDC *ptr; int namelen; /* * return -1 for mangled names */ if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) return(-1); if ((grptb == NULL) && (grptb_start() < 0)) return(-1); /* * look up in hash table, if found and valid return the uid, * if found and invalid, return a -1 */ ptr = grptb[st_hash(name, namelen, GID_SZ)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) return(-1); *gid = ptr->gid; return(0); } if (!gropn) { setgroupent(1); ++gropn; } if (ptr == NULL) ptr = grptb[st_hash(name, namelen, GID_SZ)] = (GIDC *)malloc(sizeof(GIDC)); /* * no match, look it up, if no match store it as an invalid entry, * or store the matching gid */ if (ptr == NULL) { if ((gr = getgrnam(name)) == NULL) return(-1); *gid = gr->gr_gid; return(0); } (void)strncpy(ptr->name, name, GNMLEN - 1); ptr->name[GNMLEN-1] = '\0'; if ((gr = getgrnam(name)) == NULL) { ptr->valid = INVALID; return(-1); } ptr->valid = VALID; *gid = ptr->gid = gr->gr_gid; return(0); }
static int group_fill_test_data(struct group_test_data *td) { struct group *grp; setgroupent(1); while ((grp = getgrent()) != NULL) { if (group_test_correctness(grp, NULL) == 0) TEST_DATA_APPEND(group, td, grp); else return (-1); } endgrent(); return (0); }
fsnode * read_mtree(const char *fname, fsnode *node) { struct mtree_fileinfo *fi; FILE *fp; int c, error; /* We do not yet support nesting... */ assert(node == NULL); if (strcmp(fname, "-") == 0) fp = stdin; else { fp = fopen(fname, "r"); if (fp == NULL) err(1, "Can't open `%s'", fname); } error = mtree_file_push(fname, fp); if (error) goto out; bzero(&mtree_global, sizeof(mtree_global)); bzero(&mtree_global_inode, sizeof(mtree_global_inode)); mtree_global.inode = &mtree_global_inode; mtree_global_inode.nlink = 1; mtree_global_inode.st.st_nlink = 1; mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime = mtree_global_inode.st.st_mtime = time(NULL); errors = warnings = 0; setgroupent(1); setpassent(1); mtree_root = node; mtree_current = node; do { /* Start of a new line... */ fi = SLIST_FIRST(&mtree_fileinfo); fi->line++; error = skip_over(fp, " \t"); if (error) break; c = getc(fp); if (c == EOF) { error = ferror(fp) ? errno : -1; break; } switch (c) { case '\n': /* empty line */ error = 0; break; case '#': /* comment -- skip to end of line. */ error = skip_to(fp, "\n"); if (!error) (void)getc(fp); break; case '/': /* special commands */ error = read_mtree_command(fp); break; default: /* specification */ ungetc(c, fp); error = read_mtree_spec(fp); break; } } while (!error); endpwent(); endgrent(); if (error <= 0 && (errors || warnings)) { warnx("%u error(s) and %u warning(s) in mtree manifest", errors, warnings); if (errors) exit(1); } out: if (error > 0) errc(1, error, "Error reading mtree file"); if (fp != stdin) fclose(fp); if (mtree_root != NULL) return (mtree_root); /* Handle empty specifications. */ node = create_node(".", S_IFDIR, NULL, &mtree_global); node->first = node; return (node); }
void setgrent(void) { (void) setgroupent(0); }
int main(int argc, char **argv) { int ch; int i; #ifdef HAVE_CAP_ENTER int retval; pid_t childpid, pid; #endif FILE *fp; while ((ch = getopt(argc, argv, "d:lnprsx")) != -1) { switch(ch) { case 'd': del = optarg; break; case 'l': oneline = 1; break; case 'n': oflags |= AU_OFLAG_NORESOLVE; break; case 'p': partial = 1; break; case 'r': if (oflags & AU_OFLAG_SHORT) usage(); /* Exclusive from shortfrm. */ oflags |= AU_OFLAG_RAW; break; case 's': if (oflags & AU_OFLAG_RAW) usage(); /* Exclusive from raw. */ oflags |= AU_OFLAG_SHORT; break; case 'x': oflags |= AU_OFLAG_XML; break; case '?': default: usage(); } } #ifdef HAVE_CAP_ENTER /* * Prime group, password, and audit-event files to be opened before we * enter capability mode. */ (void)getgrgid(0); (void)setgroupent(1); (void)getpwuid(0); (void)setpassent(1); (void)getauevent(); #endif if (oflags & AU_OFLAG_XML) au_print_xml_header(stdout); /* For each of the files passed as arguments dump the contents. */ if (optind == argc) { #ifdef HAVE_CAP_ENTER retval = cap_enter(); if (retval != 0 && errno != ENOSYS) err(EXIT_FAILURE, "cap_enter"); #endif print_tokens(stdin); return (1); } for (i = optind; i < argc; i++) { fp = fopen(argv[i], "r"); if (fp == NULL) { perror(argv[i]); continue; } /* * If operating with sandboxing, create a sandbox process for * each trail file we operate on. This avoids the need to do * fancy things with file descriptors, etc, when iterating on * a list of arguments. */ #ifdef HAVE_CAP_ENTER childpid = fork(); if (childpid == 0) { /* Child. */ retval = cap_enter(); if (retval != 0 && errno != ENOSYS) err(EXIT_FAILURE, "cap_enter"); if (print_tokens(fp) == -1) perror(argv[i]); exit(0); } /* Parent. Await child termination. */ while ((pid = waitpid(childpid, NULL, 0)) != childpid); #else if (print_tokens(fp) == -1) perror(argv[i]); #endif fclose(fp); } if (oflags & AU_OFLAG_XML) au_print_xml_footer(stdout); return (0); }
int main(int argc, char **argv) { const char *tmpdir; size_t tdlen; /* may not be a constant, thus initialising early */ listf = stderr; now = time(NULL); /* * Keep a reference to cwd, so we can always come back home. */ cwdfd = binopen2(BO_CLEXEC, ".", O_RDONLY); if (cwdfd < 0) { syswarn(1, errno, "Cannot open current working directory."); return(exit_val); } /* * Where should we put temporary files? */ if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') tmpdir = _PATH_TMP; tdlen = strlen(tmpdir); while (tdlen > 0 && tmpdir[tdlen - 1] == '/') tdlen--; tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE)); if (tempfile == NULL) { paxwarn(1, "%s for %s", "Out of memory", "temp file name"); return (exit_val); } if (tdlen) memcpy(tempfile, tmpdir, tdlen); tempbase = tempfile + tdlen; *tempbase++ = '/'; #if HAVE_SETPGENT /* * keep passwd and group files open for faster lookups. */ setpassent(1); setgroupent(1); #endif /* * parse options, determine operational mode, general init */ options(argc, argv); if ((gen_init() < 0) || (tty_init() < 0)) return(exit_val); #if HAVE_PLEDGE /* * pmode needs to restore setugid bits when extracting or copying, * so can't pledge at all then. */ if (pmode == 0 || (act != EXTRACT && act != COPY)) { if (pledge("stdio rpath wpath cpath fattr dpath getpw proc exec tape", NULL) == -1) err(1, "pledge"); /* Copy mode, or no gzip -- don't need to fork/exec. */ if (compress_program == NULL || act == COPY) { if (pledge("stdio rpath wpath cpath fattr dpath getpw tape", NULL) == -1) err(1, "pledge"); } } #endif /* make list fd independent and line-buffered */ if ((listfd = dup(fileno(listf))) < 0 || !(listf = fdopen(listfd, "wb"))) { syswarn(1, errno, "Cannot open list file descriptor"); return (exit_val); } if (fcntl(listfd, F_SETFD, FD_CLOEXEC) == -1) syswarn(0, errno, "%s on list file descriptor", "Failed to set the close-on-exec flag"); setlinebuf(listf); /* * select a primary operation mode */ switch (act) { case EXTRACT: extract(); break; case ARCHIVE: archive(); break; case APPND: if (compress_program != NULL) errx(1, "cannot compress while appending"); append(); break; case COPY: copy(); break; default: /* for ar_io.c etc. */ act = LIST; /* FALLTHROUGH */ case LIST: list(); break; } return(exit_val); }