# # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright (c) 2018 Peter Tribble. # Copyright (c) 2014 Gary Mills # Copyright 2008 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # PROG= getent include ../Makefile.cmd OBJECTS= \ dogetauthattr.o \ dogetethers.o \ dogetexecattr.o \ dogetgr.o \ dogethost.o \ dogetipnodes.o \ dogetnet.o \ dogetnetgr.o \ dogetnetmask.o \ dogetprofattr.o \ dogetproject.o \ dogetproto.o \ dogetpw.o \ dogetsp.o \ dogetserv.o \ dogetuserattr.o \ getent.o SRCS= $(OBJECTS:.o=.c) LDLIBS += -lsecdb -lsocket -lnsl -lproject # # for message catalog # POFILE= gettent.po POFILES= $(SRCS:%.c=%.po) .KEEP_STATE: all: $(PROG) $(POFILE): $(POFILES) $(RM) $@ cat $(POFILES) > $@ $(PROG): $(OBJECTS) $(LINK.c) -o $@ $(OBJECTS) $(LDLIBS) $(POST_PROCESS) install: all $(ROOTPROG) clean: $(RM) $(OBJECTS) lint: lint_SRCS include ../Makefile.targ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include "getent.h" static int putauthattr(const authattr_t *auth, FILE *fp) { int i; kva_t *attrs; kv_t *data; if (auth == NULL) return (1); if (fprintf(fp, "%s:%s:%s:%s:%s:", auth->name != NULL ? auth->name : "", auth->res1 != NULL ? auth->res1 : "", auth->res2 != NULL ? auth->res2 : "", auth->short_desc != NULL ? auth->short_desc : "", auth->long_desc != NULL ? auth->long_desc : "") == EOF) return (1); attrs = auth->attr; if (attrs != NULL) { data = attrs->data; for (i = 0; i < attrs->length; i++) { if (fprintf(fp, "%s=%s%s", data[i].key != NULL ? data[i].key : "", data[i].value != NULL ? data[i].value : "", i < (attrs->length)-1 ? ";" : "") == EOF) return (1); } } if (putc('\n', fp) == EOF) return (1); return (0); } int dogetauthattr(const char **list) { authattr_t *pauth; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { setauthattr(); while ((pauth = getauthattr()) != NULL) (void) putauthattr(pauth, stdout); endauthattr(); } else { for (; *list != NULL; list++) { pauth = getauthnam(*list); if (pauth == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putauthattr(pauth, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 1994,2002 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include "getent.h" static int putethers(const char *hostname, const struct ether_addr *e, FILE *fp) { if (hostname == NULL || e == NULL) return (EXC_SYNTAX); if (fprintf(fp, "%-20s %s\n", hostname, ether_ntoa(e)) == EOF) return (EXC_SYNTAX); /* for lack of a better error code */ return (EXC_SUCCESS); } /* * ether_ntohost/hostton - get entries from ethers database */ int dogetethers(const char **list) { int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { rc = EXC_ENUM_NOT_SUPPORTED; } else { for (; *list != NULL; list++) { struct ether_addr ea; struct ether_addr *e; char hostname[MAXHOSTNAMELEN + 1]; char *hp; int retval; if ((e = ether_aton(*list)) != NULL) { hp = hostname; retval = ether_ntohost(hp, e); } else { hp = (char *)*list; e = &ea; retval = ether_hostton(hp, e); } if (retval != 0) rc = EXC_NAME_NOT_FOUND; else rc = putethers(hp, e, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include "getent.h" static int putexecattr(const execattr_t *exec, FILE *fp) { int i; kva_t *attrs; kv_t *data; if (exec == NULL) return (1); if (fprintf(fp, "%s:%s:%s:%s:%s:%s:", exec->name != NULL ? exec->name : "", exec->policy != NULL ? exec->policy : "", exec->type != NULL ? exec->type : "", exec->res1 != NULL ? exec->res1 : "", exec->res2 != NULL ? exec->res2 : "", exec->id != NULL ? exec->id : "") == EOF) return (1); attrs = exec->attr; if (attrs != NULL) { data = attrs->data; for (i = 0; i < attrs->length; i++) { if (fprintf(fp, "%s=%s%s", data[i].key != NULL ? data[i].key : "", data[i].value != NULL ? data[i].value : "", i < (attrs->length)-1 ? ";" : "") == EOF) return (1); } } if (putc('\n', fp) == EOF) return (1); return (0); } int dogetexecattr(const char **list) { execattr_t *pexec; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { setexecattr(); while ((pexec = getexecattr()) != NULL) (void) putexecattr(pexec, stdout); endexecattr(); } else { for (; *list != NULL; list++) { pexec = getexecprof(*list, NULL, NULL, GET_ALL); if (pexec == NULL) { rc = EXC_NAME_NOT_FOUND; } else { for (; pexec != NULL; pexec = pexec->next) { (void) putexecattr(pexec, stdout); } } } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include "getent.h" static int putgrent(const struct group *grp, FILE *fp) { char **mem; int rc = 0; if (grp == NULL) { return (1); } if (fprintf(fp, "%s:%s:%u:", grp->gr_name != NULL ? grp->gr_name : "", grp->gr_passwd != NULL ? grp->gr_passwd : "", grp->gr_gid) == EOF) rc = 1; mem = grp ->gr_mem; if (mem != NULL) { if (*mem != NULL) if (fputs(*mem++, fp) == EOF) rc = 1; while (*mem != NULL) if (fprintf(fp, ",%s", *mem++) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; return (rc); } int dogetgr(const char **list) { struct group *grp; int rc = EXC_SUCCESS; char *ptr; gid_t gid; if (list == NULL || *list == NULL) { while ((grp = getgrent()) != NULL) (void) putgrent(grp, stdout); } else { for (; *list != NULL; list++) { errno = 0; /* * Here we assume that the argument passed is * a gid, if it can be completely transformed * to a long integer. So we check for gid in * the database and if we fail then we check * for the group name. * If the argument passed is not numeric, then * we take it as the group name and proceed. */ gid = strtoul(*list, &ptr, 10); if (!(*ptr == '\0' && errno == 0) || ((grp = getgrgid(gid)) == NULL)) { grp = getgrnam(*list); } if (grp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putgrent(grp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1994-2000 by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include #include #include #include #include "getent.h" static int puthostent(const struct hostent *hp, FILE *fp) { char **p; int rc = 0; if (hp == NULL) { return (1); } for (p = hp->h_addr_list; *p != 0; p++) { struct in_addr in; char **q; (void) memcpy((char *)&in.s_addr, *p, sizeof (in)); if (fprintf(fp, "%s\t%s", inet_ntoa(in), hp->h_name) == EOF) rc = 1; for (q = hp->h_aliases; *q != 0; q++) { if (fprintf(fp, " %s", *q) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; } return (rc); } /* * gethostbyname/addr - get entries from hosts database */ int dogethost(const char **list) { struct hostent *hp; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { while ((hp = gethostent()) != NULL) (void) puthostent(hp, stdout); } else { for (; *list != NULL; list++) { struct in_addr addr; addr.s_addr = inet_addr(*list); if (addr.s_addr != (in_addr_t)-1) hp = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET); else hp = gethostbyname(*list); if (hp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) puthostent(hp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright (c) 1994-1999, by Sun Microsystems, Inc. */ #include #include #include #include #include #include #include #include #include "getent.h" static int puthostent(const struct hostent *hp, FILE *fp) { char **p; int rc = 0; char obuf[INET6_ADDRSTRLEN]; if (hp == NULL) { return (1); } for (p = hp->h_addr_list; *p != 0; p++) { void *addr; struct in_addr in4; int af; const char *res; char **q; if (hp->h_addrtype == AF_INET6) { if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)*p)) { IN6_V4MAPPED_TO_INADDR((struct in6_addr *)*p, &in4); af = AF_INET; addr = &in4; } else { af = AF_INET6; addr = *p; } } else { af = AF_INET; addr = *p; } res = inet_ntop(af, addr, obuf, sizeof (obuf)); if (res == 0) { rc = 1; continue; } if (fprintf(fp, "%s\t%s", res, hp->h_name) == EOF) rc = 1; for (q = hp->h_aliases; q && *q; q++) { if (fprintf(fp, " %s", *q) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; } return (rc); } /* * getipnodebyname/addr - get entries from ipnodes database */ int dogetipnodes(const char **list) { struct hostent *hp; int rc = EXC_SUCCESS; struct in6_addr in6; struct in_addr in4; int af, len; void *addr; int err_ret; if (list == NULL || *list == NULL) { rc = EXC_ENUM_NOT_SUPPORTED; } else { for (; *list != NULL; list++) { if (strchr(*list, ':') != 0) { af = AF_INET6; len = sizeof (in6); addr = &in6; } else { af = AF_INET; len = sizeof (in4); addr = &in4; } if (inet_pton(af, *list, addr) == 1) hp = getipnodebyaddr(addr, len, af, &err_ret); else hp = getipnodebyname(*list, AF_INET6, AI_V4MAPPED|AI_ALL, &err_ret); if (hp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) puthostent(hp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ #ident "%Z%%M% %I% %E% SMI" /* * Copyright (c) 1994, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include #include #include #include #include "getent.h" /* * Print a network number such as 129.144 */ char * inet_nettoa(struct in_addr in) { u_long addr = htonl(in.s_addr); u_char *up = (u_char *)&addr; static char result[256]; /* Omit leading zeros */ if (up[0]) { (void) sprintf(result, "%d.%d.%d.%d", up[0], up[1], up[2], up[3]); } else if (up[1]) { (void) sprintf(result, "%d.%d.%d", up[1], up[2], up[3]); } else if (up[2]) { (void) sprintf(result, "%d.%d", up[2], up[3]); } else { (void) sprintf(result, "%d", up[3]); } return (result); } static int putnetent(const struct netent *np, FILE *fp) { char **p; int rc = 0; struct in_addr in; if (np == NULL) { return (1); } in.s_addr = np->n_net; if (fprintf(fp, "%-20s %s", np->n_name, inet_nettoa(in)) == EOF) rc = 1; for (p = np->n_aliases; *p != 0; p++) { if (fprintf(fp, " %s", *p) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; return (rc); } /* * getnetbyname/addr - get entries from network database */ int dogetnet(const char **list) { struct netent *np; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { while ((np = getnetent()) != NULL) (void) putnetent(np, stdout); } else { for (; *list != NULL; list++) { long addr = inet_network(*list); if (addr != -1) np = getnetbyaddr(addr, AF_INET); else np = getnetbyname(*list); if (np == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putnetent(np, stdout); } } return (rc); } /* * This file and its contents are supplied under the terms of the * Common Development and Distribution License ("CDDL"), version 1.0. * You may only use this file in accordance with the terms of version * 1.0 of the CDDL. * * A full copy of the text of the CDDL should have accompanied this * source. A copy of the CDDL is also available via the Internet at * http://www.illumos.org/license/CDDL. */ /* * Copyright 2012 Nexenta Systems, Inc. All rights reserved. * Copyright 2021 Planets Communications B.V. */ #include #include #include #include #include #include "getent.h" int dogetnetgr(const char **list) { uint_t cnt; char *host, *user, *dom; const char *host_filter, *user_filter, *dom_filter; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) return (EXC_ENUM_NOT_SUPPORTED); /* * Count the arguments given. */ cnt = 0; while (list[cnt] != NULL) cnt++; switch (cnt) { case 1: if (setnetgrent(list[0]) != 0) return (EXC_ERROR); printf("%s", list[0]); while (getnetgrent(&host, &user, &dom) != 0) { printf(" (%s,%s,%s)", (host) ? host : "", (user) ? user : "", (dom) ? dom : ""); } printf("\n"); break; case 4: host_filter = (strcmp(list[1], "*") == 0) ? NULL : list[1]; user_filter = (strcmp(list[2], "*") == 0) ? NULL : list[2]; dom_filter = (strcmp(list[3], "*") == 0) ? NULL : list[3]; printf("%-21s (%s,%s,%s) = %d\n", list[0], (host_filter) ? host_filter : "", (user_filter) ? user_filter : "", (dom_filter) ? dom_filter : "", innetgr(list[0], host_filter, user_filter, dom_filter)); break; default: rc = EXC_SYNTAX; break; } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright (c) 1994-1996, by Sun Microsystems, Inc. * All rights reserved. */ #include #include #include #include #include #include #include #include #include #include "getent.h" extern char *inet_nettoa(struct in_addr in); static int putnetmask(const struct in_addr key, const struct in_addr netmask, FILE *fp) { int rc = 0; struct in_addr net; net.s_addr = ntohl(key.s_addr); if (fprintf(fp, "%-20s", inet_nettoa(net)) == EOF) rc = 1; if (fprintf(fp, " %s", inet_ntoa(netmask)) == EOF) rc = 1; if (putc('\n', fp) == EOF) rc = 1; return (rc); } /* * getnetmaskbyaddr - get entries from network database */ int dogetnetmask(const char **list) { int rc = EXC_SUCCESS; struct in_addr addr, netmask; if (list == NULL || *list == NULL) return (EXC_ENUM_NOT_SUPPORTED); for (; *list != NULL; list++) { addr.s_addr = htonl(inet_network(*list)); if (addr.s_addr != -1) { if (getnetmaskbyaddr(addr, &netmask) == 0) { (void) putnetmask(addr, netmask, stdout); } else { rc = EXC_NAME_NOT_FOUND; } } else { rc = EXC_NAME_NOT_FOUND; } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include "getent.h" static int putprofattr(const profattr_t *prof, FILE *fp) { int i; kva_t *attrs; kv_t *data; if (prof == NULL) return (1); if (fprintf(fp, "%s:%s:%s:%s:", prof->name != NULL ? prof->name : "", prof->res1 != NULL ? prof->res1 : "", prof->res2 != NULL ? prof->res2 : "", prof->desc != NULL ? prof->desc : "") == EOF) return (1); attrs = prof->attr; if (attrs != NULL) { data = attrs->data; for (i = 0; i < attrs->length; i++) { if (fprintf(fp, "%s=%s%s", data[i].key != NULL ? data[i].key : "", data[i].value != NULL ? data[i].value : "", i < (attrs->length)-1 ? ";" : "") == EOF) return (1); } } if (putc('\n', fp) == EOF) return (1); return (0); } int dogetprofattr(const char **list) { profattr_t *pprof; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { setprofattr(); while ((pprof = getprofattr()) != NULL) (void) putprofattr(pprof, stdout); endprofattr(); } else { for (; *list != NULL; list++) { pprof = getprofnam(*list); if (pprof == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putprofattr(pprof, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include "getent.h" static int putprojent(const struct project *proj, FILE *fp) { char **names; if (proj == NULL) return (1); if (fprintf(fp, "%s:%ld:%s:", proj->pj_name != NULL ? proj->pj_name : "", proj->pj_projid, proj->pj_comment != NULL ? proj->pj_comment : "") == EOF) return (1); names = proj->pj_users; if (names != NULL) { if (*names != NULL) if (fputs(*names++, fp) == EOF) return (1); while (*names != NULL) if (fprintf(fp, ",%s", *names++) == EOF) return (1); } if (putc(':', fp) == EOF) return (1); names = proj->pj_groups; if (names != NULL) { if (*names != NULL) if (fputs(*names++, fp) == EOF) return (1); while (*names != NULL) if (fprintf(fp, ",%s", *names++) == EOF) return (1); } if (putc(':', fp) == EOF) return (1); if (fprintf(fp, "%s\n", proj->pj_attr != NULL ? proj->pj_attr : "") == EOF) return (1); return (0); } int dogetproject(const char **list) { struct project proj; struct project *pproj; projid_t projid; void *buf[PROJECT_BUFSZ]; int rc = EXC_SUCCESS; char *ptr; if (list == NULL || *list == NULL) { setprojent(); while ((pproj = getprojent(&proj, buf, PROJECT_BUFSZ)) != NULL) (void) putprojent(pproj, stdout); endprojent(); } else { for (; *list != NULL; list++) { projid = strtol(*list, &ptr, 10); if (ptr == *list) pproj = getprojbyname(*list, &proj, buf, PROJECT_BUFSZ); else pproj = getprojbyid(projid, &proj, buf, PROJECT_BUFSZ); if (pproj == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putprojent(pproj, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ #ident "%Z%%M% %I% %E% SMI" /* * Copyright (c) 1994, by Sun Microsystems, Inc. */ #include #include #include #include #include #include #include "getent.h" static int putprotoent(const struct protoent *pp, FILE *fp) { char **p; int rc = 0; if (pp == NULL) { return (1); } if (fprintf(fp, "%-20s %d", pp->p_name, pp->p_proto) == EOF) rc = 1; for (p = pp->p_aliases; *p != 0; p++) { if (fprintf(fp, " %s", *p) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; return (rc); } /* * getprotobyname/addr - get entries from protocols database */ int dogetproto(const char **list) { struct protoent *pp; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { while ((pp = getprotoent()) != NULL) (void) putprotoent(pp, stdout); } else { for (; *list != NULL; list++) { int protocol = atoi(*list); if (protocol != 0) pp = getprotobynumber(protocol); else pp = getprotobyname(*list); if (pp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putprotoent(pp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include "getent.h" /* * getpwnam - get entries from password database */ int dogetpw(const char **list) { struct passwd *pwp; int rc = EXC_SUCCESS; char *ptr; uid_t uid; if (list == NULL || *list == NULL) { while ((pwp = getpwent()) != NULL) (void) putpwent(pwp, stdout); } else { for (; *list != NULL; list++) { errno = 0; /* * Here we assume that the argument passed is * a uid, if it can be completely transformed * to a long integer. So we check for uid in * the database and if we fail then we check * for the user name. * If the argument passed is not numeric, then * we take it as the user name and proceed. */ uid = strtoul(*list, &ptr, 10); if (!(*ptr == '\0' && errno == 0) || ((pwp = getpwuid(uid)) == NULL)) { pwp = getpwnam(*list); } if (pwp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putpwent(pwp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 1994, by Sun Microsystems, Inc. */ #include #include #include #include #include #include #include #include "getent.h" static int putservent(const struct servent *sp, FILE *fp) { char **p; int rc = 0; if (sp == NULL) { return (1); } if (fprintf(fp, "%-20s %d/%s", sp->s_name, ntohs(sp->s_port), sp->s_proto) == EOF) rc = 1; for (p = sp->s_aliases; *p != 0; p++) { if (fprintf(fp, " %s", *p) == EOF) rc = 1; } if (putc('\n', fp) == EOF) rc = 1; return (rc); } /* * getservbyname/addr - get entries from service database * Accepts arguments as: * port/protocol * port * name/protocol * name */ int dogetserv(const char **list) { struct servent *sp; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { while ((sp = getservent()) != NULL) (void) putservent(sp, stdout); } else { for (; *list != NULL; list++) { int port; char key[BUFSIZ]; const char *protocol = NULL; char *cp; /* Copy string to avoiding modifying the argument */ (void) strncpy(key, *list, sizeof (key)); key[sizeof (key) - 1] = '\0'; /* Split at a '/' to extract protocol number */ if ((cp = strchr(key, '/')) != NULL) { *cp = '\0'; protocol = cp + 1; } port = htons(atoi(key)); if (port != 0) sp = getservbyport(port, protocol); else sp = getservbyname(key, protocol); if (sp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putservent(sp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright (c) 2014 Gary Mills * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Copyright 2012 Nexenta Systems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include "getent.h" /* * getspnam - get entries from shadow database */ int dogetsp(const char **list) { struct spwd *sp; int rc = EXC_SUCCESS; if (list == NULL || *list == NULL) { setspent(); while ((sp = getspent()) != NULL) (void) putspent(sp, stdout); endspent(); } else { for (; *list != NULL; list++) { sp = getspnam(*list); if (sp == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putspent(sp, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include "getent.h" static int putuserattr(const userattr_t *user, FILE *fp) { int i; kva_t *attrs; kv_t *data; if (user == NULL) return (1); if (fprintf(fp, "%s:%s:%s:%s:", user->name != NULL ? user->name : "", user->qualifier != NULL ? user->res1 : "", user->res1 != NULL ? user->res1 : "", user->res2 != NULL ? user->res2 : "") == EOF) return (1); attrs = user->attr; if (attrs != NULL) { data = attrs->data; for (i = 0; i < attrs->length; i++) { if (fprintf(fp, "%s=%s%s", data[i].key != NULL ? data[i].key : "", data[i].value != NULL ? data[i].value : "", i < (attrs->length)-1 ? ";" : "") == EOF) return (1); } } if (putc('\n', fp) == EOF) return (1); return (0); } int dogetuserattr(const char **list) { struct passwd *pwp; userattr_t *puser; int rc = EXC_SUCCESS; char *ptr; uid_t uid; if (list == NULL || *list == NULL) { setuserattr(); while ((puser = getuserattr()) != NULL) (void) putuserattr(puser, stdout); enduserattr(); } else { for (; *list != NULL; list++) { uid = strtoul(*list, &ptr, 10); if (*ptr == '\0' && errno == 0) { if ((pwp = getpwuid(uid)) == NULL) { puser = getusernam(*list); } else { puser = getusernam(pwp->pw_name); } } else { puser = getusernam(*list); } if (puser == NULL) rc = EXC_NAME_NOT_FOUND; else (void) putuserattr(puser, stdout); } } return (rc); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright (c) 2014 Gary Mills * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include "getent.h" static const char *cmdname; struct table { char *name; /* name of the table */ int (*func)(const char **); /* function to do the lookup */ }; static struct table t[] = { { "passwd", dogetpw }, { "shadow", dogetsp }, { "group", dogetgr }, { "hosts", dogethost }, { "ipnodes", dogetipnodes }, { "services", dogetserv }, { "protocols", dogetproto }, { "ethers", dogetethers }, { "networks", dogetnet }, { "netmasks", dogetnetmask }, { "project", dogetproject }, { "auth_attr", dogetauthattr }, { "exec_attr", dogetexecattr }, { "prof_attr", dogetprofattr }, { "user_attr", dogetuserattr }, { "netgroup", dogetnetgr }, { NULL, NULL } }; static void usage(void) __NORETURN; int main(int argc, const char **argv) { struct table *p; (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) #define TEXT_DOMAIN "SYS_TEXT" #endif (void) textdomain(TEXT_DOMAIN); cmdname = argv[0]; if (argc < 2) usage(); for (p = t; p->name != NULL; p++) { if (strcmp(argv[1], p->name) == 0) { int rc; rc = (*p->func)(&argv[2]); switch (rc) { case EXC_SYNTAX: (void) fprintf(stderr, gettext("Syntax error\n")); break; case EXC_ERROR: (void) fprintf(stderr, gettext("Internal error\n")); break; case EXC_ENUM_NOT_SUPPORTED: (void) fprintf(stderr, gettext("Enumeration not supported on %s\n"), argv[1]); break; case EXC_NAME_NOT_FOUND: break; } exit(rc); } } (void) fprintf(stderr, gettext("Unknown database: %s\n"), argv[1]); usage(); /* NOTREACHED */ } static void usage(void) { (void) fprintf(stderr, gettext("usage: %s database [ key ... ]\n"), cmdname); exit(EXC_SYNTAX); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2018 Peter Tribble. * Copyright (c) 2014 Gary Mills * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #ifndef _GETENT_H #define _GETENT_H #ifdef __cplusplus extern "C" { #endif #define TRUE 1 #define FALSE 0 #define EXC_SUCCESS 0 #define EXC_SYNTAX 1 #define EXC_NAME_NOT_FOUND 2 #define EXC_ENUM_NOT_SUPPORTED 3 #define EXC_ERROR 4 extern int dogetpw(const char **); extern int dogetsp(const char **); extern int dogetgr(const char **); extern int dogethost(const char **); extern int dogetipnodes(const char **); extern int dogetserv(const char **); extern int dogetnet(const char **); extern int dogetproto(const char **); extern int dogetethers(const char **); extern int dogetnetmask(const char **); extern int dogetproject(const char **); extern int dogetauthattr(const char **); extern int dogetexecattr(const char **); extern int dogetprofattr(const char **); extern int dogetuserattr(const char **); extern int dogetnetgr(const char **); #ifdef __cplusplus } #endif #endif /* _GETENT_H */