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