r7380: Mistakenly put one file twice in command line. Here's source of
[bbaumbach/samba-autobuild/.git] / source4 / libnet / libnet_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Rafal Szczesniak <mimir@samba.org> 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 #include "includes.h"
23 #include "libnet/libnet.h"
24 #include "libnet/composite.h"
25 #include "librpc/gen_ndr/ndr_samr.h"
26
27
28 NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_CreateUser *r)
29 {
30         NTSTATUS status;
31         union libnet_rpc_connect cn;
32         union libnet_find_pdc fp;
33         struct dcerpc_pipe *pipe;
34         struct rpc_composite_domain_open dom_io;
35         struct rpc_composite_useradd user_io;
36         
37         /* find domain pdc */
38         fp.generic.level             = LIBNET_FIND_PDC_GENERIC;
39         fp.generic.in.domain_name    = r->in.domain_name;
40
41         status = libnet_find_pdc(ctx, mem_ctx, &fp);
42         if (!NT_STATUS_IS_OK(status)) return status;
43
44         /* connect rpc service of remote server */
45         cn.standard.level                      = LIBNET_RPC_CONNECT_STANDARD;
46         cn.standard.in.server_name             = fp.generic.out.pdc_name;
47         cn.standard.in.dcerpc_iface_name       = DCERPC_SAMR_NAME;
48         cn.standard.in.dcerpc_iface_uuid       = DCERPC_SAMR_UUID;
49         cn.standard.in.dcerpc_iface_version    = DCERPC_SAMR_VERSION;
50
51         status = libnet_rpc_connect(ctx, mem_ctx, &cn);
52         if (!NT_STATUS_IS_OK(status)) {
53                 r->out.error_string = talloc_asprintf(mem_ctx,
54                                                       "Connection to SAMR pipe domain '%s' PDC failed: %s\n",
55                                                       r->in.domain_name, nt_errstr(status));
56                 return status;
57         }
58
59         ctx->samr = cn.pdc.out.dcerpc_pipe;
60
61         /* open connected domain */
62         dom_io.in.domain_name   = r->in.domain_name;
63         dom_io.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
64         
65         status = rpc_composite_domain_open(ctx->samr, mem_ctx, &dom_io);
66         if (!NT_STATUS_IS_OK(status)) {
67                 r->out.error_string = talloc_asprintf(mem_ctx,
68                                                       "Creating user account failed: %s\n",
69                                                       nt_errstr(status));
70                 return status;
71         }
72
73         ctx->domain_handle = dom_io.out.domain_handle;
74
75         /* create user */
76         user_io.in.username       = r->in.user_name;
77         user_io.in.domain_handle  = dom_io.out.domain_handle;
78
79         status = rpc_composite_useradd(ctx->samr, mem_ctx, &user_io);
80         if (!NT_STATUS_IS_OK(status)) {
81                 r->out.error_string = talloc_asprintf(mem_ctx,
82                                                       "Creating user account failed: %s\n",
83                                                       nt_errstr(status));
84                 return status;
85         }
86
87         ctx->user_handle = user_io.out.user_handle;
88
89         return status;
90 }