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