1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * calls that don't fit to any other category
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: stable/9/lib/libncp/ncpl_misc.c 165920 2007-01-09 23:27:39Z imp $");
34 
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <arpa/inet.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <strings.h>
42 
43 #include <netncp/ncp_lib.h>
44 
45 static time_t
ncp_nw_to_ctime(struct nw_time_buffer * source)46 ncp_nw_to_ctime(struct nw_time_buffer *source) {
47 	struct tm u_time;
48 
49 	bzero(&u_time,sizeof(struct tm));
50 	/*
51 	 * XXX: NW 4.x tracks daylight automatically
52 	 */
53 	u_time.tm_isdst = -1;
54 	u_time.tm_sec = source->second;
55 	u_time.tm_min = source->minute;
56 	u_time.tm_hour = source->hour;
57 	u_time.tm_mday = source->day;
58 	u_time.tm_mon = source->month - 1;
59 	u_time.tm_year = source->year;
60 
61 	if (u_time.tm_year < 80) {
62 		u_time.tm_year += 100;
63 	}
64 	return mktime(&u_time);
65 }
66 
67 int
ncp_get_file_server_information(NWCONN_HANDLE connid,struct ncp_file_server_info * target)68 ncp_get_file_server_information(NWCONN_HANDLE connid,
69 	struct ncp_file_server_info *target)
70 {
71 	int error;
72 	DECLARE_RQ;
73 
74 	ncp_init_request_s(conn, 17);
75 	if ((error = ncp_request(connid, 23, conn)) != 0)
76 		return error;
77 	memcpy(target, ncp_reply_data(conn, 0), sizeof(*target));
78 	target->MaximumServiceConnections
79 	    = htons(target->MaximumServiceConnections);
80 	target->ConnectionsInUse
81 	    = htons(target->ConnectionsInUse);
82 	target->MaxConnectionsEverUsed
83 	    = htons(target->MaxConnectionsEverUsed);
84 	target->NumberMountedVolumes
85 	    = htons(target->NumberMountedVolumes);
86 	return 0;
87 }
88 
89 int
ncp_get_stations_logged_info(NWCONN_HANDLE connid,u_int32_t connection,struct ncp_bindery_object * target,time_t * login_time)90 ncp_get_stations_logged_info(NWCONN_HANDLE connid, u_int32_t connection,
91 	struct ncp_bindery_object *target, time_t *login_time)
92 {
93 	int error;
94 	DECLARE_RQ;
95 
96 	ncp_init_request_s(conn, 28);
97 	ncp_add_dword_lh(conn, connection);
98 
99 	if ((error = ncp_request(connid, 23, conn)) != 0)
100 		return error;
101 	bzero(target, sizeof(*target));
102 	target->object_id = ncp_reply_dword_hl(conn, 0);
103 	target->object_type = ncp_reply_word_hl(conn, 4);
104 	memcpy(target->object_name, ncp_reply_data(conn, 6),
105 	       sizeof(target->object_name));
106 	*login_time = ncp_nw_to_ctime((struct nw_time_buffer *)ncp_reply_data(conn, 54));
107 	return 0;
108 }
109 
110 int
ncp_get_internet_address(NWCONN_HANDLE connid,u_int32_t connection,struct ipx_addr * target,u_int8_t * conn_type)111 ncp_get_internet_address(NWCONN_HANDLE connid, u_int32_t connection,
112 	struct ipx_addr *target, u_int8_t * conn_type)
113 {
114 	int error;
115 	DECLARE_RQ;
116 
117 	ncp_init_request_s(conn, 26);
118 	ncp_add_dword_lh(conn, connection);
119 	error = ncp_request(connid, 23, conn);
120 	if (error) return error;
121 	bzero(target, sizeof(*target));
122 	ipx_netlong(*target) = ncp_reply_dword_lh(conn, 0);
123 	memcpy(&(target->x_host), ncp_reply_data(conn, 4), 6);
124 	target->x_port = ncp_reply_word_lh(conn, 10);
125 	*conn_type = ncp_reply_byte(conn, 12);
126 	return 0;
127 }
128 
129 NWCCODE
NWGetObjectConnectionNumbers(NWCONN_HANDLE connHandle,pnstr8 pObjName,nuint16 objType,pnuint16 pNumConns,pnuint16 pConnHandleList,nuint16 maxConns)130 NWGetObjectConnectionNumbers(NWCONN_HANDLE connHandle,
131 		pnstr8 pObjName, nuint16 objType,
132 		pnuint16 pNumConns, pnuint16 pConnHandleList,
133 		nuint16 maxConns)
134 {
135 	int error, i, n;
136 	nuint32 lastconn;
137 	DECLARE_RQ;
138 
139 	lastconn = 0;
140 	ncp_init_request_s(conn, 27);
141 	ncp_add_dword_lh(conn, lastconn);
142 	ncp_add_word_hl(conn, objType);
143 	ncp_add_pstring(conn, pObjName);
144 	if ((error = ncp_request(connHandle, 23, conn)) != 0) return error;
145 	n = min(ncp_reply_byte(conn, 0), maxConns);
146 	*pNumConns = n;
147 	for (i = 0; i < n ; i++) {
148 		*pConnHandleList++ = ncp_reply_dword_lh(conn, i * 4 + 1);
149 	}
150 	return 0;
151 }
152 
153 void
NWUnpackDateTime(nuint32 dateTime,NW_DATE * sDate,NW_TIME * sTime)154 NWUnpackDateTime(nuint32 dateTime, NW_DATE *sDate, NW_TIME *sTime) {
155 	NWUnpackDate(dateTime >> 16, sDate);
156 	NWUnpackTime(dateTime & 0xffff, sTime);
157 }
158 
159 void
NWUnpackDate(nuint16 date,NW_DATE * sDate)160 NWUnpackDate(nuint16 date, NW_DATE *sDate) {
161 	sDate->day = date & 0x1f;
162 	sDate->month = (date >> 5) & 0xf;
163 	sDate->year = ((date >> 9) & 0x7f) + 1980;
164 }
165 
166 void
NWUnpackTime(nuint16 time,NW_TIME * sTime)167 NWUnpackTime(nuint16 time, NW_TIME *sTime) {
168 	sTime->seconds = time & 0x1f;
169 	sTime->minutes = (time >> 5) & 0x3f;
170 	sTime->hours = (time >> 11) & 0x1f;
171 }
172 
173 nuint32
NWPackDateTime(NW_DATE * sDate,NW_TIME * sTime)174 NWPackDateTime(NW_DATE *sDate, NW_TIME *sTime) {
175 	return 0;
176 }
177 
178 nuint16
NWPackDate(NW_DATE * sDate)179 NWPackDate(NW_DATE *sDate) {
180 	return 0;
181 }
182 
183 nuint16
NWPackTime(NW_TIME * sTime)184 NWPackTime(NW_TIME *sTime) {
185 	return 0;
186 }
187 
188 time_t
ncp_UnpackDateTime(nuint32 dateTime)189 ncp_UnpackDateTime(nuint32 dateTime) {
190 	struct tm u_time;
191 	NW_DATE d;
192 	NW_TIME t;
193 
194 	NWUnpackDateTime(dateTime, &d, &t);
195 	bzero(&u_time,sizeof(struct tm));
196 	u_time.tm_isdst = -1;
197 	u_time.tm_sec = t.seconds;
198 	u_time.tm_min = t.minutes;
199 	u_time.tm_hour = t.hours;
200 	u_time.tm_mday = d.day;
201 	u_time.tm_mon = d.month - 1;
202 	u_time.tm_year = d.year - 1900;
203 
204 	return mktime(&u_time);
205 }
206 
207 int
ncp_GetFileServerDateAndTime(NWCONN_HANDLE cH,time_t * target)208 ncp_GetFileServerDateAndTime(NWCONN_HANDLE cH, time_t *target) {
209 	int error;
210 	DECLARE_RQ;
211 
212 	ncp_init_request(conn);
213 	if ((error = ncp_request(cH, 20, conn)) != 0)
214 		return error;
215 	*target = ncp_nw_to_ctime((struct nw_time_buffer *) ncp_reply_data(conn, 0));
216 	return 0;
217 }
218 
219 int
ncp_SetFileServerDateAndTime(NWCONN_HANDLE cH,time_t * source)220 ncp_SetFileServerDateAndTime(NWCONN_HANDLE cH, time_t * source) {
221 	int year;
222 	struct tm *utime = localtime(source);
223 	DECLARE_RQ;
224 
225 	year = utime->tm_year;
226 	if (year > 99) {
227 		year -= 100;
228 	}
229 	ncp_init_request_s(conn, 202);
230 	ncp_add_byte(conn, year);
231 	ncp_add_byte(conn, utime->tm_mon + 1);
232 	ncp_add_byte(conn, utime->tm_mday);
233 	ncp_add_byte(conn, utime->tm_hour);
234 	ncp_add_byte(conn, utime->tm_min);
235 	ncp_add_byte(conn, utime->tm_sec);
236 	return ncp_request(cH, 23, conn);
237 }
238 
239 NWCCODE
NWDownFileServer(NWCONN_HANDLE cH,int force)240 NWDownFileServer(NWCONN_HANDLE cH, int force) {
241 	DECLARE_RQ;
242 
243 	ncp_init_request_s(conn, 211);
244 	ncp_add_byte(conn, force ? 0 : 0xff);
245 	return ncp_request(cH, 23, conn);
246 }
247 
248 NWCCODE
NWCloseBindery(NWCONN_HANDLE cH)249 NWCloseBindery(NWCONN_HANDLE cH) {
250 	DECLARE_RQ;
251 
252 	ncp_init_request_s(conn, 68);
253 	return ncp_request(cH, 23, conn);
254 }
255 
256 NWCCODE
NWOpenBindery(NWCONN_HANDLE cH)257 NWOpenBindery(NWCONN_HANDLE cH) {
258 	DECLARE_RQ;
259 
260 	ncp_init_request_s(conn, 69);
261 	return ncp_request(cH, 23, conn);
262 }
263 
264 NWCCODE
NWDisableTTS(NWCONN_HANDLE cH)265 NWDisableTTS(NWCONN_HANDLE cH) {
266 	DECLARE_RQ;
267 
268 	ncp_init_request_s(conn, 207);
269 	return ncp_request(cH, 23, conn);
270 }
271 
272 NWCCODE
NWEnableTTS(NWCONN_HANDLE cH)273 NWEnableTTS(NWCONN_HANDLE cH) {
274 	DECLARE_RQ;
275 
276 	ncp_init_request_s(conn, 208);
277 	return ncp_request(cH, 23, conn);
278 }
279 
280 NWCCODE
NWDisableFileServerLogin(NWCONN_HANDLE cH)281 NWDisableFileServerLogin(NWCONN_HANDLE cH) {
282 	DECLARE_RQ;
283 
284 	ncp_init_request_s(conn, 203);
285 	return ncp_request(cH, 23, conn);
286 }
287 
288 NWCCODE
NWEnableFileServerLogin(NWCONN_HANDLE cH)289 NWEnableFileServerLogin(NWCONN_HANDLE cH) {
290 	DECLARE_RQ;
291 
292 	ncp_init_request_s(conn, 204);
293 	return ncp_request(cH, 23, conn);
294 }
295