c091cd7f53ac7c252973b5a9103522ced7ae9e82
[kai/samba.git] / source3 / winbindd / winbindd_sid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - sid related functions
5
6    Copyright (C) Tim Potter 2000
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "winbindd.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
27
28 /* Convert a string  */
29
30 static void lookupsid_recv(void *private_data, bool success,
31                            const char *dom_name, const char *name,
32                            enum lsa_SidType type);
33
34 void winbindd_lookupsid(struct winbindd_cli_state *state)
35 {
36         DOM_SID sid;
37
38         /* Ensure null termination */
39         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
40
41         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid, 
42                   state->request->data.sid));
43
44         if (!string_to_sid(&sid, state->request->data.sid)) {
45                 DEBUG(5, ("%s not a SID\n", state->request->data.sid));
46                 request_error(state);
47                 return;
48         }
49
50         winbindd_lookupsid_async(state->mem_ctx, &sid, lookupsid_recv, state);
51 }
52
53 static void lookupsid_recv(void *private_data, bool success,
54                            const char *dom_name, const char *name,
55                            enum lsa_SidType type)
56 {
57         struct winbindd_cli_state *state =
58                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
59
60         if (!success) {
61                 DEBUG(5, ("lookupsid returned an error\n"));
62                 request_error(state);
63                 return;
64         }
65
66         fstrcpy(state->response->data.name.dom_name, dom_name);
67         fstrcpy(state->response->data.name.name, name);
68         state->response->data.name.type = type;
69         request_ok(state);
70 }
71
72 /**
73  * Look up the SID for a qualified name.  
74  **/
75
76 static void lookupname_recv(void *private_data, bool success,
77                             const DOM_SID *sid, enum lsa_SidType type);
78
79 void winbindd_lookupname(struct winbindd_cli_state *state)
80 {
81         char *name_domain, *name_user;
82         char *p;
83
84         /* Ensure null termination */
85         state->request->data.name.dom_name[sizeof(state->request->data.name.dom_name)-1]='\0';
86
87         /* Ensure null termination */
88         state->request->data.name.name[sizeof(state->request->data.name.name)-1]='\0';
89
90         /* cope with the name being a fully qualified name */
91         p = strstr(state->request->data.name.name, lp_winbind_separator());
92         if (p) {
93                 *p = 0;
94                 name_domain = state->request->data.name.name;
95                 name_user = p+1;
96         } else {
97                 name_domain = state->request->data.name.dom_name;
98                 name_user = state->request->data.name.name;
99         }
100
101         DEBUG(3, ("[%5lu]: lookupname %s%s%s\n", (unsigned long)state->pid,
102                   name_domain, lp_winbind_separator(), name_user));
103
104         winbindd_lookupname_async(state->mem_ctx, name_domain, name_user,
105                                   lookupname_recv, WINBINDD_LOOKUPNAME, 
106                                   state);
107 }
108
109 static void lookupname_recv(void *private_data, bool success,
110                             const DOM_SID *sid, enum lsa_SidType type)
111 {
112         struct winbindd_cli_state *state =
113                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
114
115         if (!success) {
116                 DEBUG(5, ("lookupname returned an error\n"));
117                 request_error(state);
118                 return;
119         }
120
121         sid_to_fstring(state->response->data.sid.sid, sid);
122         state->response->data.sid.type = type;
123         request_ok(state);
124         return;
125 }
126
127 void winbindd_lookuprids(struct winbindd_cli_state *state)
128 {
129         struct winbindd_domain *domain;
130         DOM_SID domain_sid;
131         
132         /* Ensure null termination */
133         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
134
135         DEBUG(10, ("lookup_rids: %s\n", state->request->data.sid));
136
137         if (!string_to_sid(&domain_sid, state->request->data.sid)) {
138                 DEBUG(5, ("Could not convert %s to SID\n",
139                           state->request->data.sid));
140                 request_error(state);
141                 return;
142         }
143
144         domain = find_lookup_domain_from_sid(&domain_sid);
145         if (domain == NULL) {
146                 DEBUG(10, ("Could not find domain for name %s\n",
147                            state->request->domain_name));
148                 request_error(state);
149                 return;
150         }
151
152         sendto_domain(state, domain);
153 }
154
155 /* Convert a sid to a uid.  We assume we only have one rid attached to the
156    sid. */
157
158 static void sid2uid_recv(void *private_data, bool success, uid_t uid)
159 {
160         struct winbindd_cli_state *state =
161                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
162         struct dom_sid sid;
163
164         string_to_sid(&sid, state->request->data.sid);
165
166         if (!success) {
167                 DEBUG(5, ("Could not convert sid %s\n",
168                           state->request->data.sid));
169                 request_error(state);
170                 return;
171         }
172
173         state->response->data.uid = uid;
174         request_ok(state);
175 }
176
177 static void sid2uid_lookupsid_recv( void *private_data, bool success, 
178                                     const char *domain_name, 
179                                     const char *name, 
180                                     enum lsa_SidType type)
181 {
182         struct winbindd_cli_state *state =
183                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
184         DOM_SID sid;
185
186         if (!string_to_sid(&sid, state->request->data.sid)) {
187                 DEBUG(1, ("sid2uid_lookupsid_recv: Could not get convert sid "
188                           "%s from string\n", state->request->data.sid));
189                 request_error(state);
190                 return;
191         }
192
193         if (!success) {
194                 DEBUG(5, ("sid2uid_lookupsid_recv Could not convert get sid type for %s\n",
195                           state->request->data.sid));
196                 goto fail;
197         }
198
199         if ( (type!=SID_NAME_USER) && (type!=SID_NAME_COMPUTER) ) {
200                 DEBUG(5,("sid2uid_lookupsid_recv: Sid %s is not a user or a computer.\n", 
201                          state->request->data.sid));
202                 goto fail;
203         }
204
205         /* always use the async interface (may block) */
206         winbindd_sid2uid_async(state->mem_ctx, &sid, sid2uid_recv, state);
207         return;
208
209  fail:
210         /*
211          * We have to set the cache ourselves here, the child which is
212          * normally responsible was not queried yet.
213          */
214         idmap_cache_set_sid2uid(&sid, -1);
215         request_error(state);
216         return;
217 }
218
219 void winbindd_sid_to_uid(struct winbindd_cli_state *state)
220 {
221         DOM_SID sid;
222         uid_t uid;
223         bool expired;
224
225         /* Ensure null termination */
226         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
227
228         DEBUG(3, ("[%5lu]: sid to uid %s\n", (unsigned long)state->pid,
229                   state->request->data.sid));
230
231         if (!string_to_sid(&sid, state->request->data.sid)) {
232                 DEBUG(1, ("Could not get convert sid %s from string\n",
233                           state->request->data.sid));
234                 request_error(state);
235                 return;
236         }
237
238         if (idmap_cache_find_sid2uid(&sid, &uid, &expired)) {
239                 DEBUG(10, ("idmap_cache_find_sid2uid found %d%s\n",
240                            (int)uid, expired ? " (expired)": ""));
241                 if (expired && IS_DOMAIN_ONLINE(find_our_domain())) {
242                         DEBUG(10, ("revalidating expired entry\n"));
243                         goto backend;
244                 }
245                 if (uid == -1) {
246                         DEBUG(10, ("Returning negative cache entry\n"));
247                         request_error(state);
248                         return;
249                 }
250                 DEBUG(10, ("Returning positive cache entry\n"));
251                 state->response->data.uid = uid;
252                 request_ok(state);
253                 return;
254         }
255
256         /* Validate the SID as a user.  Hopefully this will hit cache.
257            Needed to prevent DoS by exhausting the uid allocation
258            range from random SIDs. */
259
260  backend:
261         winbindd_lookupsid_async( state->mem_ctx, &sid, sid2uid_lookupsid_recv, state );
262 }
263
264 /* Convert a sid to a gid.  We assume we only have one rid attached to the
265    sid.*/
266
267 static void sid2gid_recv(void *private_data, bool success, gid_t gid)
268 {
269         struct winbindd_cli_state *state =
270                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
271         struct dom_sid sid;
272
273         string_to_sid(&sid, state->request->data.sid);
274
275         if (!success) {
276                 DEBUG(5, ("Could not convert sid %s\n",
277                           state->request->data.sid));
278                 request_error(state);
279                 return;
280         }
281
282         state->response->data.gid = gid;
283         request_ok(state);
284 }
285
286 static void sid2gid_lookupsid_recv( void *private_data, bool success, 
287                                     const char *domain_name, 
288                                     const char *name, 
289                                     enum lsa_SidType type)
290 {
291         struct winbindd_cli_state *state =
292                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
293         DOM_SID sid;
294
295         if (!string_to_sid(&sid, state->request->data.sid)) {
296                 DEBUG(1, ("sid2gid_lookupsid_recv: Could not get convert sid "
297                           "%s from string\n", state->request->data.sid));
298                 request_error(state);
299                 return;
300         }
301
302         if (!success) {
303                 DEBUG(5, ("sid2gid_lookupsid_recv: Could not get sid type for %s\n",
304                           state->request->data.sid));
305                 goto fail;
306         }
307
308         if ( (type!=SID_NAME_DOM_GRP) &&
309              (type!=SID_NAME_ALIAS) && 
310              (type!=SID_NAME_WKN_GRP) ) 
311         {
312                 DEBUG(5,("sid2gid_lookupsid_recv: Sid %s is not a group.\n", 
313                          state->request->data.sid));
314                 goto fail;
315         }
316
317         /* always use the async interface (may block) */
318         winbindd_sid2gid_async(state->mem_ctx, &sid, sid2gid_recv, state);
319         return;
320
321  fail:
322         /*
323          * We have to set the cache ourselves here, the child which is
324          * normally responsible was not queried yet.
325          */
326         idmap_cache_set_sid2gid(&sid, -1);
327         request_error(state);
328         return;
329 }
330
331 void winbindd_sid_to_gid(struct winbindd_cli_state *state)
332 {
333         DOM_SID sid;
334         gid_t gid;
335         bool expired;
336
337         /* Ensure null termination */
338         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
339
340         DEBUG(3, ("[%5lu]: sid to gid %s\n", (unsigned long)state->pid,
341                   state->request->data.sid));
342
343         if (!string_to_sid(&sid, state->request->data.sid)) {
344                 DEBUG(1, ("Could not get convert sid %s from string\n",
345                           state->request->data.sid));
346                 request_error(state);
347                 return;
348         }
349
350         if (idmap_cache_find_sid2gid(&sid, &gid, &expired)) {
351                 DEBUG(10, ("idmap_cache_find_sid2gid found %d%s\n",
352                            (int)gid, expired ? " (expired)": ""));
353                 if (expired && IS_DOMAIN_ONLINE(find_our_domain())) {
354                         DEBUG(10, ("revalidating expired entry\n"));
355                         goto backend;
356                 }
357                 if (gid == -1) {
358                         DEBUG(10, ("Returning negative cache entry\n"));
359                         request_error(state);
360                         return;
361                 }
362                 DEBUG(10, ("Returning positive cache entry\n"));
363                 state->response->data.gid = gid;
364                 request_ok(state);
365                 return;
366         }
367
368         /* Validate the SID as a group.  Hopefully this will hit cache.
369            Needed to prevent DoS by exhausting the uid allocation
370            range from random SIDs. */
371
372  backend:
373         winbindd_lookupsid_async( state->mem_ctx, &sid, sid2gid_lookupsid_recv,
374                                   state );
375 }
376
377 static void set_mapping_recv(void *private_data, bool success)
378 {
379         struct winbindd_cli_state *state =
380                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
381
382         if (!success) {
383                 DEBUG(5, ("Could not set sid mapping\n"));
384                 request_error(state);
385                 return;
386         }
387
388         request_ok(state);
389 }
390
391 void winbindd_set_mapping(struct winbindd_cli_state *state)
392 {
393         struct id_map map;
394         DOM_SID sid;
395
396         DEBUG(3, ("[%5lu]: set id map\n", (unsigned long)state->pid));
397
398         if ( ! state->privileged) {
399                 DEBUG(0, ("Only root is allowed to set mappings!\n"));
400                 request_error(state);
401                 return;
402         }
403
404         if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid)) {
405                 DEBUG(1, ("Could not get convert sid %s from string\n",
406                           state->request->data.sid));
407                 request_error(state);
408                 return;
409         }
410
411         map.sid = &sid;
412         map.xid.id = state->request->data.dual_idmapset.id;
413         map.xid.type = state->request->data.dual_idmapset.type;
414
415         winbindd_set_mapping_async(state->mem_ctx, &map,
416                         set_mapping_recv, state);
417 }
418
419 static void remove_mapping_recv(void *private_data, bool success)
420 {
421         struct winbindd_cli_state *state =
422                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
423
424         if (!success) {
425                 DEBUG(5, ("Could not remove sid mapping\n"));
426                 request_error(state);
427                 return;
428         }
429
430         request_ok(state);
431 }
432
433 void winbindd_remove_mapping(struct winbindd_cli_state *state)
434 {
435         struct id_map map;
436         DOM_SID sid;
437
438         DEBUG(3, ("[%5lu]: remove id map\n", (unsigned long)state->pid));
439
440         if ( ! state->privileged) {
441                 DEBUG(0, ("Only root is allowed to remove mappings!\n"));
442                 request_error(state);
443                 return;
444         }
445
446         if (!string_to_sid(&sid, state->request->data.dual_idmapset.sid)) {
447                 DEBUG(1, ("Could not get convert sid %s from string\n",
448                           state->request->data.sid));
449                 request_error(state);
450                 return;
451         }
452
453         map.sid = &sid;
454         map.xid.id = state->request->data.dual_idmapset.id;
455         map.xid.type = state->request->data.dual_idmapset.type;
456
457         winbindd_remove_mapping_async(state->mem_ctx, &map,
458                         remove_mapping_recv, state);
459 }
460
461 static void set_hwm_recv(void *private_data, bool success)
462 {
463         struct winbindd_cli_state *state =
464                 talloc_get_type_abort(private_data, struct winbindd_cli_state);
465
466         if (!success) {
467                 DEBUG(5, ("Could not set sid mapping\n"));
468                 request_error(state);
469                 return;
470         }
471
472         request_ok(state);
473 }
474
475 void winbindd_set_hwm(struct winbindd_cli_state *state)
476 {
477         struct unixid xid;
478
479         DEBUG(3, ("[%5lu]: set hwm\n", (unsigned long)state->pid));
480
481         if ( ! state->privileged) {
482                 DEBUG(0, ("Only root is allowed to set mappings!\n"));
483                 request_error(state);
484                 return;
485         }
486
487         xid.id = state->request->data.dual_idmapset.id;
488         xid.type = state->request->data.dual_idmapset.type;
489
490         winbindd_set_hwm_async(state->mem_ctx, &xid, set_hwm_recv, state);
491 }
492
493 /* Convert a uid to a sid */
494
495 static void uid2sid_recv(void *private_data, bool success, const char *sidstr)
496 {
497         struct winbindd_cli_state *state =
498                 (struct winbindd_cli_state *)private_data;
499         struct dom_sid sid;
500
501         if (!success || !string_to_sid(&sid, sidstr)) {
502                 ZERO_STRUCT(sid);
503                 idmap_cache_set_sid2uid(&sid, state->request->data.uid);
504                 request_error(state);
505                 return;
506         }
507
508         DEBUG(10,("uid2sid: uid %lu has sid %s\n",
509                   (unsigned long)(state->request->data.uid), sidstr));
510
511         idmap_cache_set_sid2uid(&sid, state->request->data.uid);
512         fstrcpy(state->response->data.sid.sid, sidstr);
513         state->response->data.sid.type = SID_NAME_USER;
514         request_ok(state);
515         return;
516 }
517
518 void winbindd_uid_to_sid(struct winbindd_cli_state *state)
519 {
520         struct dom_sid sid;
521         bool expired;
522
523         DEBUG(3, ("[%5lu]: uid to sid %lu\n", (unsigned long)state->pid, 
524                   (unsigned long)state->request->data.uid));
525
526         if (idmap_cache_find_uid2sid(state->request->data.uid, &sid,
527                                      &expired)) {
528                 DEBUG(10, ("idmap_cache_find_uid2sid found %d%s\n",
529                            (int)state->request->data.uid,
530                            expired ? " (expired)": ""));
531                 if (expired && IS_DOMAIN_ONLINE(find_our_domain())) {
532                         DEBUG(10, ("revalidating expired entry\n"));
533                         goto backend;
534                 }
535                 if (is_null_sid(&sid)) {
536                         DEBUG(10, ("Returning negative cache entry\n"));
537                         request_error(state);
538                         return;
539                 }
540                 DEBUG(10, ("Returning positive cache entry\n"));
541                 sid_to_fstring(state->response->data.sid.sid, &sid);
542                 request_ok(state);
543                 return;
544         }
545
546         /* always go via the async interface (may block) */
547  backend:
548         winbindd_uid2sid_async(state->mem_ctx, state->request->data.uid, uid2sid_recv, state);
549 }
550
551 /* Convert a gid to a sid */
552
553 static void gid2sid_recv(void *private_data, bool success, const char *sidstr)
554 {
555         struct winbindd_cli_state *state =
556                 (struct winbindd_cli_state *)private_data;
557         struct dom_sid sid;
558
559         if (!success || !string_to_sid(&sid, sidstr)) {
560                 ZERO_STRUCT(sid);
561                 idmap_cache_set_sid2gid(&sid, state->request->data.gid);
562                 request_error(state);
563                 return;
564         }
565         DEBUG(10,("gid2sid: gid %lu has sid %s\n",
566                   (unsigned long)(state->request->data.gid), sidstr));
567
568         idmap_cache_set_sid2gid(&sid, state->request->data.gid);
569         fstrcpy(state->response->data.sid.sid, sidstr);
570         state->response->data.sid.type = SID_NAME_DOM_GRP;
571         request_ok(state);
572         return;
573 }
574
575
576 void winbindd_gid_to_sid(struct winbindd_cli_state *state)
577 {
578         struct dom_sid sid;
579         bool expired;
580
581         DEBUG(3, ("[%5lu]: gid to sid %lu\n", (unsigned long)state->pid, 
582                   (unsigned long)state->request->data.gid));
583
584         if (idmap_cache_find_gid2sid(state->request->data.gid, &sid,
585                                      &expired)) {
586                 DEBUG(10, ("idmap_cache_find_gid2sid found %d%s\n",
587                            (int)state->request->data.gid,
588                            expired ? " (expired)": ""));
589                 if (expired && IS_DOMAIN_ONLINE(find_our_domain())) {
590                         DEBUG(10, ("revalidating expired entry\n"));
591                         goto backend;
592                 }
593                 if (is_null_sid(&sid)) {
594                         DEBUG(10, ("Returning negative cache entry\n"));
595                         request_error(state);
596                         return;
597                 }
598                 DEBUG(10, ("Returning positive cache entry\n"));
599                 sid_to_fstring(state->response->data.sid.sid, &sid);
600                 request_ok(state);
601                 return;
602         }
603
604         /* always use async calls (may block) */
605  backend:
606         winbindd_gid2sid_async(state->mem_ctx, state->request->data.gid, gid2sid_recv, state);
607 }
608
609 void winbindd_allocate_uid(struct winbindd_cli_state *state)
610 {
611         if ( !state->privileged ) {
612                 DEBUG(2, ("winbindd_allocate_uid: non-privileged access "
613                           "denied!\n"));
614                 request_error(state);
615                 return;
616         }
617
618         sendto_child(state, idmap_child());
619 }
620
621 enum winbindd_result winbindd_dual_allocate_uid(struct winbindd_domain *domain,
622                                                 struct winbindd_cli_state *state)
623 {
624         struct unixid xid;
625
626         if (!NT_STATUS_IS_OK(idmap_allocate_uid(&xid))) {
627                 return WINBINDD_ERROR;
628         }
629         state->response->data.uid = xid.id;
630         return WINBINDD_OK;
631 }
632
633 void winbindd_allocate_gid(struct winbindd_cli_state *state)
634 {
635         if ( !state->privileged ) {
636                 DEBUG(2, ("winbindd_allocate_gid: non-privileged access "
637                           "denied!\n"));
638                 request_error(state);
639                 return;
640         }
641
642         sendto_child(state, idmap_child());
643 }
644
645 enum winbindd_result winbindd_dual_allocate_gid(struct winbindd_domain *domain,
646                                                 struct winbindd_cli_state *state)
647 {
648         struct unixid xid;
649
650         if (!NT_STATUS_IS_OK(idmap_allocate_gid(&xid))) {
651                 return WINBINDD_ERROR;
652         }
653         state->response->data.gid = xid.id;
654         return WINBINDD_OK;
655 }