7bb6fa3d61fb14c39ca352df1ac425e31e7ab406
[samba.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "auth/credentials/credentials.h"
26
27 static int net_user_add(struct net_context *ctx, int argc, const char **argv)
28 {
29         NTSTATUS status;
30         struct libnet_context *lnet_ctx;
31         struct libnet_CreateUser r;
32         char *user_name;
33
34         /* command line argument preparation */
35         switch (argc) {
36         case 0:
37                 return net_user_usage(ctx, argc, argv);
38                 break;
39         case 1:
40                 user_name = talloc_strdup(ctx->mem_ctx, argv[0]);
41                 break;
42         default:
43                 return net_user_usage(ctx, argc, argv);
44         }
45
46         /* libnet context init and its params */
47         lnet_ctx = libnet_context_init(NULL);
48         if (!lnet_ctx) return -1;
49
50         lnet_ctx->cred = ctx->credentials;
51
52         /* calling CreateUser function */
53         r.in.user_name       = user_name;
54         r.in.domain_name     = cli_credentials_get_domain(lnet_ctx->cred);
55
56         status = libnet_CreateUser(lnet_ctx, ctx->mem_ctx, &r);
57         if (!NT_STATUS_IS_OK(status)) {
58                 DEBUG(0, ("Failed to add user account: %s\n",
59                           r.out.error_string));
60                 return -1;
61         }
62         
63         talloc_free(lnet_ctx);
64         return 0;
65 }
66
67 static int net_user_delete(struct net_context *ctx, int argc, const char **argv)
68 {
69         NTSTATUS status;
70         struct libnet_context *lnet_ctx;
71         struct libnet_DeleteUser r;
72         char *user_name;
73
74         /* command line argument preparation */
75         switch (argc) {
76         case 0:
77                 return net_user_usage(ctx, argc, argv);
78                 break;
79         case 1:
80                 user_name = talloc_strdup(ctx->mem_ctx, argv[0]);
81                 break;
82         default:
83                 return net_user_usage(ctx, argc, argv);
84         }
85
86         /* libnet context init and its params */
87         lnet_ctx = libnet_context_init(NULL);
88         if (!lnet_ctx) return -1;
89
90         lnet_ctx->cred = ctx->credentials;
91
92         /* calling DeleteUser function */
93         r.in.user_name       = user_name;
94         r.in.domain_name     = cli_credentials_get_domain(lnet_ctx->cred);
95
96         status = libnet_DeleteUser(lnet_ctx, ctx->mem_ctx, &r);
97         if (!NT_STATUS_IS_OK(status)) {
98                 DEBUG(0, ("Failed to delete user account: %s\n",
99                           r.out.error_string));
100                 return -1;
101         }
102         
103         talloc_free(lnet_ctx);
104         return 0;
105 }
106
107
108 static const struct net_functable net_user_functable[] = {
109         { "add", "create new user account\n", net_user_add, net_user_usage },
110         { "delete", "delete an existing user account\n", net_user_delete, net_user_usage },
111         { NULL, NULL }
112 };
113
114
115 int net_user(struct net_context *ctx, int argc, const char **argv)
116 {
117         return net_run_function(ctx, argc, argv, net_user_functable, net_user_usage);
118 }
119
120
121 int net_user_usage(struct net_context *ctx, int argc, const char **argv)
122 {
123         d_printf("net user <command> [options]\n");
124         return 0;
125 }