source: branches/samba-3.0/source/utils/smbpasswd.c@ 372

Last change on this file since 372 was 124, checked in by Paul Smedley, 18 years ago

Update source to 3.0.28a

File size: 15.1 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jeremy Allison 1995-1998
4 * Copyright (C) Tim Potter 2001
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "includes.h"
21
22extern BOOL AllowDebugChange;
23
24/*
25 * Next two lines needed for SunOS and don't
26 * hurt anything else...
27 */
28extern char *optarg;
29extern int optind;
30
31/* forced running in root-mode */
32static BOOL got_username = False;
33static BOOL stdin_passwd_get = False;
34static fstring user_name;
35static char *new_passwd = NULL;
36static const char *remote_machine = NULL;
37
38static fstring ldap_secret;
39
40
41/*********************************************************
42 Print command usage on stderr and die.
43**********************************************************/
44static void usage(void)
45{
46 printf("When run by root:\n");
47 printf(" smbpasswd [options] [username]\n");
48 printf("otherwise:\n");
49 printf(" smbpasswd [options]\n\n");
50
51 printf("options:\n");
52 printf(" -L local mode (must be first option)\n");
53 printf(" -h print this usage message\n");
54 printf(" -s use stdin for password prompt\n");
55 printf(" -c smb.conf file Use the given path to the smb.conf file\n");
56 printf(" -D LEVEL debug level\n");
57 printf(" -r MACHINE remote machine\n");
58 printf(" -U USER remote username\n");
59
60 printf("extra options when run by root or in local mode:\n");
61 printf(" -a add user\n");
62 printf(" -d disable user\n");
63 printf(" -e enable user\n");
64 printf(" -i interdomain trust account\n");
65 printf(" -m machine trust account\n");
66 printf(" -n set no password\n");
67 printf(" -W use stdin ldap admin password\n");
68 printf(" -w PASSWORD ldap admin password\n");
69 printf(" -x delete user\n");
70 printf(" -R ORDER name resolve order\n");
71
72 exit(1);
73}
74
75static void set_line_buffering(FILE *f)
76{
77 setvbuf(f, NULL, _IOLBF, 0);
78}
79
80/*******************************************************************
81 Process command line options
82 ******************************************************************/
83
84static int process_options(int argc, char **argv, int local_flags)
85{
86 int ch;
87 pstring configfile;
88 pstrcpy(configfile, dyn_CONFIGFILE);
89
90 local_flags |= LOCAL_SET_PASSWORD;
91
92 ZERO_STRUCT(user_name);
93
94 user_name[0] = '\0';
95
96 while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LW")) != EOF) {
97 switch(ch) {
98 case 'L':
99#if !defined(DEVELOPER)
100 if (getuid() != 0) {
101 fprintf(stderr, "smbpasswd -L can only be used by root.\n");
102 exit(1);
103 }
104#endif
105 local_flags |= LOCAL_AM_ROOT;
106 break;
107 case 'c':
108 pstrcpy(configfile,optarg);
109 break;
110 case 'a':
111 local_flags |= LOCAL_ADD_USER;
112 break;
113 case 'x':
114 local_flags |= LOCAL_DELETE_USER;
115 local_flags &= ~LOCAL_SET_PASSWORD;
116 break;
117 case 'd':
118 local_flags |= LOCAL_DISABLE_USER;
119 local_flags &= ~LOCAL_SET_PASSWORD;
120 break;
121 case 'e':
122 local_flags |= LOCAL_ENABLE_USER;
123 local_flags &= ~LOCAL_SET_PASSWORD;
124 break;
125 case 'm':
126 local_flags |= LOCAL_TRUST_ACCOUNT;
127 break;
128 case 'i':
129 local_flags |= LOCAL_INTERDOM_ACCOUNT;
130 break;
131 case 'j':
132 d_printf("See 'net join' for this functionality\n");
133 exit(1);
134 break;
135 case 'n':
136 local_flags |= LOCAL_SET_NO_PASSWORD;
137 local_flags &= ~LOCAL_SET_PASSWORD;
138 new_passwd = smb_xstrdup("NO PASSWORD");
139 break;
140 case 'r':
141 remote_machine = optarg;
142 break;
143 case 's':
144 set_line_buffering(stdin);
145 set_line_buffering(stdout);
146 set_line_buffering(stderr);
147 stdin_passwd_get = True;
148 break;
149 case 'w':
150 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
151 fstrcpy(ldap_secret, optarg);
152 break;
153 case 'R':
154 lp_set_name_resolve_order(optarg);
155 break;
156 case 'D':
157 DEBUGLEVEL = atoi(optarg);
158 break;
159 case 'U': {
160 got_username = True;
161 fstrcpy(user_name, optarg);
162 break;
163 case 'W':
164 local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
165 *ldap_secret = '\0';
166 break;
167 }
168 case 'h':
169 default:
170 usage();
171 }
172 }
173
174 argc -= optind;
175 argv += optind;
176
177 switch(argc) {
178 case 0:
179 if (!got_username)
180 fstrcpy(user_name, "");
181 break;
182 case 1:
183 if (!(local_flags & LOCAL_AM_ROOT)) {
184 usage();
185 } else {
186 if (got_username) {
187 usage();
188 } else {
189 fstrcpy(user_name, argv[0]);
190 }
191 }
192 break;
193 default:
194 usage();
195 }
196
197 if (!lp_load(configfile,True,False,False,True)) {
198 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
199 configfile);
200 exit(1);
201 }
202
203 return local_flags;
204}
205
206/*************************************************************
207 Utility function to prompt for new password.
208*************************************************************/
209static char *prompt_for_new_password(BOOL stdin_get)
210{
211 char *p;
212 fstring new_pw;
213
214 ZERO_ARRAY(new_pw);
215
216 p = get_pass("New SMB password:", stdin_get);
217
218 fstrcpy(new_pw, p);
219 SAFE_FREE(p);
220
221 p = get_pass("Retype new SMB password:", stdin_get);
222
223 if (strcmp(p, new_pw)) {
224 fprintf(stderr, "Mismatch - password unchanged.\n");
225 ZERO_ARRAY(new_pw);
226 SAFE_FREE(p);
227 return NULL;
228 }
229
230 return p;
231}
232
233
234/*************************************************************
235 Change a password either locally or remotely.
236*************************************************************/
237
238static NTSTATUS password_change(const char *remote_mach, char *username,
239 char *old_passwd, char *new_pw,
240 int local_flags)
241{
242 NTSTATUS ret;
243 pstring err_str;
244 pstring msg_str;
245
246 if (remote_mach != NULL) {
247 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
248 LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
249 /* these things can't be done remotely yet */
250 return NT_STATUS_UNSUCCESSFUL;
251 }
252 ret = remote_password_change(remote_mach, username,
253 old_passwd, new_pw, err_str, sizeof(err_str));
254 if(*err_str)
255 fprintf(stderr, "%s", err_str);
256 return ret;
257 }
258
259 ret = local_password_change(username, local_flags, new_pw,
260 err_str, sizeof(err_str), msg_str, sizeof(msg_str));
261
262 if(*msg_str)
263 printf("%s", msg_str);
264 if(*err_str)
265 fprintf(stderr, "%s", err_str);
266
267 return ret;
268}
269
270/*******************************************************************
271 Store the LDAP admin password in secrets.tdb
272 ******************************************************************/
273static BOOL store_ldap_admin_pw (char* pw)
274{
275 if (!pw)
276 return False;
277
278 if (!secrets_init())
279 return False;
280
281 return secrets_store_ldap_pw(lp_ldap_admin_dn(), pw);
282}