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.
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.
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.
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. */
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
= NULL
;
33 static u_long next_entry
= 0;
34 static nis_name tablename_val
= NULL
;
35 static u_long tablename_len
= 0;
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 static enum nss_status
44 _nss_create_tablename (int *errnop
)
46 if (tablename_val
== NULL
)
48 char buf
[40 + strlen (nis_local_directory ())];
51 p
= __stpcpy (buf
, "mail_aliases.org_dir.");
52 p
= __stpcpy (p
, nis_local_directory ());
53 tablename_val
= __strdup (buf
);
54 if (tablename_val
== NULL
)
57 return NSS_STATUS_TRYAGAIN
;
59 tablename_len
= strlen (tablename_val
);
61 return NSS_STATUS_SUCCESS
;
65 _nss_nisplus_parse_aliasent (nis_result
*result
, unsigned long entry
,
66 struct aliasent
*alias
, char *buffer
,
67 size_t buflen
, int *errnop
)
72 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
73 || __type_of (&result
->objects
.objects_val
[entry
]) != NIS_ENTRY_OBJ
74 || strcmp (result
->objects
.objects_val
[entry
].EN_data
.en_type
,
76 || result
->objects
.objects_val
[entry
].EN_data
.en_cols
.en_cols_len
< 2)
80 char *first_unused
= buffer
+ NISENTRYLEN (0, 1, result
) + 1;
82 buflen
- (buflen
% __alignof__ (char *)) -
83 NISENTRYLEN (0, 1, result
) - 2;
87 if (NISENTRYLEN (entry
, 1, result
) >= buflen
)
89 /* The line is too long for our buffer. */
96 cp
= __stpncpy (buffer
, NISENTRYVAL (entry
, 1, result
),
97 NISENTRYLEN (entry
, 1, result
));
101 if (NISENTRYLEN(entry
, 0, result
) >= room_left
)
104 alias
->alias_local
= 0;
105 alias
->alias_members_len
= 0;
106 *first_unused
= '\0';
108 cp
= __stpncpy (first_unused
, NISENTRYVAL (entry
, 0, result
),
109 NISENTRYLEN (entry
, 0, result
));
111 alias
->alias_name
= first_unused
;
113 /* Terminate the line for any case. */
114 cp
= strpbrk (alias
->alias_name
, "#\n");
118 first_unused
+= strlen (alias
->alias_name
) +1;
119 /* Adjust the pointer so it is aligned for
121 first_unused
+= __alignof__ (char *) - 1;
122 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
123 alias
->alias_members
= (char **) first_unused
;
127 while (*line
!= '\0')
129 /* Skip leading blanks. */
130 while (isspace (*line
))
136 if (room_left
< sizeof (char *))
138 room_left
-= sizeof (char *);
139 alias
->alias_members
[alias
->alias_members_len
] = line
;
141 while (*line
!= '\0' && *line
!= ',')
144 if (line
!= alias
->alias_members
[alias
->alias_members_len
])
147 alias
->alias_members_len
++;
151 return alias
->alias_members_len
== 0 ? 0 : 1;
155 static enum nss_status
156 internal_setaliasent (void)
158 enum nss_status status
;
162 nis_freeresult (result
);
165 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
166 return NSS_STATUS_UNAVAIL
;
169 result
= nis_list (tablename_val
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
170 status
= niserr2nss (result
->status
);
171 if (status
!= NSS_STATUS_SUCCESS
)
173 nis_freeresult (result
);
180 _nss_nisplus_setaliasent (void)
182 enum nss_status status
;
184 __libc_lock_lock (lock
);
186 status
= internal_setaliasent ();
188 __libc_lock_unlock (lock
);
194 _nss_nisplus_endaliasent (void)
196 __libc_lock_lock (lock
);
199 nis_freeresult (result
);
203 __libc_lock_unlock (lock
);
205 return NSS_STATUS_SUCCESS
;
208 static enum nss_status
209 internal_nisplus_getaliasent_r (struct aliasent
*alias
,
210 char *buffer
, size_t buflen
, int *errnop
)
216 enum nss_status status
;
218 status
= internal_setaliasent ();
219 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
223 /* Get the next entry until we found a correct one. */
226 if (next_entry
>= result
->objects
.objects_len
)
227 return NSS_STATUS_NOTFOUND
;
229 parse_res
= _nss_nisplus_parse_aliasent (result
, next_entry
, alias
,
230 buffer
, buflen
, errnop
);
232 return NSS_STATUS_TRYAGAIN
;
235 } while (!parse_res
);
237 return NSS_STATUS_SUCCESS
;
241 _nss_nisplus_getaliasent_r (struct aliasent
*result
, char *buffer
,
242 size_t buflen
, int *errnop
)
246 __libc_lock_lock (lock
);
248 status
= internal_nisplus_getaliasent_r (result
, buffer
, buflen
, errnop
);
250 __libc_lock_unlock (lock
);
256 _nss_nisplus_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
257 char *buffer
, size_t buflen
, int *errnop
)
261 if (tablename_val
== NULL
)
263 enum nss_status status
= _nss_create_tablename (errnop
);
264 if (status
!= NSS_STATUS_SUCCESS
)
271 return NSS_STATUS_UNAVAIL
;
276 char buf
[strlen (name
) + 30 + tablename_len
];
278 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
280 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
282 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
283 return niserr2nss (result
->status
);
285 parse_res
= _nss_nisplus_parse_aliasent (result
, 0, alias
,
286 buffer
, buflen
, errnop
);
290 return NSS_STATUS_TRYAGAIN
;
292 return NSS_STATUS_NOTFOUND
;
294 return NSS_STATUS_SUCCESS
;