=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/getent/getent.c,v retrieving revision 1.1 retrieving revision 1.2 diff -c -r1.1 -r1.2 *** src/usr.bin/getent/getent.c 2005/11/10 19:24:36 1.1 --- src/usr.bin/getent/getent.c 2005/11/10 20:07:14 1.2 *************** *** 1,4 **** ! /* $openBSD$ */ /* $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $ */ /*- --- 1,4 ---- ! /* $OpenBSD: getent.c,v 1.2 2005/11/10 20:07:14 deraadt Exp $ */ /* $NetBSD: getent.c,v 1.7 2005/08/24 14:31:02 ginsbach Exp $ */ /*- *************** *** 40,46 **** #include #include - #include #include #include #include --- 40,45 ---- *************** *** 60,70 **** #include /* for INET6_ADDRSTRLEN */ #include - #include static int usage(void); - static int parsenum(const char *, unsigned long *); static int ethers(int, char *[]); static int group(int, char *[]); static int hosts(int, char *[]); --- 59,67 ---- *************** *** 74,90 **** static int rpc(int, char *[]); static int services(int, char *[]); static int shells(int, char *[]); enum { RV_OK = 0, RV_USAGE = 1, RV_NOTFOUND = 2, ! RV_NOENUM = 3, }; static struct getentdb { const char *name; ! int (*callback)(int, char *[]); } databases[] = { { "ethers", ethers, }, { "group", group, }, --- 71,88 ---- static int rpc(int, char *[]); static int services(int, char *[]); static int shells(int, char *[]); + extern char *__progname; enum { RV_OK = 0, RV_USAGE = 1, RV_NOTFOUND = 2, ! RV_NOENUM = 3 }; static struct getentdb { const char *name; ! int (*fn)(int, char *[]); } databases[] = { { "ethers", ethers, }, { "group", group, }, *************** *** 99,105 **** { NULL, NULL, }, }; - int main(int argc, char *argv[]) { --- 97,102 ---- *************** *** 109,162 **** usage(); for (curdb = databases; curdb->name != NULL; curdb++) { if (strcmp(curdb->name, argv[1]) == 0) { ! exit(curdb->callback(argc, argv)); break; } } ! fprintf(stderr, "Unknown database: %s\n", argv[1]); ! usage(); ! /* NOTREACHED */ return RV_USAGE; } - extern char *__progname; - static int usage(void) { ! struct getentdb *curdb; ! ! fprintf(stderr, "Usage: %s database [key ...]\n", __progname); ! fprintf(stderr, " database may be one of:\n\t"); ! for (curdb = databases; curdb->name != NULL; curdb++) { ! fprintf(stderr, " %s", curdb->name); ! } ! fprintf(stderr, "\n"); exit(RV_USAGE); /* NOTREACHED */ } - static int - parsenum(const char *word, unsigned long *result) - { - unsigned long num; - char *ep; - - assert(word != NULL); - assert(result != NULL); - - if (!isdigit((unsigned char)word[0])) - return 0; - errno = 0; - num = strtoul(word, &ep, 10); - if (num == ULONG_MAX && errno == ERANGE) - return 0; - if (*ep != '\0') - return 0; - *result = num; - return 1; - } - /* * printfmtstrings -- * vprintf(format, ...), --- 106,127 ---- usage(); for (curdb = databases; curdb->name != NULL; curdb++) { if (strcmp(curdb->name, argv[1]) == 0) { ! exit(curdb->fn(argc, argv)); break; } } ! fprintf(stderr, "%s: unknown database: %s\n", __progname, argv[1]); return RV_USAGE; } static int usage(void) { ! fprintf(stderr, "usage: %s database [key ...]\n", __progname); exit(RV_USAGE); /* NOTREACHED */ } /* * printfmtstrings -- * vprintf(format, ...), *************** *** 173,178 **** --- 138,144 ---- va_start(ap, fmt); vprintf(fmt, ap); + va_end(ap); curpref = prefix; for (i = 0; strings[i] != NULL; i++) { *************** *** 182,207 **** printf("\n"); } - /* - * ethers - */ - static int ethers(int argc, char *argv[]) { ! char hostname[MAXHOSTNAMELEN + 1], *hp; struct ether_addr ea, *eap; - int i, rv; - assert(argc > 1); - assert(argv != NULL); - - #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp) - - rv = RV_OK; if (argc == 2) { ! fprintf(stderr, "Enumeration not supported on ethers\n"); rv = RV_NOENUM; } else { for (i = 2; i < argc; i++) { --- 148,165 ---- printf("\n"); } + #define ETHERSPRINT printf("%-17s %s\n", ether_ntoa(eap), hp) static int ethers(int argc, char *argv[]) { ! char hostname[MAXHOSTNAMELEN], *hp; ! int i, rv = RV_OK; struct ether_addr ea, *eap; if (argc == 2) { ! fprintf(stderr, "%s: Enumeration not supported on ethers\n", ! __progname); rv = RV_NOENUM; } else { for (i = 2; i < argc; i++) { *************** *** 225,255 **** return rv; } ! /* ! * group ! */ 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]); --- 183,208 ---- return rv; } ! #define GROUPPRINT \ ! printfmtstrings(gr->gr_mem, ":", ",", "%s:%s:%u", \ ! gr->gr_name, gr->gr_passwd, gr->gr_gid) 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]); *************** *** 265,281 **** return rv; } - - /* - * hosts - */ - static void hostsprint(const struct hostent *he) { char buf[INET6_ADDRSTRLEN]; - assert(he != NULL); if (inet_ntop(he->h_addrtype, he->h_addr, buf, sizeof(buf)) == NULL) strlcpy(buf, "# unknown", sizeof(buf)); printfmtstrings(he->h_aliases, " ", " ", "%-16s %s", buf, he->h_name); --- 218,228 ---- *************** *** 284,298 **** static int hosts(int argc, char *argv[]) { - struct hostent *he; char addr[IN6ADDRSZ]; ! int i, rv; - assert(argc > 1); - assert(argv != NULL); - sethostent(1); - rv = RV_OK; if (argc == 2) { while ((he = gethostent()) != NULL) hostsprint(he); --- 231,241 ---- static int hosts(int argc, char *argv[]) { char addr[IN6ADDRSZ]; ! int i, rv = RV_OK; ! struct hostent *he; sethostent(1); if (argc == 2) { while ((he = gethostent()) != NULL) hostsprint(he); *************** *** 316,333 **** return rv; } - - /* - * networks - */ - static void networksprint(const struct netent *ne) { char buf[INET6_ADDRSTRLEN]; ! struct in_addr ianet; - assert(ne != NULL); ianet = inet_makeaddr(ne->n_net, 0); if (inet_ntop(ne->n_addrtype, &ianet, buf, sizeof(buf)) == NULL) strlcpy(buf, "# unknown", sizeof(buf)); --- 259,270 ---- return rv; } static void networksprint(const struct netent *ne) { char buf[INET6_ADDRSTRLEN]; ! struct in_addr ianet; ianet = inet_makeaddr(ne->n_net, 0); if (inet_ntop(ne->n_addrtype, &ianet, buf, sizeof(buf)) == NULL) strlcpy(buf, "# unknown", sizeof(buf)); *************** *** 337,351 **** static int networks(int argc, char *argv[]) { struct netent *ne; in_addr_t net; - int i, rv; - assert(argc > 1); - assert(argv != NULL); - setnetent(1); - rv = RV_OK; if (argc == 2) { while ((ne = getnetent()) != NULL) networksprint(ne); --- 274,284 ---- static int networks(int argc, char *argv[]) { + int i, rv = RV_OK; struct netent *ne; in_addr_t net; setnetent(1); if (argc == 2) { while ((ne = getnetent()) != NULL) networksprint(ne); *************** *** 368,400 **** return rv; } - /* - * passwd - */ - static int passwd(int argc, char *argv[]) { struct passwd *pw; - unsigned long id; - int i, rv; - assert(argc > 1); - assert(argv != NULL); - - #define PASSWDPRINT printf("%s:%s:%u:%u:%s:%s:%s\n", \ - pw->pw_name, pw->pw_passwd, pw->pw_uid, \ - pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell) - setpassent(1); - rv = RV_OK; if (argc == 2) { while ((pw = getpwent()) != NULL) PASSWDPRINT; } else { for (i = 2; i < argc; i++) { ! if (parsenum(argv[i], &id)) pw = getpwuid((uid_t)id); else pw = getpwnam(argv[i]); --- 301,327 ---- return rv; } + #define PASSWDPRINT \ + printf("%s:%s:%u:%u:%s:%s:%s\n", \ + pw->pw_name, pw->pw_passwd, pw->pw_uid, \ + pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell) static int passwd(int argc, char *argv[]) { + int i, rv = RV_OK; struct passwd *pw; setpassent(1); if (argc == 2) { while ((pw = getpwent()) != NULL) PASSWDPRINT; } else { for (i = 2; i < argc; i++) { ! const char *err; ! long long id = strtonum(argv[i], 0, UINT_MAX, &err); ! ! if (!err) pw = getpwuid((uid_t)id); else pw = getpwnam(argv[i]); *************** *** 410,441 **** return rv; } - /* - * protocols - */ - static int protocols(int argc, char *argv[]) { struct protoent *pe; ! unsigned long id; ! int i, rv; - assert(argc > 1); - assert(argv != NULL); - - #define PROTOCOLSPRINT printfmtstrings(pe->p_aliases, " ", " ", \ - "%-16s %5d", pe->p_name, pe->p_proto) - setprotoent(1); - rv = RV_OK; if (argc == 2) { while ((pe = getprotoent()) != NULL) PROTOCOLSPRINT; } else { for (i = 2; i < argc; i++) { ! if (parsenum(argv[i], &id)) pe = getprotobynumber((int)id); else pe = getprotobyname(argv[i]); --- 337,362 ---- return rv; } + #define PROTOCOLSPRINT \ + printfmtstrings(pe->p_aliases, " ", " ", \ + "%-16s %5d", pe->p_name, pe->p_proto) static int protocols(int argc, char *argv[]) { struct protoent *pe; ! int i, rv = RV_OK; setprotoent(1); if (argc == 2) { while ((pe = getprotoent()) != NULL) PROTOCOLSPRINT; } else { for (i = 2; i < argc; i++) { ! const char *err; ! long long id = strtonum(argv[i], 0, UINT_MAX, &err); ! ! if (!err) pe = getprotobynumber((int)id); else pe = getprotobyname(argv[i]); *************** *** 451,482 **** return rv; } ! /* ! * rpc ! */ static int rpc(int argc, char *argv[]) { struct rpcent *re; ! unsigned long id; ! int i, rv; ! ! assert(argc > 1); ! assert(argv != NULL); - #define RPCPRINT printfmtstrings(re->r_aliases, " ", " ", \ - "%-16s %6d", \ - re->r_name, re->r_number) - setrpcent(1); - rv = RV_OK; if (argc == 2) { while ((re = getrpcent()) != NULL) RPCPRINT; } else { for (i = 2; i < argc; i++) { ! if (parsenum(argv[i], &id)) re = getrpcbynumber((int)id); else re = getrpcbyname(argv[i]); --- 372,397 ---- return rv; } ! #define RPCPRINT \ ! printfmtstrings(re->r_aliases, " ", " ", \ ! "%-16s %6d", re->r_name, re->r_number) static int rpc(int argc, char *argv[]) { struct rpcent *re; ! int i, rv = RV_OK; setrpcent(1); if (argc == 2) { while ((re = getrpcent()) != NULL) RPCPRINT; } else { for (i = 2; i < argc; i++) { ! const char *err; ! long long id = strtonum(argv[i], 0, UINT_MAX, &err); ! ! if (!err) re = getrpcbynumber((int)id); else re = getrpcbyname(argv[i]); *************** *** 492,527 **** return rv; } ! /* ! * services ! */ static int services(int argc, char *argv[]) { struct servent *se; ! unsigned long id; ! char *proto; ! int i, rv; - assert(argc > 1); - assert(argv != NULL); - - #define SERVICESPRINT printfmtstrings(se->s_aliases, " ", " ", \ - "%-16s %5d/%s", \ - se->s_name, ntohs(se->s_port), se->s_proto) - setservent(1); - rv = RV_OK; if (argc == 2) { while ((se = getservent()) != NULL) SERVICESPRINT; } else { for (i = 2; i < argc; i++) { ! proto = strchr(argv[i], '/'); if (proto != NULL) *proto++ = '\0'; ! if (parsenum(argv[i], &id)) se = getservbyport((int)id, proto); else se = getservbyname(argv[i], proto); --- 407,436 ---- return rv; } ! #define SERVICESPRINT \ ! printfmtstrings(se->s_aliases, " ", " ", \ ! "%-16s %5d/%s", se->s_name, ntohs(se->s_port), se->s_proto) static int services(int argc, char *argv[]) { struct servent *se; ! int i, rv = RV_OK; setservent(1); if (argc == 2) { while ((se = getservent()) != NULL) SERVICESPRINT; } else { for (i = 2; i < argc; i++) { ! const char *err; ! long long id; ! char *proto = strchr(argv[i], '/'); ! if (proto != NULL) *proto++ = '\0'; ! id = strtonum(argv[i], 0, UINT_MAX, &err); ! if (!err) se = getservbyport((int)id, proto); else se = getservbyname(argv[i], proto); *************** *** 537,560 **** return rv; } - /* - * shells - */ - static int shells(int argc, char *argv[]) { const char *sh; ! int i, rv; - assert(argc > 1); - assert(argv != NULL); - - #define SHELLSPRINT printf("%s\n", sh) - setusershell(); - rv = RV_OK; if (argc == 2) { while ((sh = getusershell()) != NULL) SHELLSPRINT; --- 446,460 ---- return rv; } + #define SHELLSPRINT printf("%s\n", sh) static int shells(int argc, char *argv[]) { const char *sh; ! int i, rv = RV_OK; setusershell(); if (argc == 2) { while ((sh = getusershell()) != NULL) SHELLSPRINT;