enhancements
[samba.git] / source / sam / idmap_winbind.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap Winbind backend
5
6    Copyright (C) Simo Sorce 2003
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "nsswitch/winbind_nss.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 extern DOM_SID global_sid_NULL;                         /* NULL sid */
30
31 NSS_STATUS winbindd_request(int req_type,
32                                  struct winbindd_request *request,
33                                  struct winbindd_response *response);
34
35 /* Get a sid from an id */
36 static NTSTATUS db_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
37 {
38         struct winbindd_request request;
39         struct winbindd_response response;
40         int result, operation;
41
42         ZERO_STRUCT(request);
43         ZERO_STRUCT(response);
44
45         switch (id_type & ID_TYPEMASK) {
46                 case ID_USERID:
47                         request.data.uid = id.uid;
48                         operation = WINBINDD_UID_TO_SID;
49                         break;
50                 case ID_GROUPID:
51                         request.data.gid = id.gid;
52                         operation = WINBINDD_GID_TO_SID;
53                         break;
54                 default:
55                         return NT_STATUS_INVALID_PARAMETER;
56         }
57
58         /* Make The Request */
59         result = winbindd_request(operation, &request, &response);
60         if (result == NSS_STATUS_SUCCESS) {
61                 if (!string_to_sid(sid, response.data.sid.sid)) {
62                         return NT_STATUS_INVALID_SID;
63                 }
64                 return NT_STATUS_OK;
65         } else {
66                 sid_copy(sid, &global_sid_NULL);
67         }
68
69         return NT_STATUS_UNSUCCESSFUL;
70 }
71
72 /* Get an id from a sid */
73 static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
74 {
75         struct winbindd_request request;
76         struct winbindd_response response;
77         int result, operation;
78         fstring sid_str;
79
80         if (!id || !id_type) {
81                 return NT_STATUS_INVALID_PARAMETER;
82         }
83
84         /* setup request */
85
86         ZERO_STRUCT(request);
87         ZERO_STRUCT(response);
88
89         switch (*id_type & ID_TYPEMASK) {
90                 case ID_USERID:
91                         operation = WINBINDD_SID_TO_UID;
92                         break;
93                 case ID_GROUPID:
94                         operation = WINBINDD_SID_TO_GID;
95                         break;
96                 default:
97                         return NT_STATUS_INVALID_PARAMETER;
98         }
99
100         sid_to_string(sid_str, sid);
101         fstrcpy(request.data.sid, sid_str);
102
103         /* Make The Request */
104         result = winbindd_request(operation, &request, &response);
105
106         if (result == NSS_STATUS_SUCCESS) {
107                 if (operation == WINBINDD_SID_TO_UID) {
108                         (*id).uid = response.data.uid;
109                 } else {
110                         (*id).gid = response.data.gid;
111                 }
112                 return NT_STATUS_OK;
113         }
114
115         return NT_STATUS_UNSUCCESSFUL;
116 }       
117
118 static NTSTATUS db_set_mapping(DOM_SID *sid, unid_t id, int id_type) {
119         return NT_STATUS_UNSUCCESSFUL;
120 }
121
122 /*****************************************************************************
123  Initialise idmap database. 
124 *****************************************************************************/
125 static NTSTATUS db_init(void) {
126         return NT_STATUS_OK;
127 }
128
129 /* Close the tdb */
130 static NTSTATUS db_close(void) {
131         return NT_STATUS_OK;
132 }
133
134 static void db_status(void) {
135         return;
136 }
137
138 struct idmap_methods winbind_methods = {
139
140         db_init,
141         db_get_sid_from_id,
142         db_get_id_from_sid,
143         db_set_mapping,
144         db_close,
145         db_status
146
147 };
148
149 NTSTATUS idmap_reg_winbind(struct idmap_methods **meth)
150 {
151         *meth = &winbind_methods;
152
153         return NT_STATUS_OK;
154 }
155