773e18d6610f74131c7bcaef76a56fdc71f942ea
[bbaumbach/samba-autobuild/.git] / source4 / utils / net / net_password.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4
5    Copyright (C) 2004 Stefan Metzmacher (metze@samba.org)
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 "system/filesys.h"
26 #include "system/passwd.h"
27
28 /*
29  * Code for Changing and setting a password
30  */
31
32
33 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
34 {
35         NTSTATUS status;
36         struct libnet_context *libnetctx;
37         union libnet_ChangePassword r;
38         char *password_prompt = NULL;
39         const char *new_password;
40
41         if (argc > 0 && argv[0]) {
42                 new_password = argv[0];
43         } else {
44                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
45                                                         ctx->user.domain_name, ctx->user.account_name);
46                 new_password = getpass(password_prompt);
47         }
48
49         libnetctx = libnet_context_init();
50         if (!libnetctx) {
51                 return -1;      
52         }
53         libnetctx->user.account_name    = ctx->user.account_name;
54         libnetctx->user.domain_name     = ctx->user.domain_name;
55         libnetctx->user.password        = ctx->user.password;
56
57         /* prepare password change */
58         r.generic.level                 = LIBNET_CHANGE_PASSWORD_GENERIC;
59         r.generic.in.account_name       = ctx->user.account_name;
60         r.generic.in.domain_name        = ctx->user.domain_name;
61         r.generic.in.oldpassword        = ctx->user.password;
62         r.generic.in.newpassword        = new_password;
63
64         /* do password change */
65         status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
66         if (!NT_STATUS_IS_OK(status)) {
67                 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
68                 return -1;
69         }
70
71         libnet_context_destroy(&libnetctx);
72
73         return 0;
74 }
75
76 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
77 {
78         d_printf("net_password_change_usage: TODO\n");
79         return 0;       
80 }
81
82 static int net_password_change_help(struct net_context *ctx, int argc, const char **argv)
83 {
84         d_printf("net_password_change_help: TODO\n");
85         return 0;       
86 }
87
88 static int net_password_set(struct net_context *ctx, int argc, const char **argv)
89 {
90         NTSTATUS status;
91         struct libnet_context *libnetctx;
92         union libnet_SetPassword r;
93         char *password_prompt = NULL;
94         char *p;
95         char *tmp;
96         const char *account_name;
97         const char *domain_name;
98         const char *new_password = NULL;
99
100         switch (argc) {
101                 case 0: /* no args -> fail */
102                         DEBUG(0,("net_password_set: no args\n"));
103                         return -1;
104                 case 1: /* only DOM\\user; prompt for password */
105                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
106                         break;
107                 case 2: /* DOM\\USER and password */
108                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
109                         new_password = argv[1];
110                         break;
111                 default: /* too mayn args -> fail */
112                         DEBUG(0,("net_password_set: too many args [%d]\n",argc));
113                         return -1;
114         }
115
116         if ((p = strchr_m(tmp,'\\'))) {
117                 *p = 0;
118                 domain_name = tmp;
119                 account_name = talloc_strdup(ctx->mem_ctx, p+1);
120         } else {
121                 account_name = tmp;
122                 domain_name = ctx->user.domain_name;
123         }
124
125         if (!new_password) {
126                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
127                                                         domain_name, account_name);
128                 new_password = getpass(password_prompt);
129         }
130
131         libnetctx = libnet_context_init();
132         if (!libnetctx) {
133                 return -1;      
134         }
135         libnetctx->user.account_name    = ctx->user.account_name;
136         libnetctx->user.domain_name     = ctx->user.domain_name;
137         libnetctx->user.password        = ctx->user.password;
138
139         /* prepare password change */
140         r.generic.level                 = LIBNET_SET_PASSWORD_GENERIC;
141         r.generic.in.account_name       = account_name;
142         r.generic.in.domain_name        = domain_name;
143         r.generic.in.newpassword        = new_password;
144
145         /* do password change */
146         status = libnet_SetPassword(libnetctx, ctx->mem_ctx, &r);
147         if (!NT_STATUS_IS_OK(status)) {
148                 DEBUG(0,("net_password_set: %s\n",r.generic.out.error_string));
149                 return -1;
150         }
151
152         libnet_context_destroy(&libnetctx);
153
154         return 0;
155 }
156
157 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
158 {
159         d_printf("net_password_set_usage: TODO\n");
160         return 0;       
161 }
162
163 static int net_password_set_help(struct net_context *ctx, int argc, const char **argv)
164 {
165         d_printf("net_password_set_help: TODO\n");
166         return 0;       
167 }
168
169 static const struct net_functable net_password_functable[] = {
170         {"change", net_password_change, net_password_change_usage,  net_password_change_help},
171         {"set", net_password_set, net_password_set_usage,  net_password_set_help},
172         {NULL, NULL}
173 };
174
175 int net_password(struct net_context *ctx, int argc, const char **argv) 
176 {
177         return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
178 }
179
180 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
181 {
182         d_printf("net_password_usage: TODO\n");
183         return 0;       
184 }
185
186 int net_password_help(struct net_context *ctx, int argc, const char **argv)
187 {
188         d_printf("net_password_help: TODO\n");
189         return 0;       
190 }