r5917: First step in using the new cli_credentials structure. This patch
[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 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
33 {
34         d_printf("net_password_change_usage: TODO\n");
35         return 0;       
36 }
37
38
39 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
40 {
41         NTSTATUS status;
42         struct libnet_context *libnetctx;
43         union libnet_ChangePassword r;
44         char *password_prompt = NULL;
45         const char *new_password;
46
47         if (argc > 0 && argv[0]) {
48                 new_password = argv[0];
49         } else {
50                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
51                                                         cli_credentials_get_domain(ctx->credentials), 
52                                                         cli_credentials_get_username(ctx->credentials));
53                 new_password = getpass(password_prompt);
54         }
55
56         libnetctx = libnet_context_init();
57         if (!libnetctx) {
58                 return -1;      
59         }
60         libnetctx->user.account_name= cli_credentials_get_username(ctx->credentials);
61         libnetctx->user.domain_name     = cli_credentials_get_domain(ctx->credentials);
62         libnetctx->user.password        = cli_credentials_get_password(ctx->credentials);
63
64         /* prepare password change */
65         r.generic.level                 = LIBNET_CHANGE_PASSWORD_GENERIC;
66         r.generic.in.account_name       = cli_credentials_get_username(ctx->credentials);
67         r.generic.in.domain_name        = cli_credentials_get_domain(ctx->credentials);
68         r.generic.in.oldpassword        = cli_credentials_get_password(ctx->credentials);
69         r.generic.in.newpassword        = new_password;
70
71         /* do password change */
72         status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
73         if (!NT_STATUS_IS_OK(status)) {
74                 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
75                 return -1;
76         }
77
78         libnet_context_destroy(&libnetctx);
79
80         return 0;
81 }
82
83
84 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
85 {
86         d_printf("net_password_set_usage: TODO\n");
87         return 0;       
88 }
89
90
91 static int net_password_set(struct net_context *ctx, int argc, const char **argv)
92 {
93         NTSTATUS status;
94         struct libnet_context *libnetctx;
95         union libnet_SetPassword r;
96         char *password_prompt = NULL;
97         char *p;
98         char *tmp;
99         const char *account_name;
100         const char *domain_name;
101         const char *new_password = NULL;
102
103         switch (argc) {
104                 case 0: /* no args -> fail */
105                         return net_password_set_usage(ctx, argc, argv);
106                 case 1: /* only DOM\\user; prompt for password */
107                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
108                         break;
109                 case 2: /* DOM\\USER and password */
110                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
111                         new_password = argv[1];
112                         break;
113                 default: /* too mayn args -> fail */
114                         DEBUG(0,("net_password_set: too many args [%d]\n",argc));
115                         return net_password_usage(ctx, argc, argv);
116         }
117
118         if ((p = strchr_m(tmp,'\\'))) {
119                 *p = 0;
120                 domain_name = tmp;
121                 account_name = talloc_strdup(ctx->mem_ctx, p+1);
122         } else {
123                 account_name = tmp;
124                 domain_name = cli_credentials_get_domain(ctx->credentials);
125         }
126
127         if (!new_password) {
128                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
129                                                         domain_name, account_name);
130                 new_password = getpass(password_prompt);
131         }
132
133         libnetctx = libnet_context_init();
134         if (!libnetctx) {
135                 return -1;      
136         }
137         libnetctx->user.account_name= cli_credentials_get_username(ctx->credentials);
138         libnetctx->user.domain_name     = cli_credentials_get_domain(ctx->credentials);
139         libnetctx->user.password        = cli_credentials_get_password(ctx->credentials);
140
141         /* prepare password change */
142         r.generic.level                 = LIBNET_SET_PASSWORD_GENERIC;
143         r.generic.in.account_name       = account_name;
144         r.generic.in.domain_name        = domain_name;
145         r.generic.in.newpassword        = new_password;
146
147         /* do password change */
148         status = libnet_SetPassword(libnetctx, ctx->mem_ctx, &r);
149         if (!NT_STATUS_IS_OK(status)) {
150                 DEBUG(0,("net_password_set: %s\n",r.generic.out.error_string));
151                 return -1;
152         }
153
154         libnet_context_destroy(&libnetctx);
155
156         return 0;
157 }
158
159
160 static const struct net_functable net_password_functable[] = {
161         {"change", "change password (old password required)\n", net_password_change, net_password_change_usage },
162         {"set", "set password\n", net_password_set, net_password_set_usage },
163         {NULL, NULL}
164 };
165
166 int net_password(struct net_context *ctx, int argc, const char **argv) 
167 {
168         return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
169 }
170
171 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
172 {
173         d_printf("net password <command> [options]\n");
174         return 0;       
175 }