1/* Copyright (c) 1997-2016 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.
4
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.
9
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.
14
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/>. */
18
19#include <errno.h>
20#include <string.h>
21#include <unistd.h>
22#include <libintl.h>
23#include <rpcsvc/nis.h>
24
25nis_name
26nis_local_group (void)
27{
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
29
30 char *cptr;
31 if (__nisgroup[0] == '\0'
32 && (cptr = getenv ("NIS_GROUP")) != NULL
33 && strlen (cptr) < NIS_MAXNAMELEN)
34 {
35 char *cp = stpcpy (__nisgroup, cptr);
36
37 if (cp[-1] != '.')
38 {
39 cptr = nis_local_directory ();
40 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
41 {
42 *cp++ = '.';
43 strcpy (cp, cptr);
44 }
45 else
46 __nisgroup[0] = '\0';
47 }
48 }
49
50 return __nisgroup;
51}
52libnsl_hidden_def (nis_local_group)
53
54nis_name
55nis_local_directory (void)
56{
57 static char __nisdomainname[NIS_MAXNAMELEN + 1];
58
59 if (__nisdomainname[0] == '\0')
60 {
61 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
62 __nisdomainname[0] = '\0';
63 else
64 {
65 char *cp = rawmemchr (__nisdomainname, '\0');
66
67 /* Missing trailing dot? */
68 if (cp[-1] != '.')
69 {
70 *cp++ = '.';
71 *cp = '\0';
72 }
73 }
74 }
75
76 return __nisdomainname;
77}
78libnsl_hidden_def (nis_local_directory)
79
80nis_name
81nis_local_principal (void)
82{
83 static char __principal[NIS_MAXNAMELEN + 1];
84
85 if (__principal[0] == '\0')
86 {
87 char buf[NIS_MAXNAMELEN + 1];
88 nis_result *res;
89 uid_t uid = geteuid ();
90
91 if (uid != 0)
92 {
93 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
94 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
95 uid, nis_local_directory ());
96
97 if (len >= NIS_MAXNAMELEN - 1)
98 nobody:
99 /* XXX The buffer is too small. Can this happen??? */
100 return strcpy (__principal, "nobody");
101
102 if (buf[len - 1] != '.')
103 {
104 buf[len++] = '.';
105 buf[len] = '\0';
106 }
107
108 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
109 FOLLOW_PATH, NULL, NULL);
110
111 if (res == NULL)
112 goto nobody;
113
114 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
115 {
116 if (res->objects.objects_len > 1)
117 {
118 /* More than one principal with same uid? something
119 wrong with cred table. Should be unique. Warn user
120 and continue. */
121 printf (_("\
122LOCAL entry for UID %d in directory %s not unique\n"),
123 uid, nis_local_directory ());
124 }
125 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
126 nis_freeresult (res);
127 return __principal;
128 }
129 else
130 {
131 nis_freeresult (res);
132 goto nobody;
133 }
134 }
135 else
136 return strcpy (__principal, nis_local_host ());
137
138 /* Should be never reached */
139 goto nobody;
140 }
141 return __principal;
142}
143libnsl_hidden_def (nis_local_principal)
144
145nis_name
146nis_local_host (void)
147{
148 static char __nishostname[NIS_MAXNAMELEN + 1];
149
150 if (__nishostname[0] == '\0')
151 {
152 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
153 __nishostname[0] = '\0';
154 else
155 {
156 char *cp = rawmemchr (__nishostname, '\0');
157 int len = cp - __nishostname;
158
159 /* Hostname already fully qualified? */
160 if (cp[-1] == '.')
161 return __nishostname;
162
163 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
164 {
165 __nishostname[0] = '\0';
166 return __nishostname;
167 }
168
169 *cp++ = '.';
170 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
171 __nishostname[NIS_MAXNAMELEN] = '\0';
172 }
173 }
174
175 return __nishostname;
176}
177libnsl_hidden_def (nis_local_host)
178