1 /* Copyright (c) 1997-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
23 #include <rpcsvc/nis.h>
26 nis_local_group (void)
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
31 if (__nisgroup[0] == '\0'
32 && (cptr = getenv ("NIS_GROUP")) != NULL
33 && strlen (cptr) < NIS_MAXNAMELEN)
35 char *cp = stpcpy (__nisgroup, cptr);
39 cptr = nis_local_directory ();
40 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
52 libnsl_hidden_def (nis_local_group)
55 nis_local_directory (void)
57 static char __nisdomainname[NIS_MAXNAMELEN + 1];
59 if (__nisdomainname[0] == '\0')
61 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
62 __nisdomainname[0] = '\0';
65 char *cp = rawmemchr (__nisdomainname, '\0');
67 /* Missing trailing dot? */
76 return __nisdomainname;
78 libnsl_hidden_def (nis_local_directory)
81 nis_local_principal (void)
83 static char __principal[NIS_MAXNAMELEN + 1];
85 if (__principal[0] == '\0')
87 char buf[NIS_MAXNAMELEN + 1];
89 uid_t uid = geteuid ();
93 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
94 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
95 uid, nis_local_directory ());
97 if (len >= NIS_MAXNAMELEN - 1)
99 /* XXX The buffer is too small. Can this happen??? */
100 return strcpy (__principal, "nobody");
102 if (buf[len - 1] != '.')
108 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
109 FOLLOW_PATH, NULL, NULL);
114 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
116 if (res->objects.objects_len > 1)
118 /* More than one principal with same uid? something
119 wrong with cred table. Should be unique. Warn user
122 LOCAL entry for UID %d in directory %s not unique\n"),
123 uid, nis_local_directory ());
125 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
126 nis_freeresult (res);
131 nis_freeresult (res);
136 return strcpy (__principal, nis_local_host ());
138 /* Should be never reached */
143 libnsl_hidden_def (nis_local_principal)
146 nis_local_host (void)
148 static char __nishostname[NIS_MAXNAMELEN + 1];
150 if (__nishostname[0] == '\0')
152 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
153 __nishostname[0] = '\0';
156 char *cp = rawmemchr (__nishostname, '\0');
157 int len = cp - __nishostname;
159 /* Hostname already fully qualified? */
161 return __nishostname;
163 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
165 __nishostname[0] = '\0';
166 return __nishostname;
170 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
171 __nishostname[NIS_MAXNAMELEN] = '\0';
175 return __nishostname;
177 libnsl_hidden_def (nis_local_host)