111-owrt_smbpasswd.patch 6.5 KB

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