s3-rpc: Avoid including every pipe's client and server stubs everywhere in samba.
[amitay/samba.git] / source3 / rpc_server / srv_wkssvc_nt.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *
5  *  Copyright (C) Andrew Tridgell               1992-1997,
6  *  Copyright (C) Gerald (Jerry) Carter         2006.
7  *  Copyright (C) Guenther Deschner             2007-2008.
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 /* This is the implementation of the wks interface. */
24
25 #include "includes.h"
26 #include "libnet/libnet.h"
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/srv_wkssvc.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
32
33 /*******************************************************************
34  Fill in the values for the struct wkssvc_NetWkstaInfo100.
35  ********************************************************************/
36
37 static void create_wks_info_100(struct wkssvc_NetWkstaInfo100 *info100)
38 {
39         info100->platform_id     = PLATFORM_ID_NT;      /* unknown */
40         info100->version_major   = lp_major_announce_version();
41         info100->version_minor   = lp_minor_announce_version();
42
43         info100->server_name = talloc_asprintf_strupper_m(
44                 info100, "%s", global_myname());
45         info100->domain_name = talloc_asprintf_strupper_m(
46                 info100, "%s", lp_workgroup());
47
48         return;
49 }
50
51 /********************************************************************
52  only supports info level 100 at the moment.
53  ********************************************************************/
54
55 WERROR _wkssvc_NetWkstaGetInfo(pipes_struct *p, struct wkssvc_NetWkstaGetInfo *r)
56 {
57         struct wkssvc_NetWkstaInfo100 *wks100 = NULL;
58
59         /* We only support info level 100 currently */
60
61         if ( r->in.level != 100 ) {
62                 return WERR_UNKNOWN_LEVEL;
63         }
64
65         if ( (wks100 = TALLOC_ZERO_P(p->mem_ctx, struct wkssvc_NetWkstaInfo100)) == NULL ) {
66                 return WERR_NOMEM;
67         }
68
69         create_wks_info_100( wks100 );
70
71         r->out.info->info100 = wks100;
72
73         return WERR_OK;
74 }
75
76 /********************************************************************
77  ********************************************************************/
78
79 WERROR _wkssvc_NetWkstaSetInfo(pipes_struct *p, struct wkssvc_NetWkstaSetInfo *r)
80 {
81         /* FIXME: Add implementation code here */
82         p->rng_fault_state = True;
83         return WERR_NOT_SUPPORTED;
84 }
85
86 /********************************************************************
87  ********************************************************************/
88
89 WERROR _wkssvc_NetWkstaEnumUsers(pipes_struct *p, struct wkssvc_NetWkstaEnumUsers *r)
90 {
91         /* FIXME: Add implementation code here */
92         p->rng_fault_state = True;
93         return WERR_NOT_SUPPORTED;
94 }
95
96 /********************************************************************
97  ********************************************************************/
98
99 WERROR _wkssvc_NetrWkstaUserGetInfo(pipes_struct *p, struct wkssvc_NetrWkstaUserGetInfo *r)
100 {
101         /* FIXME: Add implementation code here */
102         p->rng_fault_state = True;
103         return WERR_NOT_SUPPORTED;
104 }
105
106 /********************************************************************
107  ********************************************************************/
108
109 WERROR _wkssvc_NetrWkstaUserSetInfo(pipes_struct *p, struct wkssvc_NetrWkstaUserSetInfo *r)
110 {
111         /* FIXME: Add implementation code here */
112         p->rng_fault_state = True;
113         return WERR_NOT_SUPPORTED;
114 }
115
116 /********************************************************************
117  ********************************************************************/
118
119 WERROR _wkssvc_NetWkstaTransportEnum(pipes_struct *p, struct wkssvc_NetWkstaTransportEnum *r)
120 {
121         /* FIXME: Add implementation code here */
122         p->rng_fault_state = True;
123         return WERR_NOT_SUPPORTED;
124 }
125
126 /********************************************************************
127  ********************************************************************/
128
129 WERROR _wkssvc_NetrWkstaTransportAdd(pipes_struct *p, struct wkssvc_NetrWkstaTransportAdd *r)
130 {
131         /* FIXME: Add implementation code here */
132         p->rng_fault_state = True;
133         return WERR_NOT_SUPPORTED;
134 }
135
136 /********************************************************************
137  ********************************************************************/
138
139 WERROR _wkssvc_NetrWkstaTransportDel(pipes_struct *p, struct wkssvc_NetrWkstaTransportDel *r)
140 {
141         /* FIXME: Add implementation code here */
142         p->rng_fault_state = True;
143         return WERR_NOT_SUPPORTED;
144 }
145
146 /********************************************************************
147  ********************************************************************/
148
149 WERROR _wkssvc_NetrUseAdd(pipes_struct *p, struct wkssvc_NetrUseAdd *r)
150 {
151         /* FIXME: Add implementation code here */
152         p->rng_fault_state = True;
153         return WERR_NOT_SUPPORTED;
154 }
155
156 /********************************************************************
157  ********************************************************************/
158
159 WERROR _wkssvc_NetrUseGetInfo(pipes_struct *p, struct wkssvc_NetrUseGetInfo *r)
160 {
161         /* FIXME: Add implementation code here */
162         p->rng_fault_state = True;
163         return WERR_NOT_SUPPORTED;
164 }
165
166 /********************************************************************
167  ********************************************************************/
168
169 WERROR _wkssvc_NetrUseDel(pipes_struct *p, struct wkssvc_NetrUseDel *r)
170 {
171         /* FIXME: Add implementation code here */
172         p->rng_fault_state = True;
173         return WERR_NOT_SUPPORTED;
174 }
175
176 /********************************************************************
177  ********************************************************************/
178
179 WERROR _wkssvc_NetrUseEnum(pipes_struct *p, struct wkssvc_NetrUseEnum *r)
180 {
181         /* FIXME: Add implementation code here */
182         p->rng_fault_state = True;
183         return WERR_NOT_SUPPORTED;
184 }
185
186 /********************************************************************
187  ********************************************************************/
188
189 WERROR _wkssvc_NetrMessageBufferSend(pipes_struct *p, struct wkssvc_NetrMessageBufferSend *r)
190 {
191         /* FIXME: Add implementation code here */
192         p->rng_fault_state = True;
193         return WERR_NOT_SUPPORTED;
194 }
195
196 /********************************************************************
197  ********************************************************************/
198
199 WERROR _wkssvc_NetrWorkstationStatisticsGet(pipes_struct *p, struct wkssvc_NetrWorkstationStatisticsGet *r) 
200 {
201         /* FIXME: Add implementation code here */
202         p->rng_fault_state = True;
203         return WERR_NOT_SUPPORTED;
204 }
205
206 /********************************************************************
207  ********************************************************************/
208
209 WERROR _wkssvc_NetrLogonDomainNameAdd(pipes_struct *p, struct wkssvc_NetrLogonDomainNameAdd *r)
210 {
211         /* FIXME: Add implementation code here */
212         p->rng_fault_state = True;
213         return WERR_NOT_SUPPORTED;
214 }
215
216 /********************************************************************
217  ********************************************************************/
218
219 WERROR _wkssvc_NetrLogonDomainNameDel(pipes_struct *p, struct wkssvc_NetrLogonDomainNameDel *r)
220 {
221         /* FIXME: Add implementation code here */
222         p->rng_fault_state = True;
223         return WERR_NOT_SUPPORTED;
224 }
225
226 /********************************************************************
227  ********************************************************************/
228
229 WERROR _wkssvc_NetrJoinDomain(pipes_struct *p, struct wkssvc_NetrJoinDomain *r)
230 {
231         /* FIXME: Add implementation code here */
232         p->rng_fault_state = True;
233         return WERR_NOT_SUPPORTED;
234 }
235
236 /********************************************************************
237  ********************************************************************/
238
239 WERROR _wkssvc_NetrUnjoinDomain(pipes_struct *p, struct wkssvc_NetrUnjoinDomain *r)
240 {
241         /* FIXME: Add implementation code here */
242         p->rng_fault_state = True;
243         return WERR_NOT_SUPPORTED;
244 }
245
246 /********************************************************************
247  ********************************************************************/
248
249 WERROR _wkssvc_NetrRenameMachineInDomain(pipes_struct *p, struct wkssvc_NetrRenameMachineInDomain *r)
250 {
251         /* FIXME: Add implementation code here */
252         p->rng_fault_state = True;
253         return WERR_NOT_SUPPORTED;
254 }
255
256 /********************************************************************
257  ********************************************************************/
258
259 WERROR _wkssvc_NetrValidateName(pipes_struct *p, struct wkssvc_NetrValidateName *r)
260 {
261         /* FIXME: Add implementation code here */
262         p->rng_fault_state = True;
263         return WERR_NOT_SUPPORTED;
264 }
265
266 /********************************************************************
267  ********************************************************************/
268
269 WERROR _wkssvc_NetrGetJoinInformation(pipes_struct *p, struct wkssvc_NetrGetJoinInformation *r)
270 {
271         /* FIXME: Add implementation code here */
272         p->rng_fault_state = True;
273         return WERR_NOT_SUPPORTED;
274 }
275
276 /********************************************************************
277  ********************************************************************/
278
279 WERROR _wkssvc_NetrGetJoinableOus(pipes_struct *p, struct wkssvc_NetrGetJoinableOus *r)
280 {
281         /* FIXME: Add implementation code here */
282         p->rng_fault_state = True;
283         return WERR_NOT_SUPPORTED;
284 }
285
286 /********************************************************************
287  _wkssvc_NetrJoinDomain2
288  ********************************************************************/
289
290 WERROR _wkssvc_NetrJoinDomain2(pipes_struct *p,
291                                struct wkssvc_NetrJoinDomain2 *r)
292 {
293         struct libnet_JoinCtx *j = NULL;
294         char *cleartext_pwd = NULL;
295         char *admin_domain = NULL;
296         char *admin_account = NULL;
297         WERROR werr;
298         struct nt_user_token *token = p->server_info->ptok;
299
300         if (!r->in.domain_name) {
301                 return WERR_INVALID_PARAM;
302         }
303
304         if (!r->in.admin_account || !r->in.encrypted_password) {
305                 return WERR_INVALID_PARAM;
306         }
307
308         if (!user_has_privileges(token, &se_machine_account) &&
309             !nt_token_check_domain_rid(token, DOMAIN_GROUP_RID_ADMINS) &&
310             !nt_token_check_sid(&global_sid_Builtin_Administrators, token)) {
311                 DEBUG(5,("_wkssvc_NetrJoinDomain2: account doesn't have "
312                         "sufficient privileges\n"));
313                 return WERR_ACCESS_DENIED;
314         }
315
316         if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED) ||
317             (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
318                 return WERR_NOT_SUPPORTED;
319         }
320
321         werr = decode_wkssvc_join_password_buffer(
322                 p->mem_ctx, r->in.encrypted_password,
323                 &p->server_info->user_session_key, &cleartext_pwd);
324         if (!W_ERROR_IS_OK(werr)) {
325                 return werr;
326         }
327
328         split_domain_user(p->mem_ctx,
329                           r->in.admin_account,
330                           &admin_domain,
331                           &admin_account);
332
333         werr = libnet_init_JoinCtx(p->mem_ctx, &j);
334         if (!W_ERROR_IS_OK(werr)) {
335                 return werr;
336         }
337
338         j->in.domain_name       = r->in.domain_name;
339         j->in.account_ou        = r->in.account_ou;
340         j->in.join_flags        = r->in.join_flags;
341         j->in.admin_account     = admin_account;
342         j->in.admin_password    = cleartext_pwd;
343         j->in.debug             = true;
344         j->in.modify_config     = lp_config_backend_is_registry();
345         j->in.msg_ctx           = smbd_messaging_context();
346
347         become_root();
348         werr = libnet_Join(p->mem_ctx, j);
349         unbecome_root();
350
351         if (!W_ERROR_IS_OK(werr)) {
352                 DEBUG(5,("_wkssvc_NetrJoinDomain2: libnet_Join failed with: %s\n",
353                         j->out.error_string ? j->out.error_string :
354                         win_errstr(werr)));
355         }
356
357         TALLOC_FREE(j);
358         return werr;
359 }
360
361 /********************************************************************
362  _wkssvc_NetrUnjoinDomain2
363  ********************************************************************/
364
365 WERROR _wkssvc_NetrUnjoinDomain2(pipes_struct *p,
366                                  struct wkssvc_NetrUnjoinDomain2 *r)
367 {
368         struct libnet_UnjoinCtx *u = NULL;
369         char *cleartext_pwd = NULL;
370         char *admin_domain = NULL;
371         char *admin_account = NULL;
372         WERROR werr;
373         struct nt_user_token *token = p->server_info->ptok;
374
375         if (!r->in.account || !r->in.encrypted_password) {
376                 return WERR_INVALID_PARAM;
377         }
378
379         if (!user_has_privileges(token, &se_machine_account) &&
380             !nt_token_check_domain_rid(token, DOMAIN_GROUP_RID_ADMINS) &&
381             !nt_token_check_sid(&global_sid_Builtin_Administrators, token)) {
382                 DEBUG(5,("_wkssvc_NetrUnjoinDomain2: account doesn't have "
383                         "sufficient privileges\n"));
384                 return WERR_ACCESS_DENIED;
385         }
386
387         werr = decode_wkssvc_join_password_buffer(
388                 p->mem_ctx, r->in.encrypted_password,
389                 &p->server_info->user_session_key, &cleartext_pwd);
390         if (!W_ERROR_IS_OK(werr)) {
391                 return werr;
392         }
393
394         split_domain_user(p->mem_ctx,
395                           r->in.account,
396                           &admin_domain,
397                           &admin_account);
398
399         werr = libnet_init_UnjoinCtx(p->mem_ctx, &u);
400         if (!W_ERROR_IS_OK(werr)) {
401                 return werr;
402         }
403
404         u->in.domain_name       = lp_realm();
405         u->in.unjoin_flags      = r->in.unjoin_flags |
406                                   WKSSVC_JOIN_FLAGS_JOIN_TYPE;
407         u->in.admin_account     = admin_account;
408         u->in.admin_password    = cleartext_pwd;
409         u->in.debug             = true;
410         u->in.modify_config     = lp_config_backend_is_registry();
411         u->in.msg_ctx           = smbd_messaging_context();
412
413         become_root();
414         werr = libnet_Unjoin(p->mem_ctx, u);
415         unbecome_root();
416
417         if (!W_ERROR_IS_OK(werr)) {
418                 DEBUG(5,("_wkssvc_NetrUnjoinDomain2: libnet_Unjoin failed with: %s\n",
419                         u->out.error_string ? u->out.error_string :
420                         win_errstr(werr)));
421         }
422
423         TALLOC_FREE(u);
424         return werr;
425 }
426
427 /********************************************************************
428  ********************************************************************/
429
430 WERROR _wkssvc_NetrRenameMachineInDomain2(pipes_struct *p, struct wkssvc_NetrRenameMachineInDomain2 *r)
431 {
432         /* for now just return not supported */
433         return WERR_NOT_SUPPORTED;
434 }
435
436 /********************************************************************
437  ********************************************************************/
438
439 WERROR _wkssvc_NetrValidateName2(pipes_struct *p, struct wkssvc_NetrValidateName2 *r)
440 {
441         /* FIXME: Add implementation code here */
442         p->rng_fault_state = True;
443         return WERR_NOT_SUPPORTED;
444 }
445
446 /********************************************************************
447  ********************************************************************/
448
449 WERROR _wkssvc_NetrGetJoinableOus2(pipes_struct *p, struct wkssvc_NetrGetJoinableOus2 *r)
450 {
451         /* FIXME: Add implementation code here */
452         p->rng_fault_state = True;
453         return WERR_NOT_SUPPORTED;
454 }
455
456 /********************************************************************
457  ********************************************************************/
458
459 WERROR _wkssvc_NetrAddAlternateComputerName(pipes_struct *p, struct wkssvc_NetrAddAlternateComputerName *r)
460 {
461         /* FIXME: Add implementation code here */
462         p->rng_fault_state = True;
463         return WERR_NOT_SUPPORTED;
464 }
465
466 /********************************************************************
467  ********************************************************************/
468
469 WERROR _wkssvc_NetrRemoveAlternateComputerName(pipes_struct *p, struct wkssvc_NetrRemoveAlternateComputerName *r)
470 {
471         /* FIXME: Add implementation code here */
472         p->rng_fault_state = True;
473         return WERR_NOT_SUPPORTED;
474 }
475
476 /********************************************************************
477  ********************************************************************/
478
479 WERROR _wkssvc_NetrSetPrimaryComputername(pipes_struct *p, struct wkssvc_NetrSetPrimaryComputername *r)
480 {
481         /* FIXME: Add implementation code here */
482         p->rng_fault_state = True;
483         return WERR_NOT_SUPPORTED;
484 }
485
486 /********************************************************************
487  ********************************************************************/
488
489 WERROR _wkssvc_NetrEnumerateComputerNames(pipes_struct *p, struct wkssvc_NetrEnumerateComputerNames *r)
490 {
491         /* FIXME: Add implementation code here */
492         p->rng_fault_state = True;
493         return WERR_NOT_SUPPORTED;
494 }
495