r23792: convert Samba4 to GPLv3
[jelmer/samba4-debian.git] / source / 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 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 "system/filesys.h"
25 #include "auth/credentials/credentials.h"
26
27 /*
28  * Code for Changing and setting a password
29  */
30
31 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
32 {
33         d_printf("net_password_change_usage: TODO\n");
34         return 0;       
35 }
36
37
38 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
39 {
40         NTSTATUS status;
41         struct libnet_context *libnetctx;
42         union libnet_ChangePassword r;
43         char *password_prompt = NULL;
44         const char *new_password;
45
46         if (argc > 0 && argv[0]) {
47                 new_password = argv[0];
48         } else {
49                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
50                                                         cli_credentials_get_domain(ctx->credentials), 
51                                                         cli_credentials_get_username(ctx->credentials));
52                 new_password = getpass(password_prompt);
53         }
54
55         libnetctx = libnet_context_init(NULL);
56         if (!libnetctx) {
57                 return -1;      
58         }
59         libnetctx->cred = ctx->credentials;
60
61         /* prepare password change */
62         r.generic.level                 = LIBNET_CHANGE_PASSWORD_GENERIC;
63         r.generic.in.account_name       = cli_credentials_get_username(ctx->credentials);
64         r.generic.in.domain_name        = cli_credentials_get_domain(ctx->credentials);
65         r.generic.in.oldpassword        = cli_credentials_get_password(ctx->credentials);
66         r.generic.in.newpassword        = new_password;
67
68         /* do password change */
69         status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
70         if (!NT_STATUS_IS_OK(status)) {
71                 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
72                 return -1;
73         }
74
75         talloc_free(libnetctx);
76
77         return 0;
78 }
79
80
81 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
82 {
83         d_printf("net_password_set_usage: TODO\n");
84         return 0;       
85 }
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                         return net_password_set_usage(ctx, argc, argv);
103                 case 1: /* only DOM\\user; prompt for password */
104                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
105                         break;
106                 case 2: /* DOM\\USER and password */
107                         tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
108                         new_password = argv[1];
109                         break;
110                 default: /* too mayn args -> fail */
111                         DEBUG(0,("net_password_set: too many args [%d]\n",argc));
112                         return net_password_usage(ctx, argc, argv);
113         }
114
115         if ((p = strchr_m(tmp,'\\'))) {
116                 *p = 0;
117                 domain_name = tmp;
118                 account_name = talloc_strdup(ctx->mem_ctx, p+1);
119         } else {
120                 account_name = tmp;
121                 domain_name = cli_credentials_get_domain(ctx->credentials);
122         }
123
124         if (!new_password) {
125                 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:", 
126                                                         domain_name, account_name);
127                 new_password = getpass(password_prompt);
128         }
129
130         libnetctx = libnet_context_init(NULL);
131         if (!libnetctx) {
132                 return -1;      
133         }
134         libnetctx->cred = ctx->credentials;
135
136         /* prepare password change */
137         r.generic.level                 = LIBNET_SET_PASSWORD_GENERIC;
138         r.generic.in.account_name       = account_name;
139         r.generic.in.domain_name        = domain_name;
140         r.generic.in.newpassword        = new_password;
141
142         /* do password change */
143         status = libnet_SetPassword(libnetctx, ctx->mem_ctx, &r);
144         if (!NT_STATUS_IS_OK(status)) {
145                 DEBUG(0,("net_password_set: %s\n",r.generic.out.error_string));
146                 return -1;
147         }
148
149         talloc_free(libnetctx);
150
151         return 0;
152 }
153
154
155 static const struct net_functable net_password_functable[] = {
156         {"change", "change password (old password required)\n", net_password_change, net_password_change_usage },
157         {"set", "set password\n", net_password_set, net_password_set_usage },
158         {NULL, NULL}
159 };
160
161 int net_password(struct net_context *ctx, int argc, const char **argv) 
162 {
163         return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
164 }
165
166 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
167 {
168         d_printf("net password <command> [options]\n");
169         return 0;       
170 }