2 Unix SMB/CIFS implementation.
6 Copyright (C) Simo Sorce 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/passwd.h"
25 #include "nsswitch/winbind_client.h"
27 #include "lib/winbind_util.h"
30 #define DBGC_CLASS DBGC_IDMAP
32 /*****************************
33 Initialise idmap database.
34 *****************************/
36 static NTSTATUS
idmap_nss_int_init(struct idmap_domain
*dom
)
41 /**********************************
42 lookup a set of unix ids.
43 **********************************/
45 static NTSTATUS
idmap_nss_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
49 /* initialize the status to avoid suprise */
50 for (i
= 0; ids
[i
]; i
++) {
51 ids
[i
]->status
= ID_UNKNOWN
;
54 for (i
= 0; ids
[i
]; i
++) {
58 enum lsa_SidType type
;
61 switch (ids
[i
]->xid
.type
) {
63 pw
= getpwuid((uid_t
)ids
[i
]->xid
.id
);
66 ids
[i
]->status
= ID_UNMAPPED
;
72 gr
= getgrgid((gid_t
)ids
[i
]->xid
.id
);
75 ids
[i
]->status
= ID_UNMAPPED
;
81 ids
[i
]->status
= ID_UNKNOWN
;
85 /* by default calls to winbindd are disabled
86 the following call will not recurse so this is safe */
88 /* Lookup name from PDC using lsa_lookup_names() */
89 ret
= winbind_lookup_name(dom
->name
, name
, ids
[i
]->sid
, &type
);
93 /* TODO: how do we know if the name is really not mapped,
94 * or something just failed ? */
95 ids
[i
]->status
= ID_UNMAPPED
;
101 if (ids
[i
]->xid
.type
== ID_TYPE_UID
) {
102 ids
[i
]->status
= ID_MAPPED
;
106 case SID_NAME_DOM_GRP
:
108 case SID_NAME_WKN_GRP
:
109 if (ids
[i
]->xid
.type
== ID_TYPE_GID
) {
110 ids
[i
]->status
= ID_MAPPED
;
115 ids
[i
]->status
= ID_UNKNOWN
;
122 /**********************************
123 lookup a set of sids.
124 **********************************/
126 static NTSTATUS
idmap_nss_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
130 /* initialize the status to avoid suprise */
131 for (i
= 0; ids
[i
]; i
++) {
132 ids
[i
]->status
= ID_UNKNOWN
;
135 for (i
= 0; ids
[i
]; i
++) {
137 enum lsa_SidType type
;
141 /* by default calls to winbindd are disabled
142 the following call will not recurse so this is safe */
144 ret
= winbind_lookup_sid(talloc_tos(), ids
[i
]->sid
, NULL
,
145 (const char **)&name
, &type
);
149 /* TODO: how do we know if the name is really not mapped,
150 * or something just failed ? */
151 ids
[i
]->status
= ID_UNMAPPED
;
156 case SID_NAME_USER
: {
159 /* this will find also all lower case name and use username level */
161 pw
= Get_Pwnam_alloc(talloc_tos(), name
);
163 ids
[i
]->xid
.id
= pw
->pw_uid
;
164 ids
[i
]->xid
.type
= ID_TYPE_UID
;
165 ids
[i
]->status
= ID_MAPPED
;
171 case SID_NAME_DOM_GRP
:
173 case SID_NAME_WKN_GRP
:
177 ids
[i
]->xid
.id
= gr
->gr_gid
;
178 ids
[i
]->xid
.type
= ID_TYPE_GID
;
179 ids
[i
]->status
= ID_MAPPED
;
184 ids
[i
]->status
= ID_UNKNOWN
;
192 /**********************************
193 Close the idmap tdb instance
194 **********************************/
196 static struct idmap_methods nss_methods
= {
198 .init
= idmap_nss_int_init
,
199 .unixids_to_sids
= idmap_nss_unixids_to_sids
,
200 .sids_to_unixids
= idmap_nss_sids_to_unixids
,
203 NTSTATUS
idmap_nss_init(void)
205 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "nss", &nss_methods
);