r12892: Add a 'Migrate from Windows' page to our installation section in SWAT.
[jra/samba/.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
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 }
78
79 NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_DeleteUser *r)
80 {
81         NTSTATUS status;
82         struct libnet_RpcConnect cn;
83         struct libnet_rpc_domain_open dom_io;
84         struct libnet_rpc_userdel user_io;
85         
86         /* connect rpc service of remote DC */
87         cn.level               = LIBNET_RPC_CONNECT_PDC;
88         cn.in.name             = talloc_strdup(mem_ctx, r->in.domain_name);
89         cn.in.dcerpc_iface     = &dcerpc_table_samr;
90
91         status = libnet_RpcConnect(ctx, mem_ctx, &cn);
92         if (!NT_STATUS_IS_OK(status)) {
93                 r->out.error_string = talloc_asprintf(mem_ctx,
94                                                       "Connection to SAMR pipe domain '%s' PDC failed: %s\n",
95                                                       r->in.domain_name, nt_errstr(status));
96                 return status;
97         }
98
99         ctx->pipe = cn.out.dcerpc_pipe;
100
101         /* open connected domain */
102         dom_io.in.domain_name   = r->in.domain_name;
103         dom_io.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
104         
105         status = libnet_rpc_domain_open(ctx->pipe, mem_ctx, &dom_io);
106         if (!NT_STATUS_IS_OK(status)) {
107                 r->out.error_string = talloc_asprintf(mem_ctx,
108                                                       "Opening domain to delete user account failed: %s\n",
109                                                       nt_errstr(status));
110                 return status;
111         }
112
113         ctx->domain_handle = dom_io.out.domain_handle;
114
115         /* create user */
116         user_io.in.username       = r->in.user_name;
117         user_io.in.domain_handle  = dom_io.out.domain_handle;
118
119         status = libnet_rpc_userdel(ctx->pipe, mem_ctx, &user_io);
120         if (!NT_STATUS_IS_OK(status)) {
121                 r->out.error_string = talloc_asprintf(mem_ctx,
122                                                       "Deleting user account failed: %s\n",
123                                                       nt_errstr(status));
124                 return status;
125         }
126
127         return status;
128 }