summaryrefslogtreecommitdiffstats
path: root/lib/libncp/ncpl_bind.c
blob: ab591eb44fc95f35b7212caff6fd6e01a66ac812 (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
/*
 * Copyright (c) 1999, Boris Popov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <netncp/ncp_lib.h>

static void nw_passencrypt(char *old, char *new, char *out);

int
ncp_get_bindery_object_id(NWCONN_HANDLE connid, u_int16_t object_type,
	const char *object_name, struct ncp_bindery_object *target)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 53);
	ncp_add_word_hl(conn, object_type);
	ncp_add_pstring(conn, object_name);

	if ((error = ncp_request(connid, 23, conn)) != 0) {
		return error;
	}
	if (conn->rpsize < 54) {
		return EACCES;
	}
	target->object_id = ncp_reply_dword_hl(conn, 0);
	target->object_type = ncp_reply_word_hl(conn, 4);
	memcpy(target->object_name, ncp_reply_data(conn, 6), 48);
	return 0;
}

int
ncp_read_property_value(NWCONN_HANDLE connid, int object_type,
	const char *object_name, int segment, const char *prop_name,
	struct nw_property *target)
{
	int error;
	struct ncp_buf conn;
	ncp_init_request_s(&conn, 61);
	ncp_add_word_hl(&conn, object_type);
	ncp_add_pstring(&conn, object_name);
	ncp_add_byte(&conn, segment);
	ncp_add_pstring(&conn, prop_name);

	if ((error = ncp_request(connid,23,&conn)) != 0) {
		return error;
	}
	memcpy(&(target->value), ncp_reply_data(&conn, 0), 128);
	target->more_flag = ncp_reply_byte(&conn, 128);
	target->property_flag = ncp_reply_byte(&conn, 129);
	return 0;
}

int
ncp_scan_bindery_object(NWCONN_HANDLE connid, u_int32_t last_id,
	u_int16_t object_type, char *search_string,
	struct ncp_bindery_object *target)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 55);
	ncp_add_dword_hl(conn, last_id);
	ncp_add_word_hl(conn, object_type);
	ncp_add_pstring(conn, search_string);
	error = ncp_request(connid, 23, conn);
	if (error) return error;
	target->object_id = ncp_reply_dword_hl(conn, 0);
	target->object_type = ncp_reply_word_hl(conn, 4);
	memcpy(target->object_name, ncp_reply_data(conn, 6),NCP_BINDERY_NAME_LEN);
	target->object_flags = ncp_reply_byte(conn, 54);
	target->object_security = ncp_reply_byte(conn, 55);
	target->object_has_prop = ncp_reply_byte(conn, 56);
	return 0;
}

int
ncp_get_bindery_object_name(NWCONN_HANDLE connid, u_int32_t object_id,
	struct ncp_bindery_object *target)
{
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 54);
	ncp_add_dword_hl(conn, object_id);
	if ((error = ncp_request(connid, 23, conn)) != 0)
		return error;
	target->object_id = ncp_reply_dword_hl(conn, 0);
	target->object_type = ncp_reply_word_hl(conn, 4);
	memcpy(target->object_name, ncp_reply_data(conn, 6), 48);
	return 0;
}

int
ncp_change_obj_passwd(NWCONN_HANDLE connid, 
	const struct ncp_bindery_object *object,
	const u_char *key,
	const u_char *oldpasswd,
	const u_char *newpasswd)
{
	long id = htonl(object->object_id);
	u_char cryptkey[8];
	u_char newpwd[16];	/* new passwd as stored by server */
	u_char oldpwd[16];	/* old passwd as stored by server */
	u_char len;
	DECLARE_RQ;

	memcpy(cryptkey, key, 8);
	nw_keyhash((u_char *)&id, oldpasswd, strlen(oldpasswd), oldpwd);
	nw_keyhash((u_char *)&id, newpasswd, strlen(newpasswd), newpwd);
	nw_encrypt(cryptkey, oldpwd, cryptkey);
	nw_passencrypt(oldpwd, newpwd, newpwd);
	nw_passencrypt(oldpwd + 8, newpwd + 8, newpwd + 8);
	if ((len = strlen(newpasswd)) > 63) {
		len = 63;
	}
	len = ((len ^ oldpwd[0] ^ oldpwd[1]) & 0x7f) | 0x40;

	ncp_init_request_s(conn, 75);
	ncp_add_mem(conn, cryptkey, 8);
	ncp_add_word_hl(conn, object->object_type);
	ncp_add_pstring(conn, object->object_name);
	ncp_add_byte(conn, len);
	ncp_add_mem(conn, newpwd, 16);
	return ncp_request(connid, 23, conn);
}

/*
 * target is a 8-byte buffer
 */
int
ncp_get_encryption_key(NWCONN_HANDLE cH, char *target) {
	int error;
	DECLARE_RQ;

	ncp_init_request_s(conn, 23);

	error = ncp_request(cH, 23, conn);
	if (error)
		return error;
	if (conn->rpsize < 8)
		return EACCES;
	memcpy(target, ncp_reply_data(conn, 0), 8);
	return 0;
}

int
ncp_keyed_verify_password(NWCONN_HANDLE cH, char *key, char *passwd,
	struct ncp_bindery_object *objinfo)
{
	u_long id = htonl(objinfo->object_id);
	u_char cryptkey[8];
	u_char buf[128];
	DECLARE_RQ;

	nw_keyhash((u_char *)&id, passwd, strlen(passwd), buf);
	nw_encrypt(key, buf, cryptkey);

	ncp_init_request_s(conn, 74);
	ncp_add_mem(conn, cryptkey, sizeof(cryptkey));
	ncp_add_word_hl(conn, objinfo->object_type);
	ncp_add_pstring(conn, objinfo->object_name);

	return ncp_request(cH, 23, conn);
}

static char passkeys[256 + 16] = {
	0x0f, 0x08, 0x05, 0x07, 0x0c, 0x02, 0x0e, 0x09,
	0x00, 0x01, 0x06, 0x0d, 0x03, 0x04, 0x0b, 0x0a,
	0x02, 0x0c, 0x0e, 0x06, 0x0f, 0x00, 0x01, 0x08,
	0x0d, 0x03, 0x0a, 0x04, 0x09, 0x0b, 0x05, 0x07,
	0x05, 0x02, 0x09, 0x0f, 0x0c, 0x04, 0x0d, 0x00,
	0x0e, 0x0a, 0x06, 0x08, 0x0b, 0x01, 0x03, 0x07,
	0x0f, 0x0d, 0x02, 0x06, 0x07, 0x08, 0x05, 0x09,
	0x00, 0x04, 0x0c, 0x03, 0x01, 0x0a, 0x0b, 0x0e,
	0x05, 0x0e, 0x02, 0x0b, 0x0d, 0x0a, 0x07, 0x00,
	0x08, 0x06, 0x04, 0x01, 0x0f, 0x0c, 0x03, 0x09,
	0x08, 0x02, 0x0f, 0x0a, 0x05, 0x09, 0x06, 0x0c,
	0x00, 0x0b, 0x01, 0x0d, 0x07, 0x03, 0x04, 0x0e,
	0x0e, 0x08, 0x00, 0x09, 0x04, 0x0b, 0x02, 0x07,
	0x0c, 0x03, 0x0a, 0x05, 0x0d, 0x01, 0x06, 0x0f,
	0x01, 0x04, 0x08, 0x0a, 0x0d, 0x0b, 0x07, 0x0e,
	0x05, 0x0f, 0x03, 0x09, 0x00, 0x02, 0x06, 0x0c,
	0x05, 0x03, 0x0c, 0x08, 0x0b, 0x02, 0x0e, 0x0a,
	0x04, 0x01, 0x0d, 0x00, 0x06, 0x07, 0x0f, 0x09,
	0x06, 0x00, 0x0b, 0x0e, 0x0d, 0x04, 0x0c, 0x0f,
	0x07, 0x02, 0x08, 0x0a, 0x01, 0x05, 0x03, 0x09,
	0x0b, 0x05, 0x0a, 0x0e, 0x0f, 0x01, 0x0c, 0x00,
	0x06, 0x04, 0x02, 0x09, 0x03, 0x0d, 0x07, 0x08,
	0x07, 0x02, 0x0a, 0x00, 0x0e, 0x08, 0x0f, 0x04,
	0x0c, 0x0b, 0x09, 0x01, 0x05, 0x0d, 0x03, 0x06,
	0x07, 0x04, 0x0f, 0x09, 0x05, 0x01, 0x0c, 0x0b,
	0x00, 0x03, 0x08, 0x0e, 0x02, 0x0a, 0x06, 0x0d,
	0x09, 0x04, 0x08, 0x00, 0x0a, 0x03, 0x01, 0x0c,
	0x05, 0x0f, 0x07, 0x02, 0x0b, 0x0e, 0x06, 0x0d,
	0x09, 0x05, 0x04, 0x07, 0x0e, 0x08, 0x03, 0x01,
	0x0d, 0x0b, 0x0c, 0x02, 0x00, 0x0f, 0x06, 0x0a,
	0x09, 0x0a, 0x0b, 0x0d, 0x05, 0x03, 0x0f, 0x00,
	0x01, 0x0c, 0x08, 0x07, 0x06, 0x04, 0x0e, 0x02,
	0x03, 0x0e, 0x0f, 0x02, 0x0d, 0x0c, 0x04, 0x05,
	0x09, 0x06, 0x00, 0x01, 0x0b, 0x07, 0x0a, 0x08
};

static void
nw_passencrypt(char *old, char *new, char *out)
{
	char *p, v;
	char copy[8];
	int i, di, ax;

#define HIGH(x)	(((x) >> 4) & 0xf)
#define LOW(x)	((x) & 0xf)
	memcpy(copy, new, 8);

	for (i = 0; i < 16; i++) {
		for (di = 0, ax = 0, p = old; di < 8; di++, ax += 0x20, p++) {
			v = copy[di] ^ *p;
			copy[di] = (passkeys[HIGH(v) + ax + 0x10] << 4) |
				   passkeys[LOW(v) + ax];
		}
		v = old[7];
		for (p = old + 7; p > old; p--) {
			*p = HIGH(p[-1]) | ((*p) << 4);
		}
		*old = HIGH(v) | (*old) << 4;
		bzero(out, 8);

		for (di = 0; di < 16; di++) {
			v = passkeys[di + 0x100];
			v = (v & 1) ? HIGH(copy[v / 2]) : LOW(copy[v / 2]);
			out[di / 2] |= ((di & 1) ? v << 4 : v);
		}
		memcpy(copy, out, 8);
	}
}
OpenPOWER on IntegriCloud