50cb14d2907d5d9266fb775f4a33c8d76c8669dc
[samba.git] / source / 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
25
26 NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_CreateUser *r)
27 {
28         NTSTATUS status;
29         struct libnet_RpcConnect cn;
30         struct libnet_rpc_domain_open dom_io;
31         struct libnet_rpc_useradd user_io;
32         
33         /* connect rpc service of remote DC */
34         cn.level               = LIBNET_RPC_CONNECT_PDC;
35         cn.in.name             = talloc_strdup(mem_ctx, r->in.domain_name);
36         cn.in.dcerpc_iface     = &dcerpc_table_samr;
37
38         status = libnet_RpcConnect(ctx, mem_ctx, &cn);
39         if (!NT_STATUS_IS_OK(status)) {
40                 r->out.error_string = talloc_asprintf(mem_ctx,
41                                                       "Connection to SAMR pipe domain '%s' PDC failed: %s\n",
42                                                       r->in.domain_name, nt_errstr(status));
43                 return status;
44         }
45
46         ctx->pipe = cn.out.dcerpc_pipe;
47
48         /* open connected domain */
49         dom_io.in.domain_name   = r->in.domain_name;
50         dom_io.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
51         
52         status = libnet_rpc_domain_open(ctx->pipe, mem_ctx, &dom_io);
53         if (!NT_STATUS_IS_OK(status)) {
54                 r->out.error_string = talloc_asprintf(mem_ctx,
55                                                       "Creating user account failed: %s\n",
56                                                       nt_errstr(status));
57                 return status;
58         }
59
60         ctx->domain_handle = dom_io.out.domain_handle;
61
62         /* create user */
63         user_io.in.username       = r->in.user_name;
64         user_io.in.domain_handle  = dom_io.out.domain_handle;
65
66         status = libnet_rpc_useradd(ctx->pipe, mem_ctx, &user_io);
67         if (!NT_STATUS_IS_OK(status)) {
68                 r->out.error_string = talloc_asprintf(mem_ctx,
69                                                       "Creating user account failed: %s\n",
70                                                       nt_errstr(status));
71                 return status;
72         }
73
74         ctx->user_handle = user_io.out.user_handle;
75
76         return status;
77 }