1 --- a/source/Makefile.in
2 +++ b/source/Makefile.in
3 @@ -538,10 +538,7 @@ TESTPARM_OBJ = utils/testparm.o \
5 PASSWD_UTIL_OBJ = utils/passwd_util.o
7 -SMBPASSWD_OBJ = utils/smbpasswd.o $(PASSWD_UTIL_OBJ) $(PASSCHANGE_OBJ) \
8 - $(PARAM_OBJ) $(SECRETS_OBJ) $(LIBSMB_OBJ) $(PASSDB_OBJ) \
9 - $(GROUPDB_OBJ) $(LIB_NONSMBD_OBJ) $(KRBCLIENT_OBJ) \
10 - $(POPT_LIB_OBJ) $(SMBLDAP_OBJ) $(RPC_PARSE_OBJ) $(LIBMSRPC_GEN_OBJ) $(LIBMSRPC_OBJ)
11 +SMBPASSWD_OBJ = utils/owrt_smbpasswd.o lib/md4.o
13 PDBEDIT_OBJ = utils/pdbedit.o $(PASSWD_UTIL_OBJ) $(PARAM_OBJ) $(PASSDB_OBJ) \
14 $(LIBSAMBA_OBJ) $(LIB_NONSMBD_OBJ) $(GROUPDB_OBJ) \
16 +++ b/source/utils/owrt_smbpasswd.c
19 + * Copyright (C) John Crispin <blogic@openwrt.org>
21 + * This program is free software; you can redistribute it and/or modify it
22 + * under the terms of the GNU General Public License as published by the
23 + * Free Software Foundation; either version 2 of the License, or (at your
24 + * option) any later version.
26 + * This program is distributed in the hope that it will be useful, but WITHOUT
27 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
28 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 + * You should have received a copy of the GNU General Public License along with
32 + * this program; if not, write to the Free Software Foundation, Inc., 675
33 + * Mass Ave, Cambridge, MA 02139, USA. */
35 +#include "includes.h"
38 +void E_md4hash(const char *passwd, uchar p16[16])
41 + smb_ucs2_t wpwd[129];
44 + len = strlen(passwd);
45 + for (i = 0; i < len; i++) {
46 +#if __BYTE_ORDER == __LITTLE_ENDIAN
47 + wpwd[i] = (unsigned char)passwd[i];
49 + wpwd[i] = (unsigned char)passwd[i] << 8;
54 + len = len * sizeof(int16);
55 + mdfour(p16, (unsigned char *)wpwd, len);
59 +/* returns -1 if user is not present in /etc/passwd*/
60 +int find_uid_for_user(char *user)
63 + FILE *fp = fopen("/etc/passwd", "r");
68 + printf("failed to open /etc/passwd");
74 + if(fgets(t, 255, fp))
77 + p1 = strchr(t, ':');
78 + if(p1 && (p1 - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
80 + p1 = strchr(t, ':');
83 + p2 = strchr(++p1, ':');
86 + p1 = strchr(++p2, ':');
95 + printf("No valid user found in /etc/passwd\n");
103 +void insert_user_in_smbpasswd(char *user, char *line)
106 + FILE *fp = fopen("/etc/samba/smbpasswd", "r+");
110 + printf("failed to open /etc/samba/smbpasswd");
116 + if(fgets(t, 255, fp))
119 + p = strchr(t, ':');
120 + if(p && (p - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
122 + fseek(fp, -strlen(line), SEEK_CUR);
135 +void delete_user_from_smbpasswd(char *user)
138 + FILE *fp = fopen("/etc/samba/smbpasswd", "r+");
142 + printf("failed to open /etc/samba/smbpasswd");
148 + if(fgets(t, 255, fp))
151 + p = strchr(t, ':');
152 + if(p && (p - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
154 + fpos_t r_pos, w_pos;
156 + fgetpos(fp, &r_pos);
158 + w_pos.__pos -= strlen(t);
159 + while(fgets(t2, 256, fp))
161 + fsetpos(fp, &w_pos);
163 + r_pos.__pos += strlen(t2);
164 + w_pos.__pos += strlen(t2);
165 + fsetpos(fp, &r_pos);
167 + ftruncate(fileno(fp), w_pos.__pos);
178 +int main(int argc, char **argv)
181 + uchar new_nt_p16[NT_HASH_LEN];
183 + int smbpasswd_present;
184 + char smbpasswd_line[256];
189 + printf("usage for openwrt_smbpasswd - \n\t%s USERNAME PASSWD\n\t%s -del USERNAME\n", argv[0], argv[0]);
192 + if(strcmp(argv[1], "-del") == 0)
194 + printf("deleting user %s\n", argv[2]);
195 + delete_user_from_smbpasswd(argv[2]);
198 + uid = find_uid_for_user(argv[1]);
202 + E_md4hash(argv[2], new_nt_p16);
203 + s = smbpasswd_line;
204 + s += snprintf(s, 256 - (s - smbpasswd_line), "%s:%u:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:", argv[1], uid);
205 + for(g = 0; g < 16; g++)
206 + s += snprintf(s, 256 - (s - smbpasswd_line), "%02X", new_nt_p16[g]);
207 + snprintf(s, 256 - (s - smbpasswd_line), ":[U ]:LCT-00000001:\n");
209 + insert_user_in_smbpasswd(argv[1], smbpasswd_line);