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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "includes.h"
|
---|
22 | #include "samba_tool/samba_tool.h"
|
---|
23 | #include "libnet/libnet.h"
|
---|
24 | #include "system/filesys.h"
|
---|
25 | #include "lib/events/events.h"
|
---|
26 | #include "auth/credentials/credentials.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("samba-tool password change [options]\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, "Enter new password for account [%s\\%s]:",
|
---|
51 | cli_credentials_get_domain(ctx->credentials),
|
---|
52 | cli_credentials_get_username(ctx->credentials));
|
---|
53 | new_password = getpass(password_prompt);
|
---|
54 | }
|
---|
55 |
|
---|
56 | libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
|
---|
57 | if (!libnetctx) {
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 | libnetctx->cred = ctx->credentials;
|
---|
61 |
|
---|
62 | /* prepare password change */
|
---|
63 | r.generic.level = LIBNET_CHANGE_PASSWORD_GENERIC;
|
---|
64 | r.generic.in.account_name = cli_credentials_get_username(ctx->credentials);
|
---|
65 | r.generic.in.domain_name = cli_credentials_get_domain(ctx->credentials);
|
---|
66 | r.generic.in.oldpassword = cli_credentials_get_password(ctx->credentials);
|
---|
67 | r.generic.in.newpassword = new_password;
|
---|
68 |
|
---|
69 | /* do password change */
|
---|
70 | status = libnet_ChangePassword(libnetctx, ctx, &r);
|
---|
71 | if (!NT_STATUS_IS_OK(status)) {
|
---|
72 | DEBUG(0,("net_password_change: %s\n",r.generic.out.error_string));
|
---|
73 | return -1;
|
---|
74 | }
|
---|
75 |
|
---|
76 | talloc_free(libnetctx);
|
---|
77 |
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | static int net_password_set_usage(struct net_context *ctx, int argc, const char **argv)
|
---|
83 | {
|
---|
84 | d_printf("samba-tool password set <username> [<password>] [options]\n");
|
---|
85 | return 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | static int net_password_set(struct net_context *ctx, int argc, const char **argv)
|
---|
90 | {
|
---|
91 | NTSTATUS status;
|
---|
92 | struct libnet_context *libnetctx;
|
---|
93 | union libnet_SetPassword r;
|
---|
94 | char *password_prompt = NULL;
|
---|
95 | char *p;
|
---|
96 | char *tmp;
|
---|
97 | const char *account_name;
|
---|
98 | const char *domain_name;
|
---|
99 | const char *new_password = NULL;
|
---|
100 |
|
---|
101 | switch (argc) {
|
---|
102 | case 0: /* no args -> fail */
|
---|
103 | return net_password_set_usage(ctx, argc, argv);
|
---|
104 | case 1: /* only DOM\\user; prompt for password */
|
---|
105 | tmp = talloc_strdup(ctx, argv[0]);
|
---|
106 | break;
|
---|
107 | case 2: /* DOM\\USER and password */
|
---|
108 | tmp = talloc_strdup(ctx, argv[0]);
|
---|
109 | new_password = argv[1];
|
---|
110 | break;
|
---|
111 | default: /* too mayn args -> fail */
|
---|
112 | DEBUG(0,("samba-tool password set: too many args [%d]\n",argc));
|
---|
113 | return net_password_set_usage(ctx, argc, argv);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if ((p = strchr_m(tmp,'\\'))) {
|
---|
117 | *p = 0;
|
---|
118 | domain_name = tmp;
|
---|
119 | account_name = talloc_strdup(ctx, p+1);
|
---|
120 | } else {
|
---|
121 | account_name = tmp;
|
---|
122 | domain_name = cli_credentials_get_domain(ctx->credentials);
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (!new_password) {
|
---|
126 | password_prompt = talloc_asprintf(ctx, "Enter new password for account [%s\\%s]:",
|
---|
127 | domain_name, account_name);
|
---|
128 | new_password = getpass(password_prompt);
|
---|
129 | }
|
---|
130 |
|
---|
131 | libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
|
---|
132 | if (!libnetctx) {
|
---|
133 | return -1;
|
---|
134 | }
|
---|
135 | libnetctx->cred = ctx->credentials;
|
---|
136 |
|
---|
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;
|
---|
142 |
|
---|
143 | /* do password change */
|
---|
144 | status = libnet_SetPassword(libnetctx, 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;
|
---|
148 | }
|
---|
149 |
|
---|
150 | talloc_free(libnetctx);
|
---|
151 |
|
---|
152 | return 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | static const struct net_functable net_password_functable[] = {
|
---|
157 | {"change", "change password (old password required)\n", net_password_change, net_password_change_usage },
|
---|
158 | {"set", "set password\n", net_password_set, net_password_set_usage },
|
---|
159 | {NULL, NULL}
|
---|
160 | };
|
---|
161 |
|
---|
162 | int net_password(struct net_context *ctx, int argc, const char **argv)
|
---|
163 | {
|
---|
164 | return net_run_function(ctx, argc, argv, net_password_functable, net_password_usage);
|
---|
165 | }
|
---|
166 |
|
---|
167 | int net_password_usage(struct net_context *ctx, int argc, const char **argv)
|
---|
168 | {
|
---|
169 | d_printf("samba-tool password <command> [options]\n");
|
---|
170 | d_printf("available commands:\n");
|
---|
171 | net_password_change_usage(ctx, argc, argv);
|
---|
172 | net_password_set_usage(ctx, argc, argv);
|
---|
173 | return 0;
|
---|
174 | }
|
---|