2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) 2004 Stefan Metzmacher (metze@samba.org)
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.
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.
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/>.
22 #include "utils/net/net.h"
23 #include "libnet/libnet.h"
24 #include "system/filesys.h"
25 #include "auth/credentials/credentials.h"
28 * Code for Changing and setting a password
31 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
33 d_printf("net_password_change_usage: TODO\n");
38 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
41 struct libnet_context *libnetctx;
42 union libnet_ChangePassword r;
43 char *password_prompt = NULL;
44 const char *new_password;
46 if (argc > 0 && argv[0]) {
47 new_password = argv[0];
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);
55 libnetctx = libnet_context_init(NULL);
59 libnetctx->cred = ctx->credentials;
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;
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));
75 talloc_free(libnetctx);
81 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
83 d_printf("net_password_set_usage: TODO\n");
88 static int net_password_set(struct net_context *ctx, int argc, const char **argv)
91 struct libnet_context *libnetctx;
92 union libnet_SetPassword r;
93 char *password_prompt = NULL;
96 const char *account_name;
97 const char *domain_name;
98 const char *new_password = NULL;
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]);
106 case 2: /* DOM\\USER and password */
107 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
108 new_password = argv[1];
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);
115 if ((p = strchr_m(tmp,'\\'))) {
118 account_name = talloc_strdup(ctx->mem_ctx, p+1);
121 domain_name = cli_credentials_get_domain(ctx->credentials);
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);
130 libnetctx = libnet_context_init(NULL);
134 libnetctx->cred = ctx->credentials;
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;
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));
149 talloc_free(libnetctx);
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 },
161 int net_password(struct net_context *ctx, int argc, const char **argv)
163 return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
166 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
168 d_printf("net password <command> [options]\n");