1 /* Copyright (C) 1997 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 <libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
= NULL
;
34 static nis_name
*names
= NULL
;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 _nss_nisplus_parse_aliasent (nis_result
*result
, struct aliasent
*alias
,
44 char *buffer
, size_t buflen
)
49 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
50 result
->objects
.objects_len
!= 1 ||
51 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
52 strcmp(result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
53 "mail_aliases") != 0 ||
54 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 2)
58 char *first_unused
= buffer
+ NISENTRYLEN(0, 1, result
) + 1;
60 buflen
- (buflen
% __alignof__ (char *)) -
61 NISENTRYLEN(0, 1, result
) - 2;
65 if (NISENTRYLEN(0, 1, result
) >= buflen
)
67 /* The line is too long for our buffer. */
74 strncpy (buffer
, NISENTRYVAL(0, 1, result
), NISENTRYLEN(0, 1, result
));
75 buffer
[NISENTRYLEN(0, 1, result
)] = '\0';
78 if (NISENTRYLEN(0, 0, result
) >= room_left
)
81 alias
->alias_local
= 0;
82 alias
->alias_members_len
= 0;
85 strcpy (first_unused
, NISENTRYVAL(0, 0, result
));
86 first_unused
[NISENTRYLEN(0, 0, result
)] = '\0';
87 alias
->alias_name
= first_unused
;
89 /* Terminate the line for any case. */
90 cp
= strpbrk (alias
->alias_name
, "#\n");
94 first_unused
+= strlen (alias
->alias_name
) +1;
95 /* Adjust the pointer so it is aligned for
97 first_unused
+= __alignof__ (char *) - 1;
98 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
99 alias
->alias_members
= (char **) first_unused
;
103 while (*line
!= '\0')
105 /* Skip leading blanks. */
106 while (isspace (*line
))
112 if (room_left
< sizeof (char *))
114 room_left
-= sizeof (char *);
115 alias
->alias_members
[alias
->alias_members_len
] = line
;
117 while (*line
!= '\0' && *line
!= ',')
120 if (line
!= alias
->alias_members
[alias
->alias_members_len
])
124 alias
->alias_members_len
++;
128 return alias
->alias_members_len
== 0 ? 0 : 1;
133 _nss_nisplus_setaliasent (void)
135 __libc_lock_lock (lock
);
138 nis_freeresult (result
);
142 nis_freenames (names
);
146 __libc_lock_unlock (lock
);
148 return NSS_STATUS_SUCCESS
;
152 _nss_nisplus_endaliasent (void)
154 __libc_lock_lock (lock
);
157 nis_freeresult (result
);
161 nis_freenames (names
);
165 __libc_lock_unlock (lock
);
167 return NSS_STATUS_SUCCESS
;
170 static enum nss_status
171 internal_nisplus_getaliasent_r (struct aliasent
*alias
,
172 char *buffer
, size_t buflen
)
176 /* Get the next entry until we found a correct one. */
181 names
= nis_getnames("mail_aliases.org_dir");
182 if (names
== NULL
|| names
[0] == NULL
)
183 return NSS_STATUS_UNAVAIL
;
185 result
= nis_first_entry(names
[0]);
186 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
187 return niserr2nss (result
->status
);
193 res2
= nis_next_entry(names
[0], &result
->cookie
);
194 nis_freeresult (result
);
196 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
197 return niserr2nss (result
->status
);
200 parse_res
= _nss_nisplus_parse_aliasent (result
, alias
, buffer
, buflen
);
201 } while (!parse_res
);
203 return NSS_STATUS_SUCCESS
;
207 _nss_nisplus_getaliasent_r (struct aliasent
*result
, char *buffer
,
212 __libc_lock_lock (lock
);
214 status
= internal_nisplus_getaliasent_r (result
, buffer
, buflen
);
216 __libc_lock_unlock (lock
);
222 _nss_nisplus_getaliasbyname_r (const char *name
, struct aliasent
*alias
,
223 char *buffer
, size_t buflen
)
227 if (name
== NULL
|| strlen(name
) > 8)
228 return NSS_STATUS_NOTFOUND
;
232 char buf
[strlen (name
) + 30];
234 sprintf(buf
, "[name=%s],mail_aliases.org_dir", name
);
236 result
= nis_list(buf
, EXPAND_NAME
, NULL
, NULL
);
238 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
239 return niserr2nss (result
->status
);
241 parse_res
= _nss_nisplus_parse_aliasent (result
, alias
, buffer
, buflen
);
244 return NSS_STATUS_SUCCESS
;
246 if (!parse_res
&& errno
== ERANGE
)
247 return NSS_STATUS_TRYAGAIN
;
249 return NSS_STATUS_NOTFOUND
;