]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_netgroup.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
34 #include "lu_overrides.h"
43 #define ENTRY_SIZE sizeof(struct li_netgrent)
44 #define ENTRY_KEY _li_data_key_netgroup
46 static struct li_netgrent
*
47 copy_netgroup(struct li_netgrent
*in
)
49 if (in
== NULL
) return NULL
;
51 return (struct li_netgrent
*)LI_ils_create("sss", in
->ng_host
, in
->ng_user
, in
->ng_domain
);
55 * Extract the next netgroup entry from a kvarray.
57 static struct li_netgrent
*
58 extract_netgroup(kvarray_t
*in
)
60 struct li_netgrent tmp
;
61 uint32_t d
, k
, kcount
;
63 if (in
== NULL
) return NULL
;
68 if (d
>= in
->count
) return NULL
;
70 memset(&tmp
, 0, ENTRY_SIZE
);
72 kcount
= in
->dict
[d
].kcount
;
74 for (k
= 0; k
< kcount
; k
++)
76 if (!strcmp(in
->dict
[d
].key
[k
], "user"))
78 if (tmp
.ng_user
!= NULL
) continue;
79 if (in
->dict
[d
].vcount
[k
] == 0) continue;
81 tmp
.ng_user
= (char *)in
->dict
[d
].val
[k
][0];
83 else if (!strcmp(in
->dict
[d
].key
[k
], "host"))
85 if (tmp
.ng_host
!= NULL
) continue;
86 if (in
->dict
[d
].vcount
[k
] == 0) continue;
88 tmp
.ng_host
= (char *)in
->dict
[d
].val
[k
][0];
90 else if (!strcmp(in
->dict
[d
].key
[k
], "domain"))
92 if (tmp
.ng_domain
!= NULL
) continue;
93 if (in
->dict
[d
].vcount
[k
] == 0) continue;
95 tmp
.ng_domain
= (char *)in
->dict
[d
].val
[k
][0];
99 if (tmp
.ng_user
== NULL
) tmp
.ng_user
= "";
100 if (tmp
.ng_host
== NULL
) tmp
.ng_host
= "";
101 if (tmp
.ng_domain
== NULL
) tmp
.ng_domain
= "";
103 return copy_netgroup(&tmp
);
107 check_innetgr(kvarray_t
*in
)
109 uint32_t d
, k
, kcount
;
111 if (in
== NULL
) return 0;
114 if (d
>= in
->count
) return 0;
116 kcount
= in
->dict
[d
].kcount
;
118 for (k
= 0; k
< kcount
; k
++)
120 if (!strcmp(in
->dict
[d
].key
[k
], "result"))
122 if (in
->dict
[d
].vcount
[k
] == 0) continue;
123 return atoi(in
->dict
[d
].val
[k
][0]);
131 ds_innetgr(const char *group
, const char *host
, const char *user
, const char *domain
)
136 kern_return_t status
;
137 static int proc
= -1;
141 status
= LI_DSLookupGetProcedureNumber("innetgr", &proc
);
142 if (status
!= KERN_SUCCESS
) return 0;
146 if (group
== NULL
) group
= "";
147 if (host
== NULL
) host
= "";
148 if (user
== NULL
) user
= "";
149 if (domain
== NULL
) domain
= "";
151 request
= kvbuf_query("ksksksks", "netgroup", group
, "host", host
, "user", user
, "domain", domain
);
152 if (request
== NULL
) return 0;
155 status
= LI_DSLookupQuery(proc
, request
, &reply
);
158 if (status
!= KERN_SUCCESS
) return 0;
160 is_innetgr
= check_innetgr(reply
);
169 LI_data_free_kvarray(LI_data_find_key(ENTRY_KEY
));
173 * This is different than the other setXXXent routines
174 * since this is really more like getnetgrbyname() than
178 ds_setnetgrent(const char *name
)
180 struct li_thread_info
*tdata
;
183 kern_return_t status
;
184 static int proc
= -1;
186 tdata
= LI_data_create_key(ENTRY_KEY
, ENTRY_SIZE
);
187 if (tdata
== NULL
) return;
189 if (tdata
->li_vm
!= NULL
) return;
193 status
= LI_DSLookupGetProcedureNumber("getnetgrent", &proc
);
194 if (status
!= KERN_SUCCESS
)
196 LI_data_free_kvarray(tdata
);
201 request
= kvbuf_query_key_val("netgroup", name
);
202 if (request
== NULL
) return;
205 status
= LI_DSLookupQuery(proc
, request
, &reply
);
208 if (status
!= KERN_SUCCESS
)
210 LI_data_free_kvarray(tdata
);
214 tdata
->li_vm
= (char *)reply
;
218 static struct li_netgrent
*
221 struct li_netgrent
*entry
;
222 struct li_thread_info
*tdata
;
224 tdata
= LI_data_create_key(ENTRY_KEY
, ENTRY_SIZE
);
225 if (tdata
== NULL
) return NULL
;
227 entry
= extract_netgroup((kvarray_t
*)(tdata
->li_vm
));
238 innetgr(const char *group
, const char *host
, const char *user
,
241 if (_ds_running()) return (ds_innetgr(group
, host
, user
, domain
));
246 getnetgrent(char **host
, char **user
, char **domain
)
248 struct li_netgrent
*res
= NULL
;
249 struct li_thread_info
*tdata
;
251 tdata
= LI_data_create_key(ENTRY_KEY
, ENTRY_SIZE
);
252 if (tdata
== NULL
) return 0;
255 if (_ds_running()) res
= ds_getnetgrent();
257 LI_data_recycle(tdata
, res
, ENTRY_SIZE
);
258 if (res
== NULL
) return 0;
260 if (host
!= NULL
) *host
= res
->ng_host
;
261 if (user
!= NULL
) *user
= res
->ng_user
;
262 if (domain
!= NULL
) *domain
= res
->ng_domain
;
268 setnetgrent(const char *name
)
270 if (_ds_running()) ds_setnetgrent(name
);
276 if (_ds_running()) ds_endnetgrent();