aboutsummaryrefslogtreecommitdiffstats
path: root/main/samba36/111-owrt_smbpasswd.patch
blob: 79abea581da247dc45299673bd51ae2dbb85c056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -1019,7 +1019,7 @@ TEST_LP_LOAD_OBJ = param/test_lp_load.o
 
 PASSWD_UTIL_OBJ = utils/passwd_util.o
 
-SMBPASSWD_OBJ = utils/smbpasswd.o $(PASSWD_UTIL_OBJ) $(PASSCHANGE_OBJ) \
+SMBPASSWD_OBJ = utils/owrt_smbpasswd.o $(PASSWD_UTIL_OBJ) $(PASSCHANGE_OBJ) \
 		$(PARAM_OBJ) $(LIBSMB_OBJ) $(PASSDB_OBJ) \
 		$(GROUPDB_OBJ) $(LIB_NONSMBD_OBJ) $(KRBCLIENT_OBJ) \
 		$(POPT_LIB_OBJ) $(SMBLDAP_OBJ) \
@@ -1791,7 +1791,7 @@ nmbd/nmbd_multicall.o: nmbd/nmbd.c nmbd/
 		echo "$(COMPILE_CC_PATH)" 1>&2;\
 		$(COMPILE_CC_PATH) >/dev/null 2>&1
 
-utils/smbpasswd_multicall.o: utils/smbpasswd.c utils/smbpasswd.o
+utils/smbpasswd_multicall.o: utils/owrt_smbpasswd.c utils/owrt_smbpasswd.o
 	@echo Compiling $<.c
 	@$(COMPILE_CC_PATH) -Dmain=smbpasswd_main && exit 0;\
 		echo "The following command failed:" 1>&2;\
@@ -1800,7 +1800,7 @@ utils/smbpasswd_multicall.o: utils/smbpa
 
 SMBD_MULTI_O = $(patsubst smbd/server.o,smbd/server_multicall.o,$(SMBD_OBJ))
 NMBD_MULTI_O = $(patsubst nmbd/nmbd.o,nmbd/nmbd_multicall.o,$(filter-out $(LIB_DUMMY_OBJ),$(NMBD_OBJ)))
-SMBPASSWD_MULTI_O = $(patsubst utils/smbpasswd.o,utils/smbpasswd_multicall.o,$(filter-out $(LIB_DUMMY_OBJ),$(SMBPASSWD_OBJ)))
+SMBPASSWD_MULTI_O = $(patsubst utils/owrt_smbpasswd.o,utils/smbpasswd_multicall.o,$(filter-out $(LIB_DUMMY_OBJ),$(SMBPASSWD_OBJ)))
 MULTI_O = multi.o
 
 MULTICALL_O = $(sort $(SMBD_MULTI_O) $(NMBD_MULTI_O) $(SMBPASSWD_MULTI_O) $(MULTI_O))
--- /dev/null
+++ b/source3/utils/owrt_smbpasswd.c
@@ -0,0 +1,249 @@
+/*
+ * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 675
+ * Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include "includes.h"
+#include <endian.h>
+#include <stdio.h>
+
+static char buf[256];
+
+static void md4hash(const char *passwd, uchar p16[16])
+{
+	int len;
+	smb_ucs2_t wpwd[129];
+	int i;
+
+	len = strlen(passwd);
+	for (i = 0; i < len; i++) {
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+		wpwd[i] = (unsigned char)passwd[i];
+#else
+		wpwd[i] = (unsigned char)passwd[i] << 8;
+#endif
+	}
+	wpwd[i] = 0;
+
+	len = len * sizeof(int16);
+	mdfour(p16, (unsigned char *)wpwd, len);
+	ZERO_STRUCT(wpwd);
+}
+
+
+static bool find_passwd_line(FILE *fp, const char *user, char **next)
+{
+	char *p1;
+
+	while (!feof(fp)) {
+		if(!fgets(buf, sizeof(buf) - 1, fp))
+			continue;
+
+		p1 = strchr(buf, ':');
+
+		if (p1 - buf != strlen(user))
+			continue;
+
+		if (strncmp(buf, user, p1 - buf) != 0)
+			continue;
+
+		if (next)
+			*next = p1;
+		return true;
+	}
+	return false;
+}
+
+/* returns -1 if user is not present in /etc/passwd*/
+static int find_uid_for_user(const char *user)
+{
+	FILE *fp;
+	char *p1, *p2, *p3;
+	int ret = -1;
+
+	fp = fopen("/etc/passwd", "r");
+	if (!fp) {
+		printf("failed to open /etc/passwd");
+		goto out;
+	}
+
+	if (!find_passwd_line(fp, user, &p1)) {
+		printf("User %s not found or invalid in /etc/passwd\n", user);
+		goto out;
+	}
+
+	p2 = strchr(p1 + 1, ':');
+	if (!p2)
+		goto out;
+
+	p2++;
+	p3 = strchr(p2, ':');
+	if (!p1)
+		goto out;
+
+	*p3 = '\0';
+	ret = atoi(p2);
+
+out:
+	if(fp)
+		fclose(fp);
+	return ret;
+}
+
+static void smbpasswd_write_user(FILE *fp, const char *user, int uid, const char *password)
+{
+	static uchar nt_p16[NT_HASH_LEN];
+	int len = 0;
+	int i;
+
+	md4hash(strdup(password), nt_p16);
+
+	len += snprintf(buf + len, sizeof(buf) - len, "%s:%u:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:", user, uid);
+	for(i = 0; i < NT_HASH_LEN; i++)
+		len += snprintf(buf + len, sizeof(buf) - len, "%02X", nt_p16[i]);
+
+	snprintf(buf + len, sizeof(buf) - len, ":[U          ]:LCT-00000001:\n");
+	fputs(buf, fp);
+}
+
+static void smbpasswd_delete_user(FILE *fp)
+{
+	fpos_t r_pos, w_pos;
+	int len = strlen(buf);
+
+	fgetpos(fp, &r_pos);
+	fseek(fp, -len, SEEK_CUR);
+	fgetpos(fp, &w_pos);
+	fsetpos(fp, &r_pos);
+
+	while (fgets(buf, sizeof(buf) - 1, fp)) {
+		int cur_len = strlen(buf);
+
+		fsetpos(fp, &w_pos);
+		fputs(buf, fp);
+		fgetpos(fp, &w_pos);
+
+		fsetpos(fp, &r_pos);
+		fseek(fp, cur_len, SEEK_CUR);
+		fgetpos(fp, &r_pos);
+	}
+
+	fsetpos(fp, &w_pos);
+	ftruncate(fileno(fp), ftello(fp));
+}
+
+static int usage(const char *progname)
+{
+	fprintf(stderr,
+		"Usage: %s [options] <username>\n"
+		"\n"
+		"Options:\n"
+		"  -s		read password from stdin\n"
+		"  -a		add user\n"
+		"  -x		delete user\n",
+		progname);
+	return 1;
+}
+
+int main(int argc, char **argv)
+{
+	const char *prog = argv[0];
+	const char *user;
+	char *pw1, *pw2;
+	FILE *fp;
+	bool add = false, delete = false, get_stdin = false, found;
+	int ch;
+	int uid;
+
+	TALLOC_CTX *frame = talloc_stackframe();
+
+	while ((ch = getopt(argc, argv, "asx")) != EOF) {
+		switch (ch) {
+		case 's':
+			get_stdin = true;
+			break;
+		case 'a':
+			add = true;
+			break;
+		case 'x':
+			delete = true;
+			break;
+		default:
+			return usage(prog);
+		}
+	}
+
+	if (add && delete)
+		return usage(prog);
+
+	argc -= optind;
+	argv += optind;
+
+	if (!argc)
+		return usage(prog);
+
+	user = argv[0];
+	if (!delete) {
+		uid = find_uid_for_user(user);
+		if (uid < 0) {
+			fprintf(stderr, "Could not find user '%s' in /etc/passwd\n", user);
+			return 2;
+		}
+	}
+
+	fp = fopen("/etc/samba/smbpasswd", "r+");
+	if(!fp) {
+		fprintf(stderr, "Failed to open /etc/samba/smbpasswd");
+		return 3;
+	}
+
+	found = find_passwd_line(fp, user, NULL);
+	if (!add && !found) {
+		fprintf(stderr, "Could not find user '%s' in /etc/samba/smbpasswd\n", user);
+		return 3;
+	}
+
+	if (delete) {
+		smbpasswd_delete_user(fp);
+		goto out;
+	}
+
+	pw1 = get_pass("New SMB password:", get_stdin);
+	if (!pw1)
+		pw1 = strdup("");
+
+	pw2 = get_pass("Retype SMB password:", get_stdin);
+	if (!pw2)
+		pw2 = strdup("");
+
+	if (strcmp(pw1, pw2) != 0) {
+		fprintf(stderr, "Mismatch - password unchanged.\n");
+		goto out_free;
+	}
+
+	if (found)
+		fseek(fp, -strlen(buf), SEEK_CUR);
+	smbpasswd_write_user(fp, user, uid, pw2);
+
+out_free:
+	free(pw1);
+	free(pw2);
+out:
+	fclose(fp);
+	TALLOC_FREE(frame);
+
+	return 0;
+}