2 Unix SMB/Netbios implementation.
4 SMB msrpcent generic functions
5 Copyright (C) Andrew Tridgell 1994-1999
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1999
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern int DEBUGLEVEL;
29 /****************************************************************************
30 read an msrpc pdu from a fd.
31 The timeout is in milliseconds.
32 ****************************************************************************/
33 BOOL receive_msrpc(int fd, prs_struct *data, unsigned int timeout)
39 prs_init(data, 0, NULL, True);
41 ok = prs_read(data, fd, 16, timeout);
49 if (!smb_io_rpc_hdr("hdr", &hdr, data, 0))
55 len = hdr.frag_len - 16;
58 ok = prs_read(data, fd, hdr.frag_len, 0);
64 data->data_offset = hdr.frag_len;
72 /****************************************************************************
73 send an smb to a fd and re-establish if necessary
74 ****************************************************************************/
75 BOOL msrpc_send(int fd, prs_struct *ps)
77 size_t len = ps != NULL ? ps->buffer_size : 0;
80 char *outbuf = ps->data_p;
82 DEBUG(10,("msrpc_send_prs: data: %p len %d\n", outbuf, len));
85 dump_data(10, outbuf, len);
87 while (nwritten < len)
89 ret = write_socket(fd,outbuf+nwritten,len - nwritten);
92 DEBUG(0,("Error writing %d msrpc bytes. %d.\n",
104 /****************************************************************************
106 ****************************************************************************/
107 BOOL msrpc_receive(int fd, prs_struct *ps)
111 DEBUG(10,("msrpc_receive: %d\n", __LINE__));
113 if (!receive_msrpc(fd, ps, 0))
118 len = ps->buffer_size;
120 if (ps->data_p == NULL || len <= 0)
125 dump_data(10, ps->data_p, len);
127 DEBUG(10,("msrpc_receive: len %d\n", len));
132 /****************************************************************************
133 open the msrpcent sockets
134 ****************************************************************************/
135 BOOL msrpc_connect(struct msrpc_state *msrpc, const char *pipe_name)
138 slprintf(path, sizeof(path)-1, "%s/.msrpc/%s", LOCKDIR, pipe_name);
140 fstrcpy(msrpc->pipe_name, pipe_name);
142 msrpc->fd = open_pipe_sock(path);
153 /****************************************************************************
154 initialise a msrpcent structure
155 ****************************************************************************/
156 void msrpc_init_creds(struct msrpc_state *msrpc, const struct user_creds *usr)
158 copy_user_creds(&msrpc->usr, usr);
161 /****************************************************************************
162 close the socket descriptor
163 ****************************************************************************/
164 void msrpc_close_socket(struct msrpc_state *msrpc)
174 /****************************************************************************
175 set socket options on a open connection
176 ****************************************************************************/
177 void msrpc_sockopt(struct msrpc_state *msrpc, char *options)
179 set_socket_options(msrpc->fd, options);
183 static BOOL msrpc_authenticate(struct msrpc_state *msrpc,
184 struct user_creds *usr)
186 struct msrpc_state msrpc_redir;
188 int sock = msrpc->fd;
192 char *in = msrpc->inbuf;
193 char *out = msrpc->outbuf;
196 command = usr != NULL ? AGENT_CMD_CON : AGENT_CMD_CON_ANON;
198 if (!create_user_creds(&ps, msrpc->pipe_name, 0x0, command,
201 DEBUG(0,("could not parse credentials\n"));
206 len = ps.data_offset;
211 #ifdef DEBUG_PASSWORD
212 DEBUG(100,("data len: %d\n", len));
213 dump_data(100, data, len);
216 if (write(sock, data, len) <= 0)
218 DEBUG(0,("write failed\n"));
224 len = read(sock, &msrpc_redir, sizeof(msrpc_redir));
226 if (len != sizeof(msrpc_redir))
228 DEBUG(0,("read failed\n"));
232 memcpy(msrpc, &msrpc_redir, sizeof(msrpc_redir));
236 msrpc->usr.reuse = False;
241 len = read(sock, &status, sizeof(status));
243 return len == sizeof(status) && status == 0x0;
248 static BOOL msrpc_init_redirect(struct msrpc_state *msrpc,
249 const char* pipe_name,
250 struct user_creds *usr)
255 slprintf(path, sizeof(path)-1, "/tmp/.msrpc/.%s/agent", pipe_name);
257 sock = open_pipe_sock(path);
266 if (!msrpc_authenticate(msrpc, usr))
268 DEBUG(0,("authenticate failed\n"));
277 BOOL msrpc_connect_auth(struct msrpc_state *msrpc,
279 const char* pipename,
280 const struct user_creds *usr)
283 if (!msrpc_initialise(msrpc, pid))
285 DEBUG(0,("unable to initialise msrpcent connection.\n"));
289 msrpc_init_creds(msrpc, usr);
291 if (!msrpc_establish_connection(msrpc, pipename))
293 msrpc_shutdown(msrpc);
300 /****************************************************************************
301 initialise a msrpcent structure
302 ****************************************************************************/
303 struct msrpc_state *msrpc_initialise(struct msrpc_state *msrpc, uint32 pid)
306 msrpc = (struct msrpc_state *)malloc(sizeof(*msrpc));
312 if (msrpc->initialised) {
313 msrpc_shutdown(msrpc);
319 msrpc->outbuf = (char *)malloc(CLI_BUFFER_SIZE+4);
320 msrpc->inbuf = (char *)malloc(CLI_BUFFER_SIZE+4);
321 if (!msrpc->outbuf || !msrpc->inbuf)
326 msrpc->initialised = 1;
327 msrpc_init_creds(msrpc, NULL);
334 /****************************************************************************
335 shutdown a msrpcent structure
336 ****************************************************************************/
337 void msrpc_shutdown(struct msrpc_state *msrpc)
339 DEBUG(10,("msrpc_shutdown\n"));
348 msrpc_close_socket(msrpc);
349 memset(msrpc, 0, sizeof(*msrpc));
352 /****************************************************************************
353 establishes a connection right up to doing tconX, reading in a password.
354 ****************************************************************************/
355 BOOL msrpc_establish_connection(struct msrpc_state *msrpc,
356 const char *pipe_name)
358 DEBUG(5,("msrpc_establish_connection: connecting to %s (%s) - %s\n",
360 msrpc->usr.ntc.user_name, msrpc->usr.ntc.domain));
362 /* establish connection */
364 if ((!msrpc->initialised))
369 if (msrpc->fd == -1 && msrpc->redirect)
371 if (msrpc_init_redirect(msrpc, pipe_name, &msrpc->usr))
373 DEBUG(10,("msrpc_establish_connection: redirected OK\n"));
378 DEBUG(10,("redirect FAILED\n"));
384 if (!msrpc_connect(msrpc, pipe_name))
386 DEBUG(1,("msrpc_establish_connection: failed %s)\n",
393 if (!msrpc_authenticate(msrpc, &msrpc->usr))
395 DEBUG(0,("authenticate failed\n"));