]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_local_names.c
Update.
[thirdparty/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 <rpcsvc/nis.h>
24
25 nis_name
26 nis_local_group (void)
27 {
28 static char __nisgroup[NIS_MAXNAMELEN + 1];
29
30 if (__nisgroup[0] == '\0')
31 {
32 char *cptr;
33 char *cp;
34
35 if ((cptr = getenv ("NIS_GROUP")) == NULL)
36 return __nisgroup;
37
38 if (strlen (cptr) >= NIS_MAXNAMELEN)
39 return __nisgroup;
40
41 cp = stpcpy (__nisgroup, cptr);
42
43 if (cp[-1] != '.')
44 {
45 cptr = nis_local_directory ();
46 if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
47 {
48 *cp++ = '.';
49 strcpy (cp, cptr);
50 }
51 else
52 __nisgroup[0] = '\0';
53 }
54 }
55
56 return __nisgroup;
57 }
58
59
60 nis_name
61 nis_local_directory (void)
62 {
63 static char __nisdomainname[NIS_MAXNAMELEN + 1];
64
65 if (__nisdomainname[0] == '\0')
66 {
67 if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
68 __nisdomainname[0] = '\0';
69 else
70 {
71 char *cp = strchr (__nisdomainname, '\0');
72
73 /* Missing trailing dot? */
74 if (cp[-1] != '.')
75 {
76 *cp++ = '.';
77 *cp = '\0';
78 }
79 }
80 }
81
82 return __nisdomainname;
83 }
84
85 nis_name
86 nis_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 {
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");
105
106 if (buf[len - 1] != '.')
107 {
108 buf[len++] = '.';
109 buf[len] = '\0';
110 }
111
112 res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
113 FOLLOW_PATH, NULL, NULL);
114
115 if (res == NULL)
116 return strcpy (__principal, "nobody");
117
118 if (NIS_RES_STATUS (res) == NIS_SUCCESS)
119 {
120 if (res->objects.objects_len > 1)
121 {
122 /* More than one principal with same uid? something
123 wrong with cred table. Should be unique. Warn user
124 and continue. */
125 printf (_("\
126 LOCAL 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);
136 return strcpy (__principal, "nobody");
137 }
138 }
139 else
140 return strcpy (__principal, nis_local_host ());
141
142 /* Should be never reached */
143 return strcpy (__principal, "nobody");
144 }
145 return __principal;
146 }
147
148 nis_name
149 nis_local_host (void)
150 {
151 static char __nishostname[NIS_MAXNAMELEN + 1];
152
153 if (__nishostname[0] == '\0')
154 {
155 if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
156 __nishostname[0] = '\0';
157 else
158 {
159 char *cp = strchr (__nishostname, '\0');
160 int len = cp - __nishostname;
161
162 /* Hostname already fully qualified? */
163 if (cp[-1] == '.')
164 return __nishostname;
165
166 if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
167 {
168 __nishostname[0] = '\0';
169 return __nishostname;
170 }
171
172 *cp++ = '.';
173 strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
174 __nishostname[NIS_MAXNAMELEN] = '\0';
175 }
176 }
177
178 return __nishostname;
179 }