r23792: convert Samba4 to GPLv3
[ira/wip.git] / source4 / utils / net / net_user.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4
5    Copyright (C) Rafal Szczesniak <mimir@samba.org>  2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "utils/net/net.h"
23 #include "libnet/libnet.h"
24 #include "auth/credentials/credentials.h"
25
26 static int net_user_add(struct net_context *ctx, int argc, const char **argv)
27 {
28         NTSTATUS status;
29         struct libnet_context *lnet_ctx;
30         struct libnet_CreateUser r;
31         char *user_name;
32
33         /* command line argument preparation */
34         switch (argc) {
35         case 0:
36                 return net_user_usage(ctx, argc, argv);
37                 break;
38         case 1:
39                 user_name = talloc_strdup(ctx->mem_ctx, argv[0]);
40                 break;
41         default:
42                 return net_user_usage(ctx, argc, argv);
43         }
44
45         /* libnet context init and its params */
46         lnet_ctx = libnet_context_init(NULL);
47         if (!lnet_ctx) return -1;
48
49         lnet_ctx->cred = ctx->credentials;
50
51         /* calling CreateUser function */
52         r.in.user_name       = user_name;
53         r.in.domain_name     = cli_credentials_get_domain(lnet_ctx->cred);
54
55         status = libnet_CreateUser(lnet_ctx, ctx->mem_ctx, &r);
56         if (!NT_STATUS_IS_OK(status)) {
57                 DEBUG(0, ("Failed to add user account: %s\n",
58                           r.out.error_string));
59                 return -1;
60         }
61         
62         talloc_free(lnet_ctx);
63         return 0;
64 }
65
66 static int net_user_delete(struct net_context *ctx, int argc, const char **argv)
67 {
68         NTSTATUS status;
69         struct libnet_context *lnet_ctx;
70         struct libnet_DeleteUser r;
71         char *user_name;
72
73         /* command line argument preparation */
74         switch (argc) {
75         case 0:
76                 return net_user_usage(ctx, argc, argv);
77                 break;
78         case 1:
79                 user_name = talloc_strdup(ctx->mem_ctx, argv[0]);
80                 break;
81         default:
82                 return net_user_usage(ctx, argc, argv);
83         }
84
85         /* libnet context init and its params */
86         lnet_ctx = libnet_context_init(NULL);
87         if (!lnet_ctx) return -1;
88
89         lnet_ctx->cred = ctx->credentials;
90
91         /* calling DeleteUser function */
92         r.in.user_name       = user_name;
93         r.in.domain_name     = cli_credentials_get_domain(lnet_ctx->cred);
94
95         status = libnet_DeleteUser(lnet_ctx, ctx->mem_ctx, &r);
96         if (!NT_STATUS_IS_OK(status)) {
97                 DEBUG(0, ("Failed to delete user account: %s\n",
98                           r.out.error_string));
99                 return -1;
100         }
101         
102         talloc_free(lnet_ctx);
103         return 0;
104 }
105
106
107 static const struct net_functable net_user_functable[] = {
108         { "add", "create new user account\n", net_user_add, net_user_usage },
109         { "delete", "delete an existing user account\n", net_user_delete, net_user_usage },
110         { NULL, NULL }
111 };
112
113
114 int net_user(struct net_context *ctx, int argc, const char **argv)
115 {
116         return net_run_function(ctx, argc, argv, net_user_functable, net_user_usage);
117 }
118
119
120 int net_user_usage(struct net_context *ctx, int argc, const char **argv)
121 {
122         d_printf("net user <command> [options]\n");
123         return 0;
124 }