]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_local_names.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nis / nis_local_names.c
CommitLineData
04277e02 1/* Copyright (c) 1997-2019 Free Software Foundation, Inc.
e61abf83
UD
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
41bdb6e2
AJ
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.
e61abf83
UD
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
41bdb6e2 13 Lesser General Public License for more details.
e61abf83 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
e61abf83
UD
18
19#include <errno.h>
20#include <string.h>
21#include <unistd.h>
4360eafd 22#include <libintl.h>
e61abf83 23#include <rpcsvc/nis.h>
82f43dd2 24#include <shlib-compat.h>
e61abf83
UD
25
26nis_name
27nis_local_group (void)
28{
29 static char __nisgroup[NIS_MAXNAMELEN + 1];
30
7440c23e
UD
31 char *cptr;
32 if (__nisgroup[0] == '\0'
33 && (cptr = getenv ("NIS_GROUP")) != NULL
34 && strlen (cptr) < NIS_MAXNAMELEN)
e61abf83 35 {
7440c23e 36 char *cp = stpcpy (__nisgroup, cptr);
e61abf83 37
714a562f 38 if (cp[-1] != '.')
e61abf83
UD
39 {
40 cptr = nis_local_directory ();
714a562f 41 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
e61abf83 42 {
714a562f
UD
43 *cp++ = '.';
44 strcpy (cp, cptr);
e61abf83
UD
45 }
46 else
714a562f 47 __nisgroup[0] = '\0';
e61abf83
UD
48 }
49 }
50
51 return __nisgroup;
52}
1e4d83f6 53libnsl_hidden_nolink_def (nis_local_group, GLIBC_2_1)
e61abf83
UD
54
55nis_name
56nis_local_directory (void)
57{
58 static char __nisdomainname[NIS_MAXNAMELEN + 1];
e61abf83
UD
59
60 if (__nisdomainname[0] == '\0')
61 {
62 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
714a562f 63 __nisdomainname[0] = '\0';
e61abf83
UD
64 else
65 {
616d9133 66 char *cp = rawmemchr (__nisdomainname, '\0');
e61abf83
UD
67
68 /* Missing trailing dot? */
714a562f 69 if (cp[-1] != '.')
e61abf83 70 {
714a562f
UD
71 *cp++ = '.';
72 *cp = '\0';
e61abf83
UD
73 }
74 }
75 }
76
77 return __nisdomainname;
78}
1e4d83f6 79libnsl_hidden_nolink_def (nis_local_directory, GLIBC_2_1)
e61abf83
UD
80
81nis_name
82nis_local_principal (void)
83{
84 static char __principal[NIS_MAXNAMELEN + 1];
85
86 if (__principal[0] == '\0')
87 {
88 char buf[NIS_MAXNAMELEN + 1];
89 nis_result *res;
90 uid_t uid = geteuid ();
91
92 if (uid != 0)
93 {
714a562f
UD
94 int len = snprintf (buf, NIS_MAXNAMELEN - 1,
95 "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
96 uid, nis_local_directory ());
97
98 if (len >= NIS_MAXNAMELEN - 1)
7440c23e 99 nobody:
714a562f
UD
100 /* XXX The buffer is too small. Can this happen??? */
101 return strcpy (__principal, "nobody");
e61abf83 102
714a562f
UD
103 if (buf[len - 1] != '.')
104 {
105 buf[len++] = '.';
106 buf[len] = '\0';
107 }
e61abf83 108
34a5a146
JM
109 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS
110 + FOLLOW_PATH, NULL, NULL);
e61abf83
UD
111
112 if (res == NULL)
7440c23e 113 goto nobody;
e61abf83 114
91eee4dd 115 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
e61abf83
UD
116 {
117 if (res->objects.objects_len > 1)
118 {
119 /* More than one principal with same uid? something
714a562f 120 wrong with cred table. Should be unique. Warn user
e61abf83
UD
121 and continue. */
122 printf (_("\
123LOCAL entry for UID %d in directory %s not unique\n"),
124 uid, nis_local_directory ());
125 }
126 strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
127 nis_freeresult (res);
128 return __principal;
129 }
130 else
131 {
132 nis_freeresult (res);
7440c23e 133 goto nobody;
e61abf83
UD
134 }
135 }
136 else
714a562f 137 return strcpy (__principal, nis_local_host ());
e61abf83
UD
138
139 /* Should be never reached */
7440c23e 140 goto nobody;
e61abf83
UD
141 }
142 return __principal;
143}
1e4d83f6 144libnsl_hidden_nolink_def (nis_local_principal, GLIBC_2_1)
e61abf83
UD
145
146nis_name
147nis_local_host (void)
148{
149 static char __nishostname[NIS_MAXNAMELEN + 1];
e61abf83
UD
150
151 if (__nishostname[0] == '\0')
152 {
e61abf83 153 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
3e5f5557
UD
154 __nishostname[0] = '\0';
155 else
156 {
616d9133 157 char *cp = rawmemchr (__nishostname, '\0');
714a562f 158 int len = cp - __nishostname;
e61abf83 159
3e5f5557 160 /* Hostname already fully qualified? */
714a562f 161 if (cp[-1] == '.')
3e5f5557 162 return __nishostname;
e61abf83 163
714a562f 164 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
3e5f5557
UD
165 {
166 __nishostname[0] = '\0';
167 return __nishostname;
168 }
e61abf83 169
3e5f5557
UD
170 *cp++ = '.';
171 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
172 __nishostname[NIS_MAXNAMELEN] = '\0';
e61abf83 173 }
e61abf83
UD
174 }
175
176 return __nishostname;
177}
1e4d83f6 178libnsl_hidden_nolink_def (nis_local_host, GLIBC_2_1)