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
32 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
34 d_printf("net_password_change_usage: TODO\n");
39 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
42 struct libnet_context *libnetctx;
43 union libnet_ChangePassword r;
44 char *password_prompt = NULL;
45 const char *new_password;
47 if (argc > 0 && argv[0]) {
48 new_password = argv[0];
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);
55 libnetctx = libnet_context_init();
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;
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;
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));
77 libnet_context_destroy(&libnetctx);
83 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
85 d_printf("net_password_set_usage: TODO\n");
90 static int net_password_set(struct net_context *ctx, int argc, const char **argv)
93 struct libnet_context *libnetctx;
94 union libnet_SetPassword r;
95 char *password_prompt = NULL;
98 const char *account_name;
99 const char *domain_name;
100 const char *new_password = NULL;
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]);
108 case 2: /* DOM\\USER and password */
109 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
110 new_password = argv[1];
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);
117 if ((p = strchr_m(tmp,'\\'))) {
120 account_name = talloc_strdup(ctx->mem_ctx, p+1);
123 domain_name = ctx->user.domain_name;
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);
132 libnetctx = libnet_context_init();
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;
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;
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));
153 libnet_context_destroy(&libnetctx);
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 },
165 int net_password(struct net_context *ctx, int argc, const char **argv)
167 return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
170 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
172 d_printf("net password <command> [options]\n");