]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_local_names.c
resolv: Deprecate the "inet6" option and RES_USE_INET6 [BZ #19582]
[thirdparty/glibc.git] / nis / nis_local_names.c
CommitLineData
f7a9f785 1/* Copyright (c) 1997-2016 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
PE
16 License along with the GNU C Library; if not, see
17 <http://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>
e61abf83
UD
24
25nis_name
26nis_local_group (void)
27{
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
29
7440c23e
UD
30 char *cptr;
31 if (__nisgroup[0] == '\0'
32 && (cptr = getenv ("NIS_GROUP")) != NULL
33 && strlen (cptr) < NIS_MAXNAMELEN)
e61abf83 34 {
7440c23e 35 char *cp = stpcpy (__nisgroup, cptr);
e61abf83 36
714a562f 37 if (cp[-1] != '.')
e61abf83
UD
38 {
39 cptr = nis_local_directory ();
714a562f 40 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
e61abf83 41 {
714a562f
UD
42 *cp++ = '.';
43 strcpy (cp, cptr);
e61abf83
UD
44 }
45 else
714a562f 46 __nisgroup[0] = '\0';
e61abf83
UD
47 }
48 }
49
50 return __nisgroup;
51}
7440c23e 52libnsl_hidden_def (nis_local_group)
e61abf83
UD
53
54nis_name
55nis_local_directory (void)
56{
57 static char __nisdomainname[NIS_MAXNAMELEN + 1];
e61abf83
UD
58
59 if (__nisdomainname[0] == '\0')
60 {
61 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
714a562f 62 __nisdomainname[0] = '\0';
e61abf83
UD
63 else
64 {
616d9133 65 char *cp = rawmemchr (__nisdomainname, '\0');
e61abf83
UD
66
67 /* Missing trailing dot? */
714a562f 68 if (cp[-1] != '.')
e61abf83 69 {
714a562f
UD
70 *cp++ = '.';
71 *cp = '\0';
e61abf83
UD
72 }
73 }
74 }
75
76 return __nisdomainname;
77}
7440c23e 78libnsl_hidden_def (nis_local_directory)
e61abf83
UD
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 {
714a562f
UD
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)
7440c23e 98 nobody:
714a562f
UD
99 /* XXX The buffer is too small. Can this happen??? */
100 return strcpy (__principal, "nobody");
e61abf83 101
714a562f
UD
102 if (buf[len - 1] != '.')
103 {
104 buf[len++] = '.';
105 buf[len] = '\0';
106 }
e61abf83
UD
107
108 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
109 FOLLOW_PATH, NULL, NULL);
110
111 if (res == NULL)
7440c23e 112 goto nobody;
e61abf83 113
91eee4dd 114 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
e61abf83
UD
115 {
116 if (res->objects.objects_len > 1)
117 {
118 /* More than one principal with same uid? something
714a562f 119 wrong with cred table. Should be unique. Warn user
e61abf83
UD
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);
7440c23e 132 goto nobody;
e61abf83
UD
133 }
134 }
135 else
714a562f 136 return strcpy (__principal, nis_local_host ());
e61abf83
UD
137
138 /* Should be never reached */
7440c23e 139 goto nobody;
e61abf83
UD
140 }
141 return __principal;
142}
7440c23e 143libnsl_hidden_def (nis_local_principal)
e61abf83
UD
144
145nis_name
146nis_local_host (void)
147{
148 static char __nishostname[NIS_MAXNAMELEN + 1];
e61abf83
UD
149
150 if (__nishostname[0] == '\0')
151 {
e61abf83 152 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
3e5f5557
UD
153 __nishostname[0] = '\0';
154 else
155 {
616d9133 156 char *cp = rawmemchr (__nishostname, '\0');
714a562f 157 int len = cp - __nishostname;
e61abf83 158
3e5f5557 159 /* Hostname already fully qualified? */
714a562f 160 if (cp[-1] == '.')
3e5f5557 161 return __nishostname;
e61abf83 162
714a562f 163 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
3e5f5557
UD
164 {
165 __nishostname[0] = '\0';
166 return __nishostname;
167 }
e61abf83 168
3e5f5557
UD
169 *cp++ = '.';
170 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
171 __nishostname[NIS_MAXNAMELEN] = '\0';
e61abf83 172 }
e61abf83
UD
173 }
174
175 return __nishostname;
176}
7440c23e 177libnsl_hidden_def (nis_local_host)