r1919: paasword change basicly works now:-)
[samba.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
24 /*
25  * Code for Changing and setting a password
26  */
27
28
29 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
30 {
31         NTSTATUS status;
32         struct libnet_context *libnetctx;
33         union libnet_ChangePassword r;
34         char *password_prompt = NULL;
35         const char *new_password;
36
37         if (argc > 0 && argv[0]) {
38                 new_password = argv[0];
39         } else {
40                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
41                                                         ctx->user.domain_name, ctx->user.account_name);
42                 new_password = getpass(password_prompt);
43         }
44
45         libnetctx = libnet_context_init();
46         if (!libnetctx) {
47                 return -1;      
48         }
49         libnetctx->user.account_name    = ctx->user.account_name;
50         libnetctx->user.domain_name     = ctx->user.domain_name;
51         libnetctx->user.password        = ctx->user.password;
52
53         /* prepare password change */
54         r.generic.level                 = LIBNET_CHANGE_PASSWORD_GENERIC;
55         r.generic.in.account_name       = ctx->user.account_name;
56         r.generic.in.domain_name        = ctx->user.domain_name;
57         r.generic.in.oldpassword        = ctx->user.password;
58         r.generic.in.newpassword        = new_password;
59
60         /* do password change */
61         status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
62         if (!NT_STATUS_IS_OK(status)) {
63                 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
64                 return -1;
65         }
66
67         libnet_context_destroy(&libnetctx);
68
69         return 0;
70 }
71
72 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
73 {
74         d_printf("net_password_change_usage: TODO\n");
75         return 0;       
76 }
77
78 static int net_password_change_help(struct net_context *ctx, int argc, const char **argv)
79 {
80         d_printf("net_password_change_help: TODO\n");
81         return 0;       
82 }
83
84 static const struct net_functable const net_password_functable[] = {
85         {"change", net_password_change, net_password_change_usage,  net_password_change_help},
86         {NULL, NULL}
87 };
88
89 int net_password(struct net_context *ctx, int argc, const char **argv) 
90 {
91         return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
92 }
93
94 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
95 {
96         d_printf("net_password_usage: TODO\n");
97         return 0;       
98 }
99
100 int net_password_help(struct net_context *ctx, int argc, const char **argv)
101 {
102         d_printf("net_password_help: TODO\n");
103         return 0;       
104 }