1 
2 /*
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 
25 #include "../includes.h"
26 
27 
28 #define	PIPE		"\\PIPE\\"
29 #define	PIPELEN		strlen(PIPE)
30 
31 extern int DEBUGLEVEL;
32 static int chain_pnum = -1;
33 
34 #ifndef MAX_OPEN_PIPES
35 #define MAX_OPEN_PIPES 50
36 #endif
37 
38 #define PIPE_HANDLE_OFFSET 0x800
39 
40 static struct
41 {
42   int cnum;
43   BOOL open; /* open connection */
44   uint16 device_state;
45   fstring name;
46 
47 } Pipes[MAX_OPEN_PIPES];
48 
49 #define VALID_PNUM(pnum)   (((pnum) >= 0) && ((pnum) < MAX_OPEN_PIPES))
50 #define OPEN_PNUM(pnum)    (VALID_PNUM(pnum) && Pipes[pnum].open)
51 #define PNUM_OK(pnum,c) (OPEN_PNUM(pnum) && (c)==Pipes[pnum].cnum)
52 
53 
54 /****************************************************************************
55   reset pipe chain handle number
56 ****************************************************************************/
reset_chain_pnum(void)57 void reset_chain_pnum(void)
58 {
59 	chain_pnum = -1;
60 }
61 
62 /****************************************************************************
63   initialise pipe handle states...
64 ****************************************************************************/
init_rpc_pipe_hnd(void)65 void init_rpc_pipe_hnd(void)
66 {
67 	int i;
68 	/* we start at 1 here for an obscure reason I can't now remember,
69 	but I think is important :-) */
70 	for (i = 1; i < MAX_OPEN_PIPES; i++)
71 	{
72 		Pipes[i].open = False;
73 	}
74 
75 	return;
76 }
77 
78 /****************************************************************************
79   find first available file slot
80 ****************************************************************************/
open_rpc_pipe_hnd(char * pipe_name,int cnum)81 int open_rpc_pipe_hnd(char *pipe_name, int cnum)
82 {
83 	int i;
84 	/* we start at 1 here for an obscure reason I can't now remember,
85 	but I think is important :-) */
86 	for (i = 1; i < MAX_OPEN_PIPES; i++)
87 	{
88 		if (!Pipes[i].open)
89 		{
90 			Pipes[i].open = True;
91 			Pipes[i].device_state = 0;
92 			Pipes[i].cnum = cnum;
93 			fstrcpy(Pipes[i].name, pipe_name);
94 
95 			DEBUG(4,("Opened pipe %s with handle %x\n",
96 			           pipe_name, i + PIPE_HANDLE_OFFSET));
97 
98 			chain_pnum = i;
99 
100 			return(i);
101 		}
102 	}
103 
104 	DEBUG(1,("ERROR! Out of pipe structures - perhaps increase MAX_OPEN_PIPES?\n"));
105 
106 	return(-1);
107 }
108 
109 /****************************************************************************
110   gets the name of a pipe
111 ****************************************************************************/
get_rpc_pipe_hnd_name(int pnum)112 char *get_rpc_pipe_hnd_name(int pnum)
113 {
114 	DEBUG(6,("get_pipe_name: "));
115 
116 	if (VALID_PNUM(pnum - PIPE_HANDLE_OFFSET))
117 	{
118 		DEBUG(6,("name: %s cnum: %d open: %s ",
119 		          Pipes[pnum - PIPE_HANDLE_OFFSET].name,
120 		          Pipes[pnum - PIPE_HANDLE_OFFSET].cnum,
121 		          BOOLSTR(Pipes[pnum - PIPE_HANDLE_OFFSET].open)));
122 	}
123 	if (OPEN_PNUM(pnum - PIPE_HANDLE_OFFSET))
124 	{
125 		DEBUG(6,("OK\n"));
126 		return Pipes[pnum - PIPE_HANDLE_OFFSET].name;
127 	}
128 	else
129 	{
130 		DEBUG(6,("NOT\n"));
131 		return NULL;
132 	}
133 }
134 
135 /****************************************************************************
136   set device state on a pipe.  exactly what this is for is unknown...
137 ****************************************************************************/
set_rpc_pipe_hnd_state(int pnum,int cnum,uint16 device_state)138 BOOL set_rpc_pipe_hnd_state(int pnum, int cnum, uint16 device_state)
139 {
140   /* mapping is PIPE_HANDLE_OFFSET up... */
141 
142   if (PNUM_OK(pnum-PIPE_HANDLE_OFFSET, cnum))
143   {
144     DEBUG(3,("%s Setting pipe device state=%x on pipe name %s pnum=%x cnum=%d\n",
145 	   timestring(), device_state,
146        Pipes[pnum-PIPE_HANDLE_OFFSET].name, pnum,cnum));
147 
148     Pipes[pnum-PIPE_HANDLE_OFFSET].device_state = device_state;
149     return True;
150   }
151   else
152   {
153     DEBUG(3,("%s Error setting pipe device state=%x (pnum=%x cnum=%d)\n",
154 	   timestring(), device_state, pnum, cnum));
155     return False;
156   }
157 }
158 
159 /****************************************************************************
160   close an rpc pipe
161 ****************************************************************************/
close_rpc_pipe_hnd(int pnum,int cnum)162 BOOL close_rpc_pipe_hnd(int pnum, int cnum)
163 {
164   /* mapping is PIPE_HANDLE_OFFSET up... */
165 
166   if (PNUM_OK(pnum-PIPE_HANDLE_OFFSET,cnum))
167   {
168     DEBUG(3,("%s Closed pipe name %s pnum=%x cnum=%d\n",
169 	   timestring(),Pipes[pnum-PIPE_HANDLE_OFFSET].name, pnum,cnum));
170 
171     Pipes[pnum-PIPE_HANDLE_OFFSET].open = False;
172     return True;
173   }
174   else
175   {
176     DEBUG(3,("%s Error closing pipe pnum=%x cnum=%d\n",
177 	   timestring(),pnum, cnum));
178     return False;
179   }
180 }
181 
182 /****************************************************************************
183   close an rpc pipe
184 ****************************************************************************/
get_rpc_pipe_num(char * buf,int where)185 int get_rpc_pipe_num(char *buf, int where)
186 {
187 	return (chain_pnum != -1 ? chain_pnum : SVAL(buf,where));
188 }
189 
190