r5400: Slightly better handling of help messages in net tool.
[Samba/gebeck_regimport.git] / source4 / utils / net / net_password.c
blobfdc1a2e9a093959423fd5bf3801142f24ac8724f
1 /*
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.
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"
29 * Code for Changing and setting a password
32 static int net_password_change(struct net_context *ctx, int argc, const char **argv)
34 NTSTATUS status;
35 struct libnet_context *libnetctx;
36 union libnet_ChangePassword r;
37 char *password_prompt = NULL;
38 const char *new_password;
40 if (argc > 0 && argv[0]) {
41 new_password = argv[0];
42 } else {
43 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:",
44 ctx->user.domain_name, ctx->user.account_name);
45 new_password = getpass(password_prompt);
48 libnetctx = libnet_context_init();
49 if (!libnetctx) {
50 return -1;
52 libnetctx->user.account_name = ctx->user.account_name;
53 libnetctx->user.domain_name = ctx->user.domain_name;
54 libnetctx->user.password = ctx->user.password;
56 /* prepare password change */
57 r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
58 r.generic.in.account_name = ctx->user.account_name;
59 r.generic.in.domain_name = ctx->user.domain_name;
60 r.generic.in.oldpassword = ctx->user.password;
61 r.generic.in.newpassword = new_password;
63 /* do password change */
64 status = libnet_ChangePassword(libnetctx, ctx->mem_ctx, &r);
65 if (!NT_STATUS_IS_OK(status)) {
66 DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
67 return -1;
70 libnet_context_destroy(&libnetctx);
72 return 0;
75 static int net_password_change_usage(struct net_context *ctx, int argc, const char **argv)
77 d_printf("net_password_change_usage: TODO\n");
78 return 0;
81 static int net_password_change_help(struct net_context *ctx, int argc, const char **argv)
83 d_printf("net_password_change_help: TODO\n");
84 return 0;
87 static int net_password_set(struct net_context *ctx, int argc, const char **argv)
89 NTSTATUS status;
90 struct libnet_context *libnetctx;
91 union libnet_SetPassword r;
92 char *password_prompt = NULL;
93 char *p;
94 char *tmp;
95 const char *account_name;
96 const char *domain_name;
97 const char *new_password = NULL;
99 switch (argc) {
100 case 0: /* no args -> fail */
101 return net_password_usage(ctx, argc, argv);
102 case 1: /* only DOM\\user; prompt for password */
103 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
104 break;
105 case 2: /* DOM\\USER and password */
106 tmp = talloc_strdup(ctx->mem_ctx, argv[0]);
107 new_password = argv[1];
108 break;
109 default: /* too mayn args -> fail */
110 DEBUG(0,("net_password_set: too many args [%d]\n",argc));
111 return net_password_usage(ctx, argc, argv);
114 if ((p = strchr_m(tmp,'\\'))) {
115 *p = 0;
116 domain_name = tmp;
117 account_name = talloc_strdup(ctx->mem_ctx, p+1);
118 } else {
119 account_name = tmp;
120 domain_name = ctx->user.domain_name;
123 if (!new_password) {
124 password_prompt = talloc_asprintf(ctx->mem_ctx, "Enter new password for account [%s\\%s]:",
125 domain_name, account_name);
126 new_password = getpass(password_prompt);
129 libnetctx = libnet_context_init();
130 if (!libnetctx) {
131 return -1;
133 libnetctx->user.account_name = ctx->user.account_name;
134 libnetctx->user.domain_name = ctx->user.domain_name;
135 libnetctx->user.password = ctx->user.password;
137 /* prepare password change */
138 r.generic.level = LIBNET_SET_PASSWORD_GENERIC;
139 r.generic.in.account_name = account_name;
140 r.generic.in.domain_name = domain_name;
141 r.generic.in.newpassword = new_password;
143 /* do password change */
144 status = libnet_SetPassword(libnetctx, ctx->mem_ctx, &r);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(0,("net_password_set: %s\n",r.generic.out.error_string));
147 return -1;
150 libnet_context_destroy(&libnetctx);
152 return 0;
155 static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
157 d_printf("net_password_set_usage: TODO\n");
158 return 0;
161 static int net_password_set_help(struct net_context *ctx, int argc, const char **argv)
163 d_printf("net_password_set_help: TODO\n");
164 return 0;
167 static const struct net_functable net_password_functable[] = {
168 {"change", net_password_change, net_password_change_usage, net_password_change_help},
169 {"set", net_password_set, net_password_set_usage, net_password_set_help},
170 {"help", net_password_help, net_password_help, net_password_help},
171 {NULL, NULL}
174 int net_password(struct net_context *ctx, int argc, const char **argv)
176 return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
179 int net_password_usage(struct net_context *ctx, int argc, const char **argv)
181 d_printf("net password <command> [options]\n");
182 return 0;
185 int net_password_help(struct net_context *ctx, int argc, const char **argv)
187 d_printf("Account password handling:\n");
188 d_printf("\tchange\t\tchanges password (old password required)\n");
189 d_printf("\tset\t\tsets password\n");
190 return 0;