s3: Support NetWkstaGetInfo 101 and 102
[ira/wip.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 #ifdef HAVE_GETUTXENT
34
35 #include <utmpx.h>
36
37 static char **get_logged_on_userlist(TALLOC_CTX *mem_ctx)
38 {
39         char **users = NULL;
40         int num_users = 0;
41         struct utmpx *u;
42
43         while ((u = getutxent()) != NULL) {
44                 char **tmp;
45                 tmp = talloc_realloc(mem_ctx, users, char *, num_users+1);
46                 if (tmp == NULL) {
47                         return NULL;
48                 }
49                 users = tmp;
50                 users[num_users] = talloc_strdup(users, u->ut_user);
51                 if (users[num_users] == NULL) {
52                         TALLOC_FREE(users);
53                         return NULL;
54                 }
55         }
56         return users;
57 }
58
59 #else
60
61 static char **get_logged_on_userlist(TALLOC_CTX *mem_ctx)
62 {
63         return NULL;
64 }
65
66 #endif
67
68 /*******************************************************************
69  Fill in the values for the struct wkssvc_NetWkstaInfo100.
70  ********************************************************************/
71
72 static struct wkssvc_NetWkstaInfo100 *create_wks_info_100(TALLOC_CTX *mem_ctx)
73 {
74         struct wkssvc_NetWkstaInfo100 *info100;
75
76         info100 = talloc(mem_ctx, struct wkssvc_NetWkstaInfo100);
77         if (info100 == NULL) {
78                 return NULL;
79         }
80
81         info100->platform_id     = PLATFORM_ID_NT;      /* unknown */
82         info100->version_major   = lp_major_announce_version();
83         info100->version_minor   = lp_minor_announce_version();
84
85         info100->server_name = talloc_asprintf_strupper_m(
86                 info100, "%s", global_myname());
87         info100->domain_name = talloc_asprintf_strupper_m(
88                 info100, "%s", lp_workgroup());
89
90         return info100;
91 }
92
93 static struct wkssvc_NetWkstaInfo101 *create_wks_info_101(TALLOC_CTX *mem_ctx)
94 {
95         struct wkssvc_NetWkstaInfo101 *info101;
96
97         info101 = talloc(mem_ctx, struct wkssvc_NetWkstaInfo101);
98         if (info101 == NULL) {
99                 return NULL;
100         }
101
102         info101->platform_id     = PLATFORM_ID_NT;      /* unknown */
103         info101->version_major   = lp_major_announce_version();
104         info101->version_minor   = lp_minor_announce_version();
105
106         info101->server_name = talloc_asprintf_strupper_m(
107                 info101, "%s", global_myname());
108         info101->domain_name = talloc_asprintf_strupper_m(
109                 info101, "%s", lp_workgroup());
110         info101->lan_root = NULL;
111
112         return info101;
113 }
114
115 static struct wkssvc_NetWkstaInfo102 *create_wks_info_102(TALLOC_CTX *mem_ctx)
116 {
117         struct wkssvc_NetWkstaInfo102 *info102;
118         char **users;
119
120         info102 = talloc(mem_ctx, struct wkssvc_NetWkstaInfo102);
121         if (info102 == NULL) {
122                 return NULL;
123         }
124
125         info102->platform_id     = PLATFORM_ID_NT;      /* unknown */
126         info102->version_major   = lp_major_announce_version();
127         info102->version_minor   = lp_minor_announce_version();
128
129         info102->server_name = talloc_asprintf_strupper_m(
130                 info102, "%s", global_myname());
131         info102->domain_name = talloc_asprintf_strupper_m(
132                 info102, "%s", lp_workgroup());
133         info102->lan_root = NULL;
134
135         users = get_logged_on_userlist(talloc_tos());
136         info102->logged_on_users = talloc_array_length(users);
137         TALLOC_FREE(users);
138
139         return info102;
140 }
141
142 /********************************************************************
143  only supports info level 100 at the moment.
144  ********************************************************************/
145
146 WERROR _wkssvc_NetWkstaGetInfo(pipes_struct *p, struct wkssvc_NetWkstaGetInfo *r)
147 {
148         switch (r->in.level) {
149         case 100:
150                 r->out.info->info100 = create_wks_info_100(p->mem_ctx);
151                 if (r->out.info->info100 == NULL) {
152                         return WERR_NOMEM;
153                 }
154                 break;
155         case 101:
156                 r->out.info->info101 = create_wks_info_101(p->mem_ctx);
157                 if (r->out.info->info101 == NULL) {
158                         return WERR_NOMEM;
159                 }
160                 break;
161         case 102:
162                 r->out.info->info102 = create_wks_info_102(p->mem_ctx);
163                 if (r->out.info->info102 == NULL) {
164                         return WERR_NOMEM;
165                 }
166                 break;
167         default:
168                 return WERR_UNKNOWN_LEVEL;
169         }
170
171         return WERR_OK;
172 }
173
174 /********************************************************************
175  ********************************************************************/
176
177 WERROR _wkssvc_NetWkstaSetInfo(pipes_struct *p, struct wkssvc_NetWkstaSetInfo *r)
178 {
179         /* FIXME: Add implementation code here */
180         p->rng_fault_state = True;
181         return WERR_NOT_SUPPORTED;
182 }
183
184 /********************************************************************
185  ********************************************************************/
186
187 WERROR _wkssvc_NetWkstaEnumUsers(pipes_struct *p, struct wkssvc_NetWkstaEnumUsers *r)
188 {
189         /* FIXME: Add implementation code here */
190         p->rng_fault_state = True;
191         return WERR_NOT_SUPPORTED;
192 }
193
194 /********************************************************************
195  ********************************************************************/
196
197 WERROR _wkssvc_NetrWkstaUserGetInfo(pipes_struct *p, struct wkssvc_NetrWkstaUserGetInfo *r)
198 {
199         /* FIXME: Add implementation code here */
200         p->rng_fault_state = True;
201         return WERR_NOT_SUPPORTED;
202 }
203
204 /********************************************************************
205  ********************************************************************/
206
207 WERROR _wkssvc_NetrWkstaUserSetInfo(pipes_struct *p, struct wkssvc_NetrWkstaUserSetInfo *r)
208 {
209         /* FIXME: Add implementation code here */
210         p->rng_fault_state = True;
211         return WERR_NOT_SUPPORTED;
212 }
213
214 /********************************************************************
215  ********************************************************************/
216
217 WERROR _wkssvc_NetWkstaTransportEnum(pipes_struct *p, struct wkssvc_NetWkstaTransportEnum *r)
218 {
219         /* FIXME: Add implementation code here */
220         p->rng_fault_state = True;
221         return WERR_NOT_SUPPORTED;
222 }
223
224 /********************************************************************
225  ********************************************************************/
226
227 WERROR _wkssvc_NetrWkstaTransportAdd(pipes_struct *p, struct wkssvc_NetrWkstaTransportAdd *r)
228 {
229         /* FIXME: Add implementation code here */
230         p->rng_fault_state = True;
231         return WERR_NOT_SUPPORTED;
232 }
233
234 /********************************************************************
235  ********************************************************************/
236
237 WERROR _wkssvc_NetrWkstaTransportDel(pipes_struct *p, struct wkssvc_NetrWkstaTransportDel *r)
238 {
239         /* FIXME: Add implementation code here */
240         p->rng_fault_state = True;
241         return WERR_NOT_SUPPORTED;
242 }
243
244 /********************************************************************
245  ********************************************************************/
246
247 WERROR _wkssvc_NetrUseAdd(pipes_struct *p, struct wkssvc_NetrUseAdd *r)
248 {
249         /* FIXME: Add implementation code here */
250         p->rng_fault_state = True;
251         return WERR_NOT_SUPPORTED;
252 }
253
254 /********************************************************************
255  ********************************************************************/
256
257 WERROR _wkssvc_NetrUseGetInfo(pipes_struct *p, struct wkssvc_NetrUseGetInfo *r)
258 {
259         /* FIXME: Add implementation code here */
260         p->rng_fault_state = True;
261         return WERR_NOT_SUPPORTED;
262 }
263
264 /********************************************************************
265  ********************************************************************/
266
267 WERROR _wkssvc_NetrUseDel(pipes_struct *p, struct wkssvc_NetrUseDel *r)
268 {
269         /* FIXME: Add implementation code here */
270         p->rng_fault_state = True;
271         return WERR_NOT_SUPPORTED;
272 }
273
274 /********************************************************************
275  ********************************************************************/
276
277 WERROR _wkssvc_NetrUseEnum(pipes_struct *p, struct wkssvc_NetrUseEnum *r)
278 {
279         /* FIXME: Add implementation code here */
280         p->rng_fault_state = True;
281         return WERR_NOT_SUPPORTED;
282 }
283
284 /********************************************************************
285  ********************************************************************/
286
287 WERROR _wkssvc_NetrMessageBufferSend(pipes_struct *p, struct wkssvc_NetrMessageBufferSend *r)
288 {
289         /* FIXME: Add implementation code here */
290         p->rng_fault_state = True;
291         return WERR_NOT_SUPPORTED;
292 }
293
294 /********************************************************************
295  ********************************************************************/
296
297 WERROR _wkssvc_NetrWorkstationStatisticsGet(pipes_struct *p, struct wkssvc_NetrWorkstationStatisticsGet *r) 
298 {
299         /* FIXME: Add implementation code here */
300         p->rng_fault_state = True;
301         return WERR_NOT_SUPPORTED;
302 }
303
304 /********************************************************************
305  ********************************************************************/
306
307 WERROR _wkssvc_NetrLogonDomainNameAdd(pipes_struct *p, struct wkssvc_NetrLogonDomainNameAdd *r)
308 {
309         /* FIXME: Add implementation code here */
310         p->rng_fault_state = True;
311         return WERR_NOT_SUPPORTED;
312 }
313
314 /********************************************************************
315  ********************************************************************/
316
317 WERROR _wkssvc_NetrLogonDomainNameDel(pipes_struct *p, struct wkssvc_NetrLogonDomainNameDel *r)
318 {
319         /* FIXME: Add implementation code here */
320         p->rng_fault_state = True;
321         return WERR_NOT_SUPPORTED;
322 }
323
324 /********************************************************************
325  ********************************************************************/
326
327 WERROR _wkssvc_NetrJoinDomain(pipes_struct *p, struct wkssvc_NetrJoinDomain *r)
328 {
329         /* FIXME: Add implementation code here */
330         p->rng_fault_state = True;
331         return WERR_NOT_SUPPORTED;
332 }
333
334 /********************************************************************
335  ********************************************************************/
336
337 WERROR _wkssvc_NetrUnjoinDomain(pipes_struct *p, struct wkssvc_NetrUnjoinDomain *r)
338 {
339         /* FIXME: Add implementation code here */
340         p->rng_fault_state = True;
341         return WERR_NOT_SUPPORTED;
342 }
343
344 /********************************************************************
345  ********************************************************************/
346
347 WERROR _wkssvc_NetrRenameMachineInDomain(pipes_struct *p, struct wkssvc_NetrRenameMachineInDomain *r)
348 {
349         /* FIXME: Add implementation code here */
350         p->rng_fault_state = True;
351         return WERR_NOT_SUPPORTED;
352 }
353
354 /********************************************************************
355  ********************************************************************/
356
357 WERROR _wkssvc_NetrValidateName(pipes_struct *p, struct wkssvc_NetrValidateName *r)
358 {
359         /* FIXME: Add implementation code here */
360         p->rng_fault_state = True;
361         return WERR_NOT_SUPPORTED;
362 }
363
364 /********************************************************************
365  ********************************************************************/
366
367 WERROR _wkssvc_NetrGetJoinInformation(pipes_struct *p, struct wkssvc_NetrGetJoinInformation *r)
368 {
369         /* FIXME: Add implementation code here */
370         p->rng_fault_state = True;
371         return WERR_NOT_SUPPORTED;
372 }
373
374 /********************************************************************
375  ********************************************************************/
376
377 WERROR _wkssvc_NetrGetJoinableOus(pipes_struct *p, struct wkssvc_NetrGetJoinableOus *r)
378 {
379         /* FIXME: Add implementation code here */
380         p->rng_fault_state = True;
381         return WERR_NOT_SUPPORTED;
382 }
383
384 /********************************************************************
385  _wkssvc_NetrJoinDomain2
386  ********************************************************************/
387
388 WERROR _wkssvc_NetrJoinDomain2(pipes_struct *p,
389                                struct wkssvc_NetrJoinDomain2 *r)
390 {
391         struct libnet_JoinCtx *j = NULL;
392         char *cleartext_pwd = NULL;
393         char *admin_domain = NULL;
394         char *admin_account = NULL;
395         WERROR werr;
396         struct nt_user_token *token = p->server_info->ptok;
397
398         if (!r->in.domain_name) {
399                 return WERR_INVALID_PARAM;
400         }
401
402         if (!r->in.admin_account || !r->in.encrypted_password) {
403                 return WERR_INVALID_PARAM;
404         }
405
406         if (!user_has_privileges(token, &se_machine_account) &&
407             !nt_token_check_domain_rid(token, DOMAIN_GROUP_RID_ADMINS) &&
408             !nt_token_check_sid(&global_sid_Builtin_Administrators, token)) {
409                 DEBUG(5,("_wkssvc_NetrJoinDomain2: account doesn't have "
410                         "sufficient privileges\n"));
411                 return WERR_ACCESS_DENIED;
412         }
413
414         if ((r->in.join_flags & WKSSVC_JOIN_FLAGS_MACHINE_PWD_PASSED) ||
415             (r->in.join_flags & WKSSVC_JOIN_FLAGS_JOIN_UNSECURE)) {
416                 return WERR_NOT_SUPPORTED;
417         }
418
419         werr = decode_wkssvc_join_password_buffer(
420                 p->mem_ctx, r->in.encrypted_password,
421                 &p->server_info->user_session_key, &cleartext_pwd);
422         if (!W_ERROR_IS_OK(werr)) {
423                 return werr;
424         }
425
426         split_domain_user(p->mem_ctx,
427                           r->in.admin_account,
428                           &admin_domain,
429                           &admin_account);
430
431         werr = libnet_init_JoinCtx(p->mem_ctx, &j);
432         if (!W_ERROR_IS_OK(werr)) {
433                 return werr;
434         }
435
436         j->in.domain_name       = r->in.domain_name;
437         j->in.account_ou        = r->in.account_ou;
438         j->in.join_flags        = r->in.join_flags;
439         j->in.admin_account     = admin_account;
440         j->in.admin_password    = cleartext_pwd;
441         j->in.debug             = true;
442         j->in.modify_config     = lp_config_backend_is_registry();
443         j->in.msg_ctx           = smbd_messaging_context();
444
445         become_root();
446         werr = libnet_Join(p->mem_ctx, j);
447         unbecome_root();
448
449         if (!W_ERROR_IS_OK(werr)) {
450                 DEBUG(5,("_wkssvc_NetrJoinDomain2: libnet_Join failed with: %s\n",
451                         j->out.error_string ? j->out.error_string :
452                         win_errstr(werr)));
453         }
454
455         TALLOC_FREE(j);
456         return werr;
457 }
458
459 /********************************************************************
460  _wkssvc_NetrUnjoinDomain2
461  ********************************************************************/
462
463 WERROR _wkssvc_NetrUnjoinDomain2(pipes_struct *p,
464                                  struct wkssvc_NetrUnjoinDomain2 *r)
465 {
466         struct libnet_UnjoinCtx *u = NULL;
467         char *cleartext_pwd = NULL;
468         char *admin_domain = NULL;
469         char *admin_account = NULL;
470         WERROR werr;
471         struct nt_user_token *token = p->server_info->ptok;
472
473         if (!r->in.account || !r->in.encrypted_password) {
474                 return WERR_INVALID_PARAM;
475         }
476
477         if (!user_has_privileges(token, &se_machine_account) &&
478             !nt_token_check_domain_rid(token, DOMAIN_GROUP_RID_ADMINS) &&
479             !nt_token_check_sid(&global_sid_Builtin_Administrators, token)) {
480                 DEBUG(5,("_wkssvc_NetrUnjoinDomain2: account doesn't have "
481                         "sufficient privileges\n"));
482                 return WERR_ACCESS_DENIED;
483         }
484
485         werr = decode_wkssvc_join_password_buffer(
486                 p->mem_ctx, r->in.encrypted_password,
487                 &p->server_info->user_session_key, &cleartext_pwd);
488         if (!W_ERROR_IS_OK(werr)) {
489                 return werr;
490         }
491
492         split_domain_user(p->mem_ctx,
493                           r->in.account,
494                           &admin_domain,
495                           &admin_account);
496
497         werr = libnet_init_UnjoinCtx(p->mem_ctx, &u);
498         if (!W_ERROR_IS_OK(werr)) {
499                 return werr;
500         }
501
502         u->in.domain_name       = lp_realm();
503         u->in.unjoin_flags      = r->in.unjoin_flags |
504                                   WKSSVC_JOIN_FLAGS_JOIN_TYPE;
505         u->in.admin_account     = admin_account;
506         u->in.admin_password    = cleartext_pwd;
507         u->in.debug             = true;
508         u->in.modify_config     = lp_config_backend_is_registry();
509         u->in.msg_ctx           = smbd_messaging_context();
510
511         become_root();
512         werr = libnet_Unjoin(p->mem_ctx, u);
513         unbecome_root();
514
515         if (!W_ERROR_IS_OK(werr)) {
516                 DEBUG(5,("_wkssvc_NetrUnjoinDomain2: libnet_Unjoin failed with: %s\n",
517                         u->out.error_string ? u->out.error_string :
518                         win_errstr(werr)));
519         }
520
521         TALLOC_FREE(u);
522         return werr;
523 }
524
525 /********************************************************************
526  ********************************************************************/
527
528 WERROR _wkssvc_NetrRenameMachineInDomain2(pipes_struct *p, struct wkssvc_NetrRenameMachineInDomain2 *r)
529 {
530         /* for now just return not supported */
531         return WERR_NOT_SUPPORTED;
532 }
533
534 /********************************************************************
535  ********************************************************************/
536
537 WERROR _wkssvc_NetrValidateName2(pipes_struct *p, struct wkssvc_NetrValidateName2 *r)
538 {
539         /* FIXME: Add implementation code here */
540         p->rng_fault_state = True;
541         return WERR_NOT_SUPPORTED;
542 }
543
544 /********************************************************************
545  ********************************************************************/
546
547 WERROR _wkssvc_NetrGetJoinableOus2(pipes_struct *p, struct wkssvc_NetrGetJoinableOus2 *r)
548 {
549         /* FIXME: Add implementation code here */
550         p->rng_fault_state = True;
551         return WERR_NOT_SUPPORTED;
552 }
553
554 /********************************************************************
555  ********************************************************************/
556
557 WERROR _wkssvc_NetrAddAlternateComputerName(pipes_struct *p, struct wkssvc_NetrAddAlternateComputerName *r)
558 {
559         /* FIXME: Add implementation code here */
560         p->rng_fault_state = True;
561         return WERR_NOT_SUPPORTED;
562 }
563
564 /********************************************************************
565  ********************************************************************/
566
567 WERROR _wkssvc_NetrRemoveAlternateComputerName(pipes_struct *p, struct wkssvc_NetrRemoveAlternateComputerName *r)
568 {
569         /* FIXME: Add implementation code here */
570         p->rng_fault_state = True;
571         return WERR_NOT_SUPPORTED;
572 }
573
574 /********************************************************************
575  ********************************************************************/
576
577 WERROR _wkssvc_NetrSetPrimaryComputername(pipes_struct *p, struct wkssvc_NetrSetPrimaryComputername *r)
578 {
579         /* FIXME: Add implementation code here */
580         p->rng_fault_state = True;
581         return WERR_NOT_SUPPORTED;
582 }
583
584 /********************************************************************
585  ********************************************************************/
586
587 WERROR _wkssvc_NetrEnumerateComputerNames(pipes_struct *p, struct wkssvc_NetrEnumerateComputerNames *r)
588 {
589         /* FIXME: Add implementation code here */
590         p->rng_fault_state = True;
591         return WERR_NOT_SUPPORTED;
592 }
593