Parionia to ensure people don't install libsmb based programs setuid root.
[ira/wip.git] / source / libsmb / clientgen.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client generic functions
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /*
27  * Change the port number used to call on 
28  */
29 int cli_set_port(struct cli_state *cli, int port)
30 {
31         cli->port = port;
32         return port;
33 }
34
35 /****************************************************************************
36 recv an smb
37 ****************************************************************************/
38 BOOL cli_receive_smb(struct cli_state *cli)
39 {
40         BOOL ret;
41  again:
42         ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
43         
44         if (ret) {
45                 /* it might be an oplock break request */
46                 if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
47                     CVAL(cli->inbuf,smb_com) == SMBlockingX &&
48                     SVAL(cli->inbuf,smb_vwv6) == 0 &&
49                     SVAL(cli->inbuf,smb_vwv7) == 0) {
50                         if (cli->oplock_handler) {
51                                 int fnum = SVAL(cli->inbuf,smb_vwv2);
52                                 unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
53                                 if (!cli->oplock_handler(cli, fnum, level)) return False;
54                         }
55                         /* try to prevent loops */
56                         CVAL(cli->inbuf,smb_com) = 0xFF;
57                         goto again;
58                 }
59         }
60
61         return ret;
62 }
63
64 /****************************************************************************
65   send an smb to a fd and re-establish if necessary
66 ****************************************************************************/
67 BOOL cli_send_smb(struct cli_state *cli)
68 {
69         size_t len;
70         size_t nwritten=0;
71         ssize_t ret;
72         BOOL reestablished=False;
73
74         len = smb_len(cli->outbuf) + 4;
75
76         while (nwritten < len) {
77                 ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
78                 if (ret <= 0 && errno == EPIPE && !reestablished) {
79                         if (cli_reestablish_connection(cli)) {
80                                 reestablished = True;
81                                 nwritten=0;
82                                 continue;
83                         }
84                 }
85                 if (ret <= 0) {
86                         DEBUG(0,("Error writing %d bytes to client. %d\n",
87                                  (int)len,(int)ret));
88                         return False;
89                 }
90                 nwritten += ret;
91         }
92         
93         return True;
94 }
95
96 /****************************************************************************
97 setup basics in a outgoing packet
98 ****************************************************************************/
99 void cli_setup_packet(struct cli_state *cli)
100 {
101         cli->rap_error = 0;
102         SSVAL(cli->outbuf,smb_pid,cli->pid);
103         SSVAL(cli->outbuf,smb_uid,cli->vuid);
104         SSVAL(cli->outbuf,smb_mid,cli->mid);
105         if (cli->protocol > PROTOCOL_CORE) {
106                 uint16 flags2;
107                 SCVAL(cli->outbuf,smb_flg,0x8);
108                 flags2 = FLAGS2_LONG_PATH_COMPONENTS;
109                 if (cli->capabilities & CAP_UNICODE) {
110                         flags2 |= FLAGS2_UNICODE_STRINGS;
111                 }
112                 if (cli->capabilities & CAP_STATUS32) {
113                         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
114                 }
115                 if (cli->use_spnego) {
116                         flags2 |= FLAGS2_EXTENDED_SECURITY;
117                 }
118                 SSVAL(cli->outbuf,smb_flg2, flags2);
119         }
120 }
121
122 /****************************************************************************
123 setup the bcc length of the packet from a pointer to the end of the data
124 ****************************************************************************/
125 void cli_setup_bcc(struct cli_state *cli, void *p)
126 {
127         set_message_bcc(cli->outbuf, PTR_DIFF(p, smb_buf(cli->outbuf)));
128 }
129
130
131
132 /****************************************************************************
133 initialise a client structure
134 ****************************************************************************/
135 void cli_init_creds(struct cli_state *cli, const struct ntuser_creds *usr)
136 {
137         /* copy_nt_creds(&cli->usr, usr); */
138         safe_strcpy(cli->domain   , usr->domain   , sizeof(usr->domain   )-1);
139         safe_strcpy(cli->user_name, usr->user_name, sizeof(usr->user_name)-1);
140         memcpy(&cli->pwd, &usr->pwd, sizeof(usr->pwd));
141         cli->ntlmssp_flags = usr->ntlmssp_flags;
142         cli->ntlmssp_cli_flgs = usr != NULL ? usr->ntlmssp_flags : 0;
143
144         DEBUG(10,("cli_init_creds: user %s domain %s flgs: %x\nntlmssp_cli_flgs:%x\n",
145                cli->user_name, cli->domain,
146                cli->ntlmssp_flags,cli->ntlmssp_cli_flgs));
147 }
148
149
150 /****************************************************************************
151 initialise a client structure
152 ****************************************************************************/
153 struct cli_state *cli_initialise(struct cli_state *cli)
154 {
155         BOOL alloced_cli = False;
156
157         /* Check the effective uid - make sure we are not setuid */
158         if (is_setuid_root()) {
159                 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
160                 return NULL;
161         }
162
163         if (!cli) {
164                 cli = (struct cli_state *)malloc(sizeof(*cli));
165                 if (!cli)
166                         return NULL;
167                 ZERO_STRUCTP(cli);
168                 alloced_cli = True;
169         }
170
171         if (cli->initialised) {
172                 cli_shutdown(cli);
173         }
174
175         ZERO_STRUCTP(cli);
176
177         cli->port = 0;
178         cli->fd = -1;
179         cli->cnum = -1;
180         cli->pid = (uint16)sys_getpid();
181         cli->mid = 1;
182         cli->vuid = UID_FIELD_INVALID;
183         cli->protocol = PROTOCOL_NT1;
184         cli->timeout = 20000; /* Timeout is in milliseconds. */
185         cli->bufsize = CLI_BUFFER_SIZE+4;
186         cli->max_xmit = cli->bufsize;
187         cli->outbuf = (char *)malloc(cli->bufsize);
188         cli->inbuf = (char *)malloc(cli->bufsize);
189         cli->oplock_handler = cli_oplock_ack;
190         cli->use_spnego = True;
191
192         if (!cli->outbuf || !cli->inbuf)
193                 goto error;
194
195         if ((cli->mem_ctx = talloc_init()) == NULL)
196                 goto error;
197
198         memset(cli->outbuf, 0, cli->bufsize);
199         memset(cli->inbuf, 0, cli->bufsize);
200
201         cli->nt_pipe_fnum = 0;
202
203         cli->initialised = 1;
204
205         return cli;
206
207         /* Clean up after malloc() error */
208
209  error:
210
211         SAFE_FREE(cli->inbuf);
212         SAFE_FREE(cli->outbuf);
213
214         if (alloced_cli)
215                 SAFE_FREE(cli);
216
217         return NULL;
218 }
219
220 /****************************************************************************
221 shutdown a client structure
222 ****************************************************************************/
223 void cli_shutdown(struct cli_state *cli)
224 {
225         SAFE_FREE(cli->outbuf);
226         SAFE_FREE(cli->inbuf);
227
228         data_blob_free(&cli->secblob);
229
230         if (cli->mem_ctx)
231                 talloc_destroy(cli->mem_ctx);
232
233 #ifdef WITH_SSL
234         if (cli->fd != -1)
235                 sslutil_disconnect(cli->fd);
236 #endif /* WITH_SSL */
237         if (cli->fd != -1) 
238                 close(cli->fd);
239         memset(cli, 0, sizeof(*cli));
240 }
241
242
243 /****************************************************************************
244 set socket options on a open connection
245 ****************************************************************************/
246 void cli_sockopt(struct cli_state *cli, char *options)
247 {
248         set_socket_options(cli->fd, options);
249 }
250
251 /****************************************************************************
252 set the PID to use for smb messages. Return the old pid.
253 ****************************************************************************/
254 uint16 cli_setpid(struct cli_state *cli, uint16 pid)
255 {
256         uint16 ret = cli->pid;
257         cli->pid = pid;
258         return ret;
259 }