1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
;
34 static u_long next_entry
;
35 static nis_name tablename_val
;
36 static size_t tablename_len
;
38 #define NISENTRYVAL(idx, col, res) \
39 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
41 #define NISENTRYLEN(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44 static enum nss_status
45 _nss_create_tablename (int *errnop
)
47 if (tablename_val
== NULL
)
49 const char *local_dir
= nis_local_directory ();
50 size_t local_dir_len
= strlen (local_dir
);
51 static const char prefix
[] = "mail_aliases.org_dir.";
53 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
57 return NSS_STATUS_TRYAGAIN
;
60 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
62 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
64 atomic_write_barrier ();
69 return NSS_STATUS_SUCCESS
;
73 _nss_nisplus_parse_aliasent (nis_result
*result
, unsigned long entry
,
74 struct aliasent
*alias
, char *buffer
,
75 size_t buflen
, int *errnop
)
80 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
81 || __type_of (&NIS_RES_OBJECT (result
)[entry
]) != NIS_ENTRY_OBJ
82 || strcmp (NIS_RES_OBJECT (result
)[entry
].EN_data
.en_type
,
84 || NIS_RES_OBJECT (result
)[entry
].EN_data
.en_cols
.en_cols_len
< 2)
87 if (NISENTRYLEN (entry
, 1, result
) >= buflen
)
89 /* The line is too long for our buffer. */
95 char *cp
= __stpncpy (buffer
, NISENTRYVAL (entry
, 1, result
),
96 NISENTRYLEN (entry
, 1, result
));
99 char *first_unused
= cp
+ 1;
100 size_t room_left
= buflen
- (first_unused
- buffer
);
102 alias
->alias_local
= 0;
103 alias
->alias_members_len
= 0;
105 if (NISENTRYLEN (entry
, 0, result
) >= room_left
)
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 size_t len
= strlen (alias
->alias_name
) + 1;
122 /* Adjust the pointer so it is aligned for
124 size_t adjust
= ((__alignof__ (char *)
125 - (first_unused
- (char *) 0) % __alignof__ (char *))
126 % __alignof__ (char *));
127 if (room_left
< adjust
)
129 first_unused
+= adjust
;
132 alias
->alias_members
= (char **) first_unused
;
135 while (*line
!= '\0')
137 /* Skip leading blanks. */
138 while (isspace (*line
))
144 if (room_left
< sizeof (char *))
146 room_left
-= sizeof (char *);
147 alias
->alias_members
[alias
->alias_members_len
] = line
;
149 while (*line
!= '\0' && *line
!= ',')
152 if (line
!= alias
->alias_members
[alias
->alias_members_len
])
155 ++alias
->alias_members_len
;
157 else if (*line
== ',')
161 return alias
->alias_members_len
== 0 ? 0 : 1;
164 static enum nss_status
165 internal_setaliasent (void)
167 enum nss_status status
;
172 nis_freeresult (result
);
176 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
177 return NSS_STATUS_UNAVAIL
;
180 result
= nis_list (tablename_val
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
183 status
= NSS_STATUS_TRYAGAIN
;
184 __set_errno (ENOMEM
);
188 status
= niserr2nss (result
->status
);
189 if (status
!= NSS_STATUS_SUCCESS
)
191 nis_freeresult (result
);
199 _nss_nisplus_setaliasent (void)
201 enum nss_status status
;
203 __libc_lock_lock (lock
);
205 status
= internal_setaliasent ();
207 __libc_lock_unlock (lock
);
213 _nss_nisplus_endaliasent (void)
215 __libc_lock_lock (lock
);
219 nis_freeresult (result
);
224 __libc_lock_unlock (lock
);
226 return NSS_STATUS_SUCCESS
;
229 static enum nss_status
230 internal_nisplus_getaliasent_r (struct aliasent
*alias
,
231 char *buffer
, size_t buflen
, int *errnop
)
237 enum nss_status status
;
239 status
= internal_setaliasent ();
240 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
244 /* Get the next entry until we found a correct one. */
247 if (next_entry
>= result
->objects
.objects_len
)
248 return NSS_STATUS_NOTFOUND
;
250 parse_res
= _nss_nisplus_parse_aliasent (result
, next_entry
, alias
,
251 buffer
, buflen
, errnop
);
253 return NSS_STATUS_TRYAGAIN
;
259 return NSS_STATUS_SUCCESS
;
263 _nss_nisplus_getaliasent_r (struct aliasent
*result
, char *buffer
,
264 size_t buflen
, int *errnop
)
268 __libc_lock_lock (lock
);
270 status
= internal_nisplus_getaliasent_r (result
, buffer
, buflen
, errnop
);
272 __libc_lock_unlock (lock
);
278 _nss_nisplus_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
279 char *buffer
, size_t buflen
, int *errnop
)
283 if (tablename_val
== NULL
)
285 __libc_lock_lock (lock
);
287 enum nss_status status
= _nss_create_tablename (errnop
);
289 __libc_lock_unlock (lock
);
291 if (status
!= NSS_STATUS_SUCCESS
)
298 return NSS_STATUS_UNAVAIL
;
301 char buf
[strlen (name
) + 9 + tablename_len
];
304 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
306 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
311 return NSS_STATUS_TRYAGAIN
;
314 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
316 enum nss_status status
= niserr2nss (result
->status
);
317 nis_freeresult (result
);
321 parse_res
= _nss_nisplus_parse_aliasent (result
, 0, alias
,
322 buffer
, buflen
, errnop
);
324 /* We do not need the lookup result anymore. */
325 nis_freeresult (result
);
327 if (__builtin_expect (parse_res
< 1, 0))
329 __set_errno (olderr
);
332 return NSS_STATUS_TRYAGAIN
;
334 return NSS_STATUS_NOTFOUND
;
337 return NSS_STATUS_SUCCESS
;