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