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