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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "system/filesys.h"
26 #include "system/passwd.h"
29 * Code for Changing and setting a password
33 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
36 struct libnet_context *libnetctx;
37 union libnet_ChangePassword r;
38 char *password_prompt = NULL;
39 const char *new_password;
41 if (argc > 0 && argv[0]) {
42 new_password = argv[0];
44 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:",
45 ctx->user.domain_name, ctx->user.account_name);
46 new_password = getpass(password_prompt);
49 libnetctx = libnet_context_init();
53 libnetctx->user.account_name = ctx->user.account_name;
54 libnetctx->user.domain_name = ctx->user.domain_name;
55 libnetctx->user.password = ctx->user.password;
57 /* prepare password change */
58 r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
59 r.generic.in.account_name = ctx->user.account_name;
60 r.generic.in.domain_name = ctx->user.domain_name;
61 r.generic.in.oldpassword = ctx->user.password;
62 r.generic.in.newpassword = new_password;
64 /* do password change */
65 status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
71 libnet_context_destroy(&libnetctx);
76 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
78 d_printf("net_password_change_usage: TODO\n");
82 static int net_password_change_help(struct net_context *ctx, int argc, const char **argv)
84 d_printf("net_password_change_help: 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 DEBUG(0,("net_password_set: no args\n"));
104 case 1: /* only DOM\\user; prompt for password */
105 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
107 case 2: /* DOM\\USER and password */
108 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
109 new_password = argv[1];
111 default: /* too mayn args -> fail */
112 DEBUG(0,("net_password_set: too many args [%d]\n",argc));
116 if ((p = strchr_m(tmp,'\\'))) {
119 account_name = talloc_strdup(ctx->mem_ctx, p+1);
122 domain_name = ctx->user.domain_name;
126 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:",
127 domain_name, account_name);
128 new_password = getpass(password_prompt);
131 libnetctx = libnet_context_init();
135 libnetctx->user.account_name = ctx->user.account_name;
136 libnetctx->user.domain_name = ctx->user.domain_name;
137 libnetctx->user.password = ctx->user.password;
139 /* prepare password change */
140 r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
141 r.generic.in.account_name = account_name;
142 r.generic.in.domain_name = domain_name;
143 r.generic.in.newpassword = new_password;
145 /* do password change */
146 status = libnet_SetPassword(libnetctx, ctx->mem_ctx, &r);
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(0,("net_password_set: %s\n",r.generic.out.error_string));
152 libnet_context_destroy(&libnetctx);
157 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
159 d_printf("net_password_set_usage: TODO\n");
163 static int net_password_set_help(struct net_context *ctx, int argc, const char **argv)
165 d_printf("net_password_set_help: TODO\n");
169 static const struct net_functable net_password_functable[] = {
170 {"change", net_password_change, net_password_change_usage, net_password_change_help},
171 {"set", net_password_set, net_password_set_usage, net_password_set_help},
175 int net_password(struct net_context *ctx, int argc, const char **argv)
177 return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
180 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
182 d_printf("net_password_usage: TODO\n");
186 int net_password_help(struct net_context *ctx, int argc, const char **argv)
188 d_printf("net_password_help: TODO\n");