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  * NetWare RPCs
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: stable/9/lib/libncp/ncpl_rpc.c 165920 2007-01-09 23:27:39Z imp $");
33 
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <arpa/inet.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <netncp/ncp_lib.h>
41 
42 struct ncp_rpc_rq {
43 	nuint16		len;	/* HL */
44 	nuint8		subfn;
45 	nuint32		reserved[4];
46 	nuint8		flags[4];
47 } __attribute__ ((packed));
48 
49 struct ncp_rpc_rp {
50 	nuint32		rpccode;
51 	nuint32		reserved[4];
52 	nuint32		rpcval;
53 } __attribute__ ((packed));
54 
55 static NWCCODE
ncp_rpc(NWCONN_HANDLE cH,int rpcfn,const nuint8 * rpcarg,char * arg1,char * arg2,nuint32 * rpcval)56 ncp_rpc(NWCONN_HANDLE cH, int rpcfn,
57 	const nuint8* rpcarg, char* arg1, char *arg2,
58 	nuint32* rpcval) {
59 	NWCCODE error;
60 	NW_FRAGMENT rq[4], rp;
61 	struct ncp_rpc_rq rqh;
62 	struct ncp_rpc_rp rph;
63 
64 	rqh.subfn = rpcfn;
65 	if (rpcarg)
66 		bcopy(rpcarg, rqh.reserved, 4 * 4 + 4);
67 	else
68 		bzero(rqh.reserved, 4 * 4 + 4);
69 	rq[0].fragAddress = (char*)&rqh;
70 	rq[0].fragSize = sizeof(rqh);
71 	rq[1].fragAddress = arg1;
72 	rq[1].fragSize = strlen(arg1) + 1;
73 	rq[2].fragAddress = arg2;
74 	rq[2].fragSize = arg2 ? (strlen(arg2) + 1) : 0;
75 	rqh.len = htons(rq[2].fragSize + rq[1].fragSize + sizeof(rqh) - 2);
76 	rp.fragAddress = (char*)&rph;
77 	rp.fragSize = sizeof(rph);
78 	error = NWRequest(cH, 131, 3, rq, 1, &rp);
79 	if (error) return error;
80 	if (rp.fragSize < 4) return EBADRPC;
81 	error = rph.rpccode;
82 	if (error) return error;
83 	if (rpcval) {
84 		if (rp.fragSize < 24)
85 			return EBADRPC;
86 		*rpcval = rph.rpcval;
87 	}
88 	return 0;
89 }
90 
91 NWCCODE
NWSMLoadNLM(NWCONN_HANDLE cH,pnstr8 cmd)92 NWSMLoadNLM(NWCONN_HANDLE cH, pnstr8 cmd) {
93 	return ncp_rpc(cH, 1, NULL, cmd, NULL, NULL);
94 }
95 
96 NWCCODE
NWSMUnloadNLM(NWCONN_HANDLE cH,pnstr8 cmd)97 NWSMUnloadNLM(NWCONN_HANDLE cH, pnstr8 cmd) {
98 	return ncp_rpc(cH, 2, NULL, cmd, NULL, NULL);
99 }
100 
101 NWCCODE
NWSMMountVolume(NWCONN_HANDLE cH,pnstr8 volName,nuint32 * volnum)102 NWSMMountVolume(NWCONN_HANDLE cH, pnstr8 volName, nuint32* volnum) {
103 	return ncp_rpc(cH, 3, NULL, volName, NULL, volnum);
104 }
105 
106 NWCCODE
NWSMDismountVolumeByName(NWCONN_HANDLE cH,pnstr8 vol)107 NWSMDismountVolumeByName(NWCONN_HANDLE cH, pnstr8 vol) {
108 	return ncp_rpc(cH, 4, NULL, vol, NULL, NULL);
109 }
110 
111 struct ncp_set_hdr {
112 	nuint32	typeFlag;	/* 0 - str, 1 - value */
113 	nuint32	value;
114 	nuint32	pad[20 - 4 - 4];
115 } __attribute__ ((packed));
116 
117 NWCCODE
NWSMSetDynamicCmdIntValue(NWCONN_HANDLE cH,pnstr8 setCommandName,nuint32 cmdValue)118 NWSMSetDynamicCmdIntValue(NWCONN_HANDLE cH, pnstr8 setCommandName, nuint32 cmdValue) {
119 	struct ncp_set_hdr rq;
120 
121 	memset(&rq, 0, sizeof(rq));
122 	rq.typeFlag = 1;
123 	rq.value = cmdValue;
124 	return ncp_rpc(cH, 6, (char*)&rq, setCommandName, NULL, NULL);
125 }
126 
127 NWCCODE
NWSMSetDynamicCmdStrValue(NWCONN_HANDLE cH,pnstr8 setCommandName,pnstr8 cmdValue)128 NWSMSetDynamicCmdStrValue(NWCONN_HANDLE cH, pnstr8 setCommandName,
129 		pnstr8 cmdValue) {
130 	return ncp_rpc(cH, 6, NULL, setCommandName, cmdValue, NULL);
131 }
132 
133 NWCCODE
NWSMExecuteNCFFile(NWCONN_HANDLE cH,pnstr8 NCFFileName)134 NWSMExecuteNCFFile(NWCONN_HANDLE cH, pnstr8 NCFFileName) {
135 	return ncp_rpc(cH, 7, NULL, NCFFileName, NULL, NULL);
136 }
137