[packages] fix samba3 logging in nmbd when CONFIG_SAMBA3_DEBUG is enabled (#6389)
[openwrt/svn-archive/archive.git] / net / samba3 / patches / 130-owrt_smbpasswd.patch
1 Index: samba-3.0.24/source/Makefile
2 ===================================================================
3 --- samba-3.0.24.orig/source/Makefile 2008-08-07 15:56:45.000000000 +0200
4 +++ samba-3.0.24/source/Makefile 2008-08-07 15:56:45.000000000 +0200
5 @@ -1015,9 +1015,9 @@
6
7 MY_PASS_OBJ = libsmb/smbdes.o lib/md4.o lib/arc4.o
8
9 -bin/smbpasswd: utils/avm_smbpasswd.o $(MY_PASS_OBJ)
10 +bin/smbpasswd: utils/owrt_smbpasswd.o $(MY_PASS_OBJ)
11 @echo Linking $@
12 - @$(CC) $(FLAGS) -o $@ utils/avm_smbpasswd.o $(MY_PASS_OBJ) \
13 + @$(CC) $(FLAGS) -o $@ utils/owrt_smbpasswd.o $(MY_PASS_OBJ) \
14 -L$(TARGETFS)/lib
15
16
17 Index: samba-3.0.24/source/utils/owrt_smbpasswd.c
18 ===================================================================
19 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
20 +++ samba-3.0.24/source/utils/owrt_smbpasswd.c 2008-08-07 15:58:25.000000000 +0200
21 @@ -0,0 +1,195 @@
22 +/*
23 + * Copyright (C) John Crispin <blogic@openwrt.org>
24 + *
25 + * This program is free software; you can redistribute it and/or modify it
26 + * under the terms of the GNU General Public License as published by the
27 + * Free Software Foundation; either version 2 of the License, or (at your
28 + * option) any later version.
29 + *
30 + * This program is distributed in the hope that it will be useful, but WITHOUT
31 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
33 + * more details.
34 + *
35 + * You should have received a copy of the GNU General Public License along with
36 + * this program; if not, write to the Free Software Foundation, Inc., 675
37 + * Mass Ave, Cambridge, MA 02139, USA. */
38 +
39 +#include "includes.h"
40 +#include <endian.h>
41 +
42 +void E_md4hash(const char *passwd, uchar p16[16])
43 +{
44 + int len;
45 + smb_ucs2_t wpwd[129];
46 + int i;
47 +
48 + len = strlen(passwd);
49 + for (i = 0; i < len; i++) {
50 +#if __BYTE_ORDER == __LITTLE_ENDIAN
51 + wpwd[i] = (unsigned char)passwd[i];
52 +#else
53 + wpwd[i] = (unsigned char)passwd[i] << 8;
54 +#endif
55 + }
56 + wpwd[i] = 0;
57 +
58 + len = len * sizeof(int16);
59 + mdfour(p16, (unsigned char *)wpwd, len);
60 + ZERO_STRUCT(wpwd);
61 +}
62 +
63 +/* returns -1 if user is not present in /etc/passwd*/
64 +int find_uid_for_user(char *user)
65 +{
66 + char t[256];
67 + FILE *fp = fopen("/etc/passwd", "r");
68 + int ret = -1;
69 +
70 + if(!fp)
71 + {
72 + printf("failed to open /etc/passwd");
73 + goto out;
74 + }
75 +
76 + while(!feof(fp))
77 + {
78 + if(fgets(t, 255, fp))
79 + {
80 + char *p1, *p2;
81 + p1 = strchr(t, ':');
82 + if(p1 && (p1 - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
83 + {
84 + p1 = strchr(t, ':');
85 + if(!p1)
86 + goto out;
87 + p2 = strchr(++p1, ':');
88 + if(!p2)
89 + goto out;
90 + p1 = strchr(++p2, ':');
91 + if(!p1)
92 + goto out;
93 + *p1 = '\0';
94 + ret = atoi(p2);
95 + goto out;
96 + }
97 + }
98 + }
99 + printf("No valid user found in /etc/passwd\n");
100 +
101 +out:
102 + if(fp)
103 + fclose(fp);
104 + return ret;
105 +}
106 +
107 +void insert_user_in_smbpasswd(char *user, char *line)
108 +{
109 + char t[256];
110 + FILE *fp = fopen("/etc/samba/smbpasswd", "r+");
111 +
112 + if(!fp)
113 + {
114 + printf("failed to open /etc/samba/smbpasswd");
115 + goto out;
116 + }
117 +
118 + while(!feof(fp))
119 + {
120 + if(fgets(t, 255, fp))
121 + {
122 + char *p;
123 + p = strchr(t, ':');
124 + if(p && (p - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
125 + {
126 + fseek(fp, -strlen(line), SEEK_CUR);
127 + break;
128 + }
129 + }
130 + }
131 +
132 + fprintf(fp, line);
133 +
134 +out:
135 + if(fp)
136 + fclose(fp);
137 +}
138 +
139 +void delete_user_from_smbpasswd(char *user)
140 +{
141 + char t[256];
142 + FILE *fp = fopen("/etc/samba/smbpasswd", "r+");
143 +
144 + if(!fp)
145 + {
146 + printf("failed to open /etc/samba/smbpasswd");
147 + goto out;
148 + }
149 +
150 + while(!feof(fp))
151 + {
152 + if(fgets(t, 255, fp))
153 + {
154 + char *p;
155 + p = strchr(t, ':');
156 + if(p && (p - t == strlen(user)) && (strncmp(t, user, strlen(user))) == 0)
157 + {
158 + fpos_t r_pos, w_pos;
159 + char t2[256];
160 + fgetpos(fp, &r_pos);
161 + w_pos = r_pos;
162 + w_pos.__pos -= strlen(t);
163 + while(fgets(t2, 256, fp))
164 + {
165 + fsetpos(fp, &w_pos);
166 + fputs(t2, fp);
167 + r_pos.__pos += strlen(t2);
168 + w_pos.__pos += strlen(t2);
169 + fsetpos(fp, &r_pos);
170 + }
171 + ftruncate(fileno(fp), w_pos.__pos);
172 + break;
173 + }
174 + }
175 + }
176 +
177 +out:
178 + if(fp)
179 + fclose(fp);
180 +}
181 +
182 +int main(int argc, char **argv)
183 +{
184 + unsigned uid;
185 + uchar new_nt_p16[NT_HASH_LEN];
186 + int g;
187 + int smbpasswd_present;
188 + char smbpasswd_line[256];
189 + char *s;
190 +
191 + if(argc != 3)
192 + {
193 + printf("usage for openwrt_smbpasswd - \n\t%s USERNAME PASSWD\n\t%s -del USERNAME\n", argv[0], argv[0]);
194 + exit(1);
195 + }
196 + if(strcmp(argv[1], "-del") == 0)
197 + {
198 + printf("deleting user %s\n", argv[2]);
199 + delete_user_from_smbpasswd(argv[2]);
200 + return 0;
201 + }
202 + uid = find_uid_for_user(argv[1]);
203 + if(uid == -1)
204 + exit(2);
205 +
206 + E_md4hash(argv[2], new_nt_p16);
207 + s = smbpasswd_line;
208 + s += snprintf(s, 256 - (s - smbpasswd_line), "%s:%u:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:", argv[1], uid);
209 + for(g = 0; g < 16; g++)
210 + s += snprintf(s, 256 - (s - smbpasswd_line), "%02X", new_nt_p16[g]);
211 + snprintf(s, 256 - (s - smbpasswd_line), ":[U ]:LCT-00000001:\n");
212 +
213 + insert_user_in_smbpasswd(argv[1], smbpasswd_line);
214 +
215 + return 0;
216 +}