extern int DEBUGLEVEL;
+extern pstring user_socket_options;
+extern pstring scope;
+
+static void cli_process_oplock(struct cli_state *cli);
+
+/*
+ * Change the port number used to call on
+ */
+int cli_set_port(struct cli_state *cli, int port)
+{
+ if (port > 0)
+ cli->port = port;
+
+ return cli->port;
+}
+
+/****************************************************************************
+recv an smb
+****************************************************************************/
+static BOOL cli_receive_smb(struct cli_state *cli)
+{
+ BOOL ret;
+ again:
+ ret = client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+
+ if (ret) {
+ /* it might be an oplock break request */
+ if (CVAL(cli->inbuf,smb_com) == SMBlockingX &&
+ SVAL(cli->inbuf,smb_vwv6) == 0 &&
+ SVAL(cli->inbuf,smb_vwv7) == 0) {
+ if (cli->use_oplocks) cli_process_oplock(cli);
+ /* try to prevent loops */
+ CVAL(cli->inbuf,smb_com) = 0xFF;
+ goto again;
+ }
+ }
+
+ return ret;
+}
+
+/****************************************************************************
+ send an smb to a fd and re-establish if necessary
+****************************************************************************/
+static BOOL cli_send_smb(struct cli_state *cli)
+{
+ size_t len;
+ size_t nwritten=0;
+ ssize_t ret;
+ BOOL reestablished=False;
+
+ len = smb_len(cli->outbuf) + 4;
+
+ while (nwritten < len) {
+ ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
+ if (ret <= 0 && errno == EPIPE && !reestablished) {
+ if (cli_reestablish_connection(cli)) {
+ reestablished = True;
+ nwritten=0;
+ continue;
+ }
+ }
+ if (ret <= 0) {
+ DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",
+ (int)len,(int)ret));
+ close_sockets();
+ exit(1);
+ }
+ nwritten += ret;
+ }
+
+ return True;
+}
+
+/****************************************************************************
+setup basics in a outgoing packet
+****************************************************************************/
+static void cli_setup_packet(struct cli_state *cli)
+{
+ cli->rap_error = 0;
+ cli->nt_error = 0;
+ SSVAL(cli->outbuf,smb_pid,cli->pid);
+ SSVAL(cli->outbuf,smb_uid,cli->vuid);
+ SSVAL(cli->outbuf,smb_mid,cli->mid);
+ if (cli->protocol > PROTOCOL_CORE) {
+ SCVAL(cli->outbuf,smb_flg,0x8);
+ SSVAL(cli->outbuf,smb_flg2,0x1);
+ }
+}
+
+
+
+/****************************************************************************
+process an oplock break request from the server
+****************************************************************************/
+static void cli_process_oplock(struct cli_state *cli)
+{
+ char *oldbuf = cli->outbuf;
+ pstring buf;
+ int fnum;
+
+ fnum = SVAL(cli->inbuf,smb_vwv2);
+
+ /* damn, we really need to keep a record of open files so we
+ can detect a oplock break and a close crossing on the
+ wire. for now this swallows the errors */
+ if (fnum == 0) return;
+
+ cli->outbuf = buf;
+
+ memset(buf,'\0',smb_size);
+ set_message(buf,8,0,True);
+
+ CVAL(buf,smb_com) = SMBlockingX;
+ SSVAL(buf,smb_tid, cli->cnum);
+ cli_setup_packet(cli);
+ SSVAL(buf,smb_vwv0,0xFF);
+ SSVAL(buf,smb_vwv1,0);
+ SSVAL(buf,smb_vwv2,fnum);
+ SSVAL(buf,smb_vwv3,2); /* oplock break ack */
+ SIVAL(buf,smb_vwv4,0); /* timoeut */
+ SSVAL(buf,smb_vwv6,0); /* unlockcount */
+ SSVAL(buf,smb_vwv7,0); /* lockcount */
+
+ cli_send_smb(cli);
+
+ cli->outbuf = oldbuf;
+}
+
/*****************************************************
RAP error codes - a small start but will be extended.
{2244, "This password cannot be used now (password history conflict)." },
{2245, "The password is shorter than required." },
{2246, "The password of this user is too recent to change."},
+
+ /* these really shouldn't be here ... */
+ {0x80, "Not listening on called name"},
+ {0x81, "Not listening for calling name"},
+ {0x82, "Called name not present"},
+ {0x83, "Called name present, but insufficient resources"},
+
{0, NULL}
};
static fstring error_message;
uint8 errclass;
uint32 errnum;
+ uint32 nt_rpc_error;
int i;
/*
* errors, whose error code is in cli.rap_error.
*/
- cli_error(cli, &errclass, &errnum);
+ cli_error(cli, &errclass, &errnum, &nt_rpc_error);
if (errclass != 0)
{
* Was it an NT error ?
*/
- if (cli->nt_error)
+ if (nt_rpc_error)
{
- char *nt_msg = get_nt_error_msg(cli->nt_error);
+ char *nt_msg = get_nt_error_msg(nt_rpc_error);
if (nt_msg == NULL)
{
- slprintf(error_message, sizeof(fstring) - 1, "NT code %d", cli->nt_error);
+ slprintf(error_message, sizeof(fstring) - 1, "NT code %d", nt_rpc_error);
}
else
{
return error_message;
}
-/****************************************************************************
-setup basics in a outgoing packet
-****************************************************************************/
-static void cli_setup_packet(struct cli_state *cli)
-{
- cli->rap_error = 0;
- cli->nt_error = 0;
- SSVAL(cli->outbuf,smb_pid,cli->pid);
- SSVAL(cli->outbuf,smb_uid,cli->vuid);
- SSVAL(cli->outbuf,smb_mid,cli->mid);
- if (cli->protocol > PROTOCOL_CORE) {
- SCVAL(cli->outbuf,smb_flg,0x8);
- SSVAL(cli->outbuf,smb_flg2,0x1);
- }
-}
-
-
/*****************************************************************************
Convert a character pointer in a cli_call_api() response to a form we can use.
This function contains code to prevent core dumps if the server returns
this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
- bzero(cli->outbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
set_message(cli->outbuf,14+lsetup,0,True);
CVAL(cli->outbuf,smb_com) = trans;
SSVAL(cli->outbuf,smb_tid, cli->cnum);
PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
show_msg(cli->outbuf);
- send_smb(cli->fd,cli->outbuf);
+ cli_send_smb(cli);
if (this_ldata < ldata || this_lparam < lparam) {
/* receive interim response */
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout) ||
+ if (!cli_receive_smb(cli) ||
CVAL(cli->inbuf,smb_rcls) != 0) {
return(False);
}
if (trans==SMBtrans2)
SSVALS(cli->outbuf,smb_sfid,fid); /* fid */
if (this_lparam) /* param[] */
- memcpy(outparam,param,this_lparam);
+ memcpy(outparam,param+tot_param,this_lparam);
if (this_ldata) /* data[] */
- memcpy(outdata,data,this_ldata);
+ memcpy(outdata,data+tot_data,this_ldata);
set_message(cli->outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
show_msg(cli->outbuf);
- send_smb(cli->fd,cli->outbuf);
+ cli_send_smb(cli);
tot_data += this_ldata;
tot_param += this_lparam;
int total_data=0;
int total_param=0;
int this_data,this_param;
-
+ uint8 eclass;
+ uint32 ecode;
+
*data_len = *param_len = 0;
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ if (!cli_receive_smb(cli))
return False;
show_msg(cli->inbuf);
return(False);
}
- if (cli_error(cli, NULL, NULL))
+ /*
+ * An NT RPC pipe call can return ERRDOS, ERRmoredata
+ * to a trans call. This is not an error and should not
+ * be treated as such.
+ */
+
+ if (cli_error(cli, &eclass, &ecode, NULL))
{
- return(False);
+ if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
+ return(False);
}
/* parse out the lengths */
if (total_data <= *data_len && total_param <= *param_len)
break;
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ if (!cli_receive_smb(cli))
return False;
show_msg(cli->inbuf);
CVAL(cli->inbuf,smb_com)));
return(False);
}
- if (cli_error(cli, NULL, NULL))
+ if (cli_error(cli, &eclass, &ecode, NULL))
{
- return(False);
+ if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
+ return(False);
}
}
pstrcpy(p,user);
strupper(p);
p += 21;
- p++;
- p += 15;
- p++;
+ p++;
+ p += 15;
+ p++;
pstrcpy(p, workstation);
strupper(p);
p += 16;
if (cli->rap_error == 0) {
DEBUG(4,("NetWkstaUserLogon success\n"));
- cli->privilages = SVAL(p, 24);
+ cli->privileges = SVAL(p, 24);
fstrcpy(cli->eff_name,p+2);
} else {
DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
pstrcpy(p,"B13BWz");
p = skip_string(p,1);
SSVAL(p,0,1);
- SSVAL(p,2,CLI_BUFFER_SIZE);
+ SSVAL(p,2,0xFFFF);
p += 4;
if (cli_api(cli,
param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
- NULL, 0, CLI_BUFFER_SIZE, /* data, length, maxlen */
+ NULL, 0, 0xFFFF, /* data, length, maxlen */
&rparam, &rprcnt, /* return params, length */
&rdata, &rdrcnt)) /* return data, length */
{
int converter=SVAL(rparam,2);
int i;
- if (res == 0)
- {
- count=SVAL(rparam,4);
- p = rdata;
-
- for (i=0;i<count;i++,p+=20)
- {
- char *sname = p;
- int type = SVAL(p,14);
- int comment_offset = IVAL(p,16) & 0xFFFF;
- char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
- fn(sname, type, cmnt);
- }
- }
+ if (res == 0 || res == ERRmoredata) {
+ count=SVAL(rparam,4);
+ p = rdata;
+
+ for (i=0;i<count;i++,p+=20) {
+ char *sname = p;
+ int type = SVAL(p,14);
+ int comment_offset = IVAL(p,16) & 0xFFFF;
+ char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
+ dos_to_unix(sname,True);
+ dos_to_unix(cmnt,True);
+ fn(sname, type, cmnt);
+ }
+ } else {
+ DEBUG(4,("NetShareEnum res=%d\n", res));
+ }
+ } else {
+ DEBUG(4,("NetShareEnum failed\n"));
}
if (rparam)
int converter=SVAL(rparam,2);
int i;
- if (res == 0) {
+ if (res == 0 || res == ERRmoredata) {
count=SVAL(rparam,4);
p = rdata;
stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
+ dos_to_unix(sname, True);
+ dos_to_unix(cmnt, True);
fn(sname, stype, cmnt);
}
}
{PROTOCOL_LANMAN1,"LANMAN1.0"},
{PROTOCOL_LANMAN2,"LM1.2X002"},
{PROTOCOL_LANMAN2,"Samba"},
- {PROTOCOL_NT1,"NT LM 0.12"},
{PROTOCOL_NT1,"NT LANMAN 1.0"},
+ {PROTOCOL_NT1,"NT LM 0.12"},
{-1,NULL}
};
/****************************************************************************
-send a session setup
+ Send a session setup. The username is in UNIX character format and must be
+ converted to DOS codepage format before sending. If the password is in
+ plaintext, the same should be done.
****************************************************************************/
+
BOOL cli_session_setup(struct cli_state *cli,
char *user,
char *pass, int passlen,
return False;
}
- if (((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
- /* Null session connect. */
- pword[0] = '\0';
- ntpword[0] = '\0';
- } else {
- if ((cli->sec_mode & 2) && passlen != 24) {
- passlen = 24;
- ntpasslen = 24;
- SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
- SMBNTencrypt((uchar *)ntpass,(uchar *)cli->cryptkey,(uchar *)ntpword);
- } else {
- fstrcpy(pword, pass);
- fstrcpy(ntpword, "");
- ntpasslen = 0;
- }
- }
+ if (((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
+ /* Null session connect. */
+ pword[0] = '\0';
+ ntpword[0] = '\0';
+ } else {
+ if ((cli->sec_mode & 2) && passlen != 24) {
+ /*
+ * Encrypted mode needed, and non encrypted password supplied.
+ */
+ passlen = 24;
+ ntpasslen = 24;
+ fstrcpy(pword, pass);
+ unix_to_dos(pword,True);
+ fstrcpy(ntpword, ntpass);;
+ unix_to_dos(ntpword,True);
+ SMBencrypt((uchar *)pword,(uchar *)cli->cryptkey,(uchar *)pword);
+ SMBNTencrypt((uchar *)ntpword,(uchar *)cli->cryptkey,(uchar *)ntpword);
+ } else if ((cli->sec_mode & 2) && passlen == 24) {
+ /*
+ * Encrypted mode needed, and encrypted password supplied.
+ */
+ memcpy(pword, pass, passlen);
+ if(ntpasslen == 24) {
+ memcpy(ntpword, ntpass, ntpasslen);
+ } else {
+ fstrcpy(ntpword, "");
+ ntpasslen = 0;
+ }
+ } else {
+ /*
+ * Plaintext mode needed, assume plaintext supplied.
+ */
+ fstrcpy(pword, pass);
+ unix_to_dos(pword,True);
+ fstrcpy(ntpword, "");
+ ntpasslen = 0;
+ }
+ }
/* if in share level security then don't send a password now */
if (!(cli->sec_mode & 1)) {
}
/* send a session setup command */
- bzero(cli->outbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
if (cli->protocol < PROTOCOL_NT1)
{
memcpy(p,pword,passlen);
p += passlen;
pstrcpy(p,user);
+ unix_to_dos(p,True);
strupper(p);
}
else
SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
SSVAL(cli->outbuf,smb_vwv7,passlen);
SSVAL(cli->outbuf,smb_vwv8,ntpasslen);
- SSVAL(cli->outbuf,smb_vwv11,CAP_STATUS32);
+ SSVAL(cli->outbuf,smb_vwv11,0);
p = smb_buf(cli->outbuf);
memcpy(p,pword,passlen);
p += SVAL(cli->outbuf,smb_vwv7);
memcpy(p,ntpword,ntpasslen);
p += SVAL(cli->outbuf,smb_vwv8);
pstrcpy(p,user);
+ unix_to_dos(p,True);
strupper(p);
p = skip_string(p,1);
pstrcpy(p,workgroup);
set_message(cli->outbuf,13,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
}
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli))
return False;
show_msg(cli->inbuf);
/* use the returned vuid from now on */
cli->vuid = SVAL(cli->inbuf,smb_uid);
+ if (cli->protocol >= PROTOCOL_NT1) {
+ /*
+ * Save off some of the connected server
+ * info.
+ */
+ char *server_domain,*server_os,*server_type;
+ server_os = smb_buf(cli->inbuf);
+ server_type = skip_string(server_os,1);
+ server_domain = skip_string(server_type,1);
+ fstrcpy(cli->server_os, server_os);
+ dos_to_unix(cli->server_os, True);
+ fstrcpy(cli->server_type, server_type);
+ dos_to_unix(cli->server_type, True);
+ fstrcpy(cli->server_domain, server_domain);
+ dos_to_unix(cli->server_domain, True);
+ }
+
+ fstrcpy(cli->user_name, user);
+ dos_to_unix(cli->user_name, True);
+
return True;
}
BOOL cli_ulogoff(struct cli_state *cli)
{
- bzero(cli->outbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
set_message(cli->outbuf,2,0,True);
CVAL(cli->outbuf,smb_com) = SMBulogoffX;
cli_setup_packet(cli);
SSVAL(cli->outbuf,smb_vwv0,0xFF);
SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli))
return False;
return CVAL(cli->inbuf,smb_rcls) == 0;
BOOL cli_send_tconX(struct cli_state *cli,
char *share, char *dev, char *pass, int passlen)
{
- fstring fullshare, pword;
+ fstring fullshare, pword, dos_pword;
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
fstrcpy(cli->share, share);
}
if ((cli->sec_mode & 2) && *pass && passlen != 24) {
+ /*
+ * Non-encrypted passwords - convert to DOS codepage before encryption.
+ */
passlen = 24;
- SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
+ fstrcpy(dos_pword,pass);
+ unix_to_dos(dos_pword,True);
+ SMBencrypt((uchar *)dos_pword,(uchar *)cli->cryptkey,(uchar *)pword);
} else {
- memcpy(pword, pass, passlen);
+ if(!(cli->sec_mode & 2)) {
+ /*
+ * Non-encrypted passwords - convert to DOS codepage before using.
+ */
+ fstrcpy(pword,pass);
+ unix_to_dos(pword,True);
+ } else {
+ memcpy(pword, pass, passlen);
+ }
}
slprintf(fullshare, sizeof(fullshare)-1,
"\\\\%s\\%s", cli->desthost, share);
+ unix_to_dos(fullshare, True);
+ strupper(fullshare);
set_message(cli->outbuf,4,
2 + strlen(fullshare) + passlen + strlen(dev),True);
fstrcpy(p,fullshare);
p = skip_string(p,1);
pstrcpy(p,dev);
+ unix_to_dos(p,True);
SCVAL(cli->inbuf,smb_rcls, 1);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli))
return False;
if (CVAL(cli->inbuf,smb_rcls) != 0) {
return False;
}
- fstrcpy(cli->dev, smb_buf(cli->inbuf));
+ fstrcpy(cli->dev, "A:");
+
+ if (cli->protocol >= PROTOCOL_NT1) {
+ fstrcpy(cli->dev, smb_buf(cli->inbuf));
+ }
+
+ if (strcasecmp(share,"IPC$")==0) {
+ fstrcpy(cli->dev, "IPC");
+ }
+ /* only grab the device if we have a recent protocol level */
if (cli->protocol >= PROTOCOL_NT1 &&
smb_buflen(cli->inbuf) == 3) {
/* almost certainly win95 - enable bug fixes */
****************************************************************************/
BOOL cli_tdis(struct cli_state *cli)
{
- bzero(cli->outbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
set_message(cli->outbuf,0,0,True);
CVAL(cli->outbuf,smb_com) = SMBtdis;
SSVAL(cli->outbuf,smb_tid,cli->cnum);
cli_setup_packet(cli);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli))
return False;
return CVAL(cli->inbuf,smb_rcls) == 0;
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,1, 4 + strlen(fname_src) + strlen(fname_dst), True);
p = smb_buf(cli->outbuf);
*p++ = 4;
pstrcpy(p,fname_src);
+ unix_to_dos(p,True);
p = skip_string(p,1);
*p++ = 4;
pstrcpy(p,fname_dst);
+ unix_to_dos(p,True);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,1, 2 + strlen(fname),True);
p = smb_buf(cli->outbuf);
*p++ = 4;
pstrcpy(p,fname);
+ unix_to_dos(p,True);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,0, 2 + strlen(dname),True);
p = smb_buf(cli->outbuf);
*p++ = 4;
pstrcpy(p,dname);
+ unix_to_dos(p,True);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,0, 2 + strlen(dname),True);
p = smb_buf(cli->outbuf);
*p++ = 4;
pstrcpy(p,dname);
+ unix_to_dos(p,True);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
+/****************************************************************************
+open a file
+****************************************************************************/
+int cli_nt_create(struct cli_state *cli, char *fname)
+{
+ char *p;
+
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
+
+ set_message(cli->outbuf,24,1 + strlen(fname),True);
+
+ CVAL(cli->outbuf,smb_com) = SMBntcreateX;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ SSVAL(cli->outbuf,smb_vwv0,0xFF);
+ SIVAL(cli->outbuf,smb_ntcreate_Flags, 0x06);
+ SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
+ SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, 0x2019f);
+ SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, 0x0);
+ SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, 0x03);
+ SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, 0x01);
+ SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, 0x0);
+ SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
+ SSVAL(cli->outbuf,smb_ntcreate_NameLength, strlen(fname));
+
+ p = smb_buf(cli->outbuf);
+ pstrcpy(p,fname);
+ unix_to_dos(p,True);
+ p = skip_string(p,1);
+
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
+ return -1;
+ }
+
+ if (CVAL(cli->inbuf,smb_rcls) != 0) {
+ return -1;
+ }
+
+ return SVAL(cli->inbuf,smb_vwv2 + 1);
+}
+
+
/****************************************************************************
open a file
****************************************************************************/
/* you must open for RW not just write - otherwise getattrE doesn't
work! */
- if ((flags & O_ACCMODE) == O_WRONLY) {
+ if ((flags & O_ACCMODE) == O_WRONLY && strncmp(cli->dev, "LPT", 3)) {
flags = (flags & ~O_ACCMODE) | O_RDWR;
}
}
#endif /* O_SYNC */
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,15,1 + strlen(fname),True);
SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
SSVAL(cli->outbuf,smb_vwv5,0);
SSVAL(cli->outbuf,smb_vwv8,openfn);
+
+ if (cli->use_oplocks) {
+ /* if using oplocks then ask for a batch oplock via
+ core and extended methods */
+ CVAL(cli->outbuf,smb_flg) |=
+ FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
+ SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
+ }
p = smb_buf(cli->outbuf);
pstrcpy(p,fname);
+ unix_to_dos(p,True);
p = skip_string(p,1);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return -1;
}
****************************************************************************/
BOOL cli_close(struct cli_state *cli, int fnum)
{
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,3,0,True);
SSVAL(cli->outbuf,smb_vwv0,fnum);
SIVALS(cli->outbuf,smb_vwv1,-1);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
BOOL cli_lock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
{
char *p;
+ int saved_timeout = cli->timeout;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0', smb_size);
set_message(cli->outbuf,8,10,True);
SSVAL(p, 0, cli->pid);
SIVAL(p, 2, offset);
SIVAL(p, 6, len);
+ cli_send_smb(cli);
+
+ cli->timeout = (timeout == -1) ? 0x7FFFFFFF : timeout;
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ if (!cli_receive_smb(cli)) {
+ cli->timeout = saved_timeout;
return False;
}
+ cli->timeout = saved_timeout;
+
if (CVAL(cli->inbuf,smb_rcls) != 0) {
return False;
}
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,8,10,True);
SIVAL(p, 2, offset);
SIVAL(p, 6, len);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
size_t size, int i)
{
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,10,0,True);
SSVAL(cli->outbuf,smb_vwv6,size);
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
- send_smb(cli->fd,cli->outbuf);
+ cli_send_smb(cli);
}
/****************************************************************************
int total = -1;
int issued=0;
int received=0;
- int mpx = MAX(cli->max_mux-1, 1);
+/*
+ * There is a problem in this code when mpx is more than one.
+ * for some reason files can get corrupted when being read.
+ * Until we understand this fully I am serializing reads (one
+ * read/one reply) for now. JRA.
+ */
+#if 0
+ int mpx = MAX(cli->max_mux-1, 1);
+#else
+ int mpx = 1;
+#endif
int block = (cli->max_xmit - (smb_size+32)) & ~1023;
int mid;
int blocks = (size + (block-1)) / block;
issued++;
}
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ if (!cli_receive_smb(cli)) {
return total;
}
}
while (received < issued) {
- client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+ cli_receive_smb(cli);
received++;
}
/****************************************************************************
issue a single SMBwrite and don't wait for a reply
****************************************************************************/
-static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, char *buf,
+static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
size_t size, int i)
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,12,size,True);
CVAL(cli->outbuf,smb_vwv0) = 0xFF;
SSVAL(cli->outbuf,smb_vwv2,fnum);
+
SIVAL(cli->outbuf,smb_vwv3,offset);
-
+ SIVAL(cli->outbuf,smb_vwv5,IS_BITS_SET_ALL(mode, 0x0008) ? 0xFFFFFFFF : 0);
+ SSVAL(cli->outbuf,smb_vwv7,mode);
+
+ SSVAL(cli->outbuf,smb_vwv8,IS_BITS_SET_ALL(mode, 0x0008) ? size : 0);
SSVAL(cli->outbuf,smb_vwv10,size);
SSVAL(cli->outbuf,smb_vwv11,
smb_buf(cli->outbuf) - smb_base(cli->outbuf));
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
- send_smb(cli->fd,cli->outbuf);
+ show_msg(cli->outbuf);
+ cli_send_smb(cli);
}
/****************************************************************************
write to a file
+ write_mode: 0x0001 disallow write cacheing
+ 0x0002 return bytes remaining
+ 0x0004 use raw named pipe protocol
+ 0x0008 start of message mode named pipe protocol
****************************************************************************/
-size_t cli_write(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
+ssize_t cli_write(struct cli_state *cli,
+ int fnum, uint16 write_mode,
+ char *buf, off_t offset, size_t size)
{
- int total = -1;
- int issued=0;
- int received=0;
+ int bwritten = 0;
+ int issued = 0;
+ int received = 0;
int mpx = MAX(cli->max_mux-1, 1);
int block = (cli->max_xmit - (smb_size+32)) & ~1023;
- int mid;
int blocks = (size + (block-1)) / block;
- if (size == 0) return 0;
-
while (received < blocks) {
- int size2;
- while (issued - received < mpx && issued < blocks) {
- int size1 = MIN(block, size-issued*block);
- cli_issue_write(cli, fnum, offset+issued*block, buf + issued*block,
+ while ((issued - received < mpx) && (issued < blocks))
+ {
+ int bsent = issued * block;
+ int size1 = MIN(block, size - bsent);
+
+ cli_issue_write(cli, fnum, offset + bsent,
+ write_mode,
+ buf + bsent,
size1, issued);
issued++;
}
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
- return total;
+ if (!cli_receive_smb(cli))
+ {
+ return bwritten;
}
received++;
- mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
- size2 = SVAL(cli->inbuf, smb_vwv2);
-
- if (CVAL(cli->inbuf,smb_rcls) != 0) {
- blocks = MIN(blocks, mid-1);
- continue;
- }
- if (size2 <= 0) {
- blocks = MIN(blocks, mid-1);
- /* this distinguishes EOF from an error */
- total = MAX(total, 0);
- continue;
+ if (CVAL(cli->inbuf,smb_rcls) != 0)
+ {
+ break;
}
- total += size2;
-
- total = MAX(total, mid*block + size2);
+ bwritten += SVAL(cli->inbuf, smb_vwv2);
}
- while (received < issued) {
- client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
+ while (received < issued && cli_receive_smb(cli))
+ {
received++;
}
+ return bwritten;
+}
+
+
+/****************************************************************************
+ write to a file using a SMBwrite and not bypassing 0 byte writes
+****************************************************************************/
+ssize_t cli_smbwrite(struct cli_state *cli,
+ int fnum, char *buf, off_t offset, size_t size1)
+{
+ char *p;
+ ssize_t total = 0;
+
+ do {
+ size_t size = MIN(size1, cli->max_xmit - 48);
+
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
+
+ set_message(cli->outbuf,5, 3 + size,True);
+
+ CVAL(cli->outbuf,smb_com) = SMBwrite;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ SSVAL(cli->outbuf,smb_vwv0,fnum);
+ SSVAL(cli->outbuf,smb_vwv1,size);
+ SIVAL(cli->outbuf,smb_vwv2,offset);
+ SSVAL(cli->outbuf,smb_vwv4,0);
+
+ p = smb_buf(cli->outbuf);
+ *p++ = 1;
+ SSVAL(p, 0, size);
+ memcpy(p+2, buf, size);
+
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
+ return -1;
+ }
+
+ if (CVAL(cli->inbuf,smb_rcls) != 0) {
+ return -1;
+ }
+
+ size = SVAL(cli->inbuf,smb_vwv0);
+ if (size == 0) break;
+
+ size1 -= size;
+ total += size;
+ } while (size1);
+
return total;
}
do a SMBgetattrE call
****************************************************************************/
BOOL cli_getattrE(struct cli_state *cli, int fd,
- uint32 *attr, size_t *size,
+ uint16 *attr, size_t *size,
time_t *c_time, time_t *a_time, time_t *m_time)
{
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
- set_message(cli->outbuf,2,0,True);
+ set_message(cli->outbuf,1,0,True);
CVAL(cli->outbuf,smb_com) = SMBgetattrE;
SSVAL(cli->outbuf,smb_tid,cli->cnum);
SSVAL(cli->outbuf,smb_vwv0,fd);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
do a SMBgetatr call
****************************************************************************/
BOOL cli_getatr(struct cli_state *cli, char *fname,
- uint32 *attr, size_t *size, time_t *t)
+ uint16 *attr, size_t *size, time_t *t)
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,0,strlen(fname)+2,True);
p = smb_buf(cli->outbuf);
*p = 4;
pstrcpy(p+1, fname);
+ unix_to_dos(p+1,True);
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
/****************************************************************************
do a SMBsetatr call
****************************************************************************/
-BOOL cli_setatr(struct cli_state *cli, char *fname, int attr, time_t t)
+BOOL cli_setatr(struct cli_state *cli, char *fname, uint16 attr, time_t t)
{
char *p;
- bzero(cli->outbuf,smb_size);
- bzero(cli->inbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
set_message(cli->outbuf,8,strlen(fname)+4,True);
p = smb_buf(cli->outbuf);
*p = 4;
pstrcpy(p+1, fname);
+ unix_to_dos(p+1,True);
p = skip_string(p,1);
*p = 4;
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
return False;
}
****************************************************************************/
BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
time_t *c_time, time_t *a_time, time_t *m_time,
- size_t *size, uint32 *mode)
+ size_t *size, uint16 *mode)
{
int data_len = 0;
int param_len = 0;
memset(param, 0, param_len);
SSVAL(param, 0, SMB_INFO_STANDARD);
pstrcpy(¶m[6], fname);
+ unix_to_dos(¶m[6],True);
do {
ret = (cli_send_trans(cli, SMBtrans2,
it gives ERRSRV/ERRerror temprarily */
uint8 eclass;
uint32 ecode;
- cli_error(cli, &eclass, &ecode);
+ cli_error(cli, &eclass, &ecode, NULL);
if (eclass != ERRSRV || ecode != ERRerror) break;
msleep(100);
}
****************************************************************************/
BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
time_t *c_time, time_t *a_time, time_t *m_time,
- time_t *w_time, size_t *size, uint32 *mode)
+ time_t *w_time, size_t *size, uint16 *mode,
+ SMB_INO_T *ino)
{
int data_len = 0;
int param_len = 0;
memset(param, 0, param_len);
SSVAL(param, 0, SMB_QUERY_FILE_ALL_INFO);
pstrcpy(¶m[6], fname);
+ unix_to_dos(¶m[6],True);
if (!cli_send_trans(cli, SMBtrans2,
NULL, 0, /* name, length */
if (w_time) {
*w_time = interpret_long_date(rdata+24) - cli->serverzone;
}
+ if (mode) {
+ *mode = SVAL(rdata, 32);
+ }
if (size) {
- *size = IVAL(rdata, 40);
+ *size = IVAL(rdata, 48);
}
- if (mode) {
- *mode = IVAL(rdata, 32);
+ if (ino) {
+ *ino = IVAL(rdata, 64);
}
if (rdata) free(rdata);
send a qfileinfo call
****************************************************************************/
BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
- uint32 *mode, size_t *size,
- time_t *c_time, time_t *a_time, time_t *m_time)
+ uint16 *mode, size_t *size,
+ time_t *c_time, time_t *a_time, time_t *m_time,
+ time_t *w_time, SMB_INO_T *ino)
{
int data_len = 0;
int param_len = 0;
pstring param;
char *rparam=NULL, *rdata=NULL;
+ /* if its a win95 server then fail this - win95 totally screws it
+ up */
+ if (cli->win95) return False;
+
param_len = 4;
memset(param, 0, param_len);
SSVAL(param, 0, fnum);
- SSVAL(param, 2, SMB_INFO_STANDARD);
+ SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
if (!cli_send_trans(cli, SMBtrans2,
NULL, 0, /* name, length */
return False;
}
- if (!rdata || data_len < 22) {
+ if (!rdata || data_len < 68) {
return False;
}
if (c_time) {
- *c_time = make_unix_date2(rdata+0);
+ *c_time = interpret_long_date(rdata+0) - cli->serverzone;
}
if (a_time) {
- *a_time = make_unix_date2(rdata+4);
+ *a_time = interpret_long_date(rdata+8) - cli->serverzone;
}
if (m_time) {
- *m_time = make_unix_date2(rdata+8);
+ *m_time = interpret_long_date(rdata+16) - cli->serverzone;
}
- if (size) {
- *size = IVAL(rdata, 12);
+ if (w_time) {
+ *w_time = interpret_long_date(rdata+24) - cli->serverzone;
}
if (mode) {
- *mode = SVAL(rdata,l1_attrFile);
+ *mode = SVAL(rdata, 32);
+ }
+ if (size) {
+ *size = IVAL(rdata, 48);
+ }
+ if (ino) {
+ *ino = IVAL(rdata, 64);
}
if (rdata) free(rdata);
finfo->size = IVAL(p,16);
finfo->mode = CVAL(p,24);
pstrcpy(finfo->name,p+27);
+ dos_to_unix(finfo->name,True);
}
return(28 + CVAL(p,26));
finfo->size = IVAL(p,16);
finfo->mode = CVAL(p,24);
pstrcpy(finfo->name,p+31);
+ dos_to_unix(finfo->name,True);
}
return(32 + CVAL(p,30));
finfo->size = IVAL(p,20);
finfo->mode = CVAL(p,28);
pstrcpy(finfo->name,p+33);
+ dos_to_unix(finfo->name,True);
}
return(SVAL(p,4)+4);
finfo->size = IVAL(p,20);
finfo->mode = CVAL(p,28);
pstrcpy(finfo->name,p+37);
+ dos_to_unix(finfo->name,True);
}
return(SVAL(p,4)+4);
p += 4; /* EA size */
p += 2; /* short name len? */
p += 24; /* short name? */
- StrnCpy(finfo->name,p,namelen);
+ StrnCpy(finfo->name,p,MIN(sizeof(finfo->name)-1,namelen));
+ dos_to_unix(finfo->name,True);
return(ret);
}
return(SVAL(p,0));
/****************************************************************************
do a directory listing, calling fn on each file found
****************************************************************************/
-int cli_list(struct cli_state *cli,char *Mask,int attribute,void (*fn)(file_info *))
+int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute,
+ void (*fn)(file_info *, const char *))
{
int max_matches = 512;
/* NT uses 260, OS/2 uses 2. Both accept 1. */
int dirlist_len = 0;
int total_received = -1;
BOOL First = True;
- int ff_resume_key = 0;
int ff_searchcount=0;
int ff_eos=0;
int ff_lastname=0;
pstring param;
pstrcpy(mask,Mask);
+ unix_to_dos(mask,True);
while (ff_eos == 0) {
loop_count++;
SSVAL(param,0,ff_dir_handle);
SSVAL(param,2,max_matches); /* max count */
SSVAL(param,4,info_level);
- SIVAL(param,6,ff_resume_key); /* ff_resume_key */
+ SIVAL(param,6,0); /* ff_resume_key */
SSVAL(param,10,8+4+2); /* resume required + close on end + continue */
pstrcpy(param+12,mask);
- DEBUG(5,("hand=0x%X resume=%d ff_lastname=%d mask=%s\n",
- ff_dir_handle,ff_resume_key,ff_lastname,mask));
+ DEBUG(5,("hand=0x%X ff_lastname=%d mask=%s\n",
+ ff_dir_handle,ff_lastname,mask));
}
if (!cli_send_trans(cli, SMBtrans2,
it gives ERRSRV/ERRerror temprarily */
uint8 eclass;
uint32 ecode;
- cli_error(cli, &eclass, &ecode);
+ cli_error(cli, &eclass, &ecode, NULL);
if (eclass != ERRSRV || ecode != ERRerror) break;
msleep(100);
continue;
switch(info_level)
{
case 260:
- ff_resume_key =0;
StrnCpy(mask,p+ff_lastname,
- data_len-ff_lastname);
+ MIN(sizeof(mask)-1,data_len-ff_lastname));
break;
case 1:
pstrcpy(mask,p + ff_lastname + 1);
- ff_resume_key = 0;
break;
}
} else {
pstrcpy(mask,"");
}
-
+
+ dos_to_unix(mask, True);
+
/* and add them to the dirlist pool */
dirlist = Realloc(dirlist,dirlist_len + data_len);
if (rdata) free(rdata); rdata = NULL;
if (rparam) free(rparam); rparam = NULL;
- DEBUG(3,("received %d entries (eos=%d resume=%d)\n",
- ff_searchcount,ff_eos,ff_resume_key));
+ DEBUG(3,("received %d entries (eos=%d)\n",
+ ff_searchcount,ff_eos));
+
+ if (ff_searchcount > 0) loop_count = 0;
First = False;
}
for (p=dirlist,i=0;i<total_received;i++) {
p += interpret_long_filename(info_level,p,&finfo);
- fn(&finfo);
+ fn(&finfo, Mask);
}
/* free up the dirlist buffer */
Send a SamOEMChangePassword command
****************************************************************************/
-BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_password,
- char *old_password)
+BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
+ const char *old_password)
{
char param[16+sizeof(fstring)];
char data[532];
unsigned char new_pw_hash[16];
int data_len;
int param_len = 0;
- int new_pw_len = strlen(new_password);
char *rparam = NULL;
char *rdata = NULL;
int rprcnt, rdrcnt;
+ pstring dos_new_password;
if (strlen(user) >= sizeof(fstring)-1) {
DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
return False;
}
- if (new_pw_len > 512) {
- DEBUG(0,("cli_oem_change_password: new password for user %s is too long.\n", user));
- return False;
- }
-
SSVAL(p,0,214); /* SamOEMChangePassword command. */
p += 2;
pstrcpy(p, "zsT");
param_len = PTR_DIFF(p,param);
- /*
- * Now setup the data area.
- * We need to generate a random fill
- * for this area to make it harder to
- * decrypt. JRA.
- */
- generate_random_buffer((unsigned char *)data, sizeof(data), False);
- fstrcpy( &data[512 - new_pw_len], new_password);
- SIVAL(data, 512, new_pw_len);
-
/*
* Get the Lanman hash of the old password, we
- * use this as the key to SamOEMHash().
+ * use this as the key to make_oem_passwd_hash().
*/
memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
fstrcpy(upper_case_old_pw, old_password);
+ unix_to_dos(upper_case_old_pw,True);
strupper(upper_case_old_pw);
E_P16((uchar *)upper_case_old_pw, old_pw_hash);
- SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, True);
+ pstrcpy(dos_new_password, new_password);
+ unix_to_dos(dos_new_password, True);
+
+ if (!make_oem_passwd_hash( data, dos_new_password, old_pw_hash, False))
+ return False;
/*
* Now place the old password hash in the data.
*/
memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
fstrcpy(upper_case_new_pw, new_password);
+ unix_to_dos(upper_case_new_pw,True);
strupper(upper_case_new_pw);
E_P16((uchar *)upper_case_new_pw, new_pw_hash);
int numprots;
int plength;
- bzero(cli->outbuf,smb_size);
+ memset(cli->outbuf,'\0',smb_size);
/* setup the protocol strings */
for (plength=0,numprots=0;
numprots++) {
*p++ = 2;
pstrcpy(p,prots[numprots].name);
+ unix_to_dos(p,True);
p += strlen(p) + 1;
}
CVAL(smb_buf(cli->outbuf),0) = 2;
- send_smb(cli->fd,cli->outbuf);
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli))
return False;
show_msg(cli->inbuf);
cli->max_mux = SVAL(cli->inbuf, smb_vwv1+1);
cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
- cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1)*60;
+ cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1);
+ cli->serverzone *= 60;
/* this time arrives in real GMT */
cli->servertime = interpret_long_date(cli->inbuf+smb_vwv11+1);
memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
- cli->serverzone = SVALS(cli->inbuf,smb_vwv10)*60;
+ cli->serverzone = SVALS(cli->inbuf,smb_vwv10);
+ cli->serverzone *= 60;
/* this time is converted to GMT by make_unix_date */
cli->servertime = make_unix_date(cli->inbuf+smb_vwv8);
cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
retry:
#endif /* WITH_SSL */
- send_smb(cli->fd,cli->outbuf);
+ cli_send_smb(cli);
DEBUG(5,("Sent session request\n"));
- if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
+ if (!cli_receive_smb(cli))
return False;
+ if (CVAL(cli->inbuf,0) == 0x84) {
+ /* C. Hoch 9/14/95 Start */
+ /* For information, here is the response structure.
+ * We do the byte-twiddling to for portability.
+ struct RetargetResponse{
+ unsigned char type;
+ unsigned char flags;
+ int16 length;
+ int32 ip_addr;
+ int16 port;
+ };
+ */
+ int port = (CVAL(cli->inbuf,8)<<8)+CVAL(cli->inbuf,9);
+ /* SESSION RETARGET */
+ putip((char *)&cli->dest_ip,cli->inbuf+4);
+
+ close_sockets();
+ cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, port, LONG_CONNECT_TIMEOUT);
+ if (cli->fd == -1)
+ return False;
+
+ DEBUG(3,("Retargeted\n"));
+
+ set_socket_options(cli->fd,user_socket_options);
+
+ /* Try again */
+ return cli_session_request(cli, calling, called);
+ } /* C. Hoch 9/14/95 End */
+
#ifdef WITH_SSL
if (CVAL(cli->inbuf,0) == 0x83 && CVAL(cli->inbuf,4) == 0x8e){ /* use ssl */
if (!sslutil_fd_is_ssl(cli->fd)){
if (CVAL(cli->inbuf,0) != 0x82) {
/* This is the wrong place to put the error... JRA. */
- cli->rap_error = CVAL(cli->inbuf,0);
+ cli->rap_error = CVAL(cli->inbuf,4);
return False;
}
return(True);
/****************************************************************************
open the client sockets
****************************************************************************/
-BOOL cli_connect(struct cli_state *cli, char *host, struct in_addr *ip)
+BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
{
- struct in_addr dest_ip;
extern struct in_addr ipzero;
fstrcpy(cli->desthost, host);
if (!ip || ip_equal(*ip, ipzero)) {
- if (!resolve_name( cli->desthost, &dest_ip, 0x20)) {
+ if (!resolve_name( cli->desthost, &cli->dest_ip, 0x20)) {
return False;
}
+ if (ip) *ip = cli->dest_ip;
} else {
- dest_ip = *ip;
+ cli->dest_ip = *ip;
}
+ if (cli->port == 0) cli->port = 139; /* Set to default */
- cli->fd = open_socket_out(SOCK_STREAM, &dest_ip, 139, cli->timeout);
+ cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip,
+ cli->port, cli->timeout);
if (cli->fd == -1)
return False;
+ set_socket_options(cli->fd,user_socket_options);
+
return True;
}
/****************************************************************************
initialise a client structure
****************************************************************************/
-BOOL cli_initialise(struct cli_state *cli)
+struct cli_state *cli_initialise(struct cli_state *cli)
{
- if (cli->initialised)
- cli_shutdown(cli);
+ if (!cli) {
+ cli = (struct cli_state *)malloc(sizeof(*cli));
+ if (!cli)
+ return NULL;
+ ZERO_STRUCTP(cli);
+ }
- memset(cli, 0, sizeof(*cli));
+ if (cli->initialised) {
+ cli_shutdown(cli);
+ }
+
+ ZERO_STRUCTP(cli);
+
+ cli->port = 0;
cli->fd = -1;
cli->cnum = -1;
cli->pid = (uint16)getpid();
cli->mid = 1;
cli->vuid = UID_FIELD_INVALID;
cli->protocol = PROTOCOL_NT1;
- cli->timeout = 20000;
+ cli->timeout = 20000; /* Timeout is in milliseconds. */
cli->bufsize = CLI_BUFFER_SIZE+4;
cli->max_xmit = cli->bufsize;
cli->outbuf = (char *)malloc(cli->bufsize);
cli->inbuf = (char *)malloc(cli->bufsize);
if (!cli->outbuf || !cli->inbuf)
- return False;
+ {
+ return False;
+ }
+
+ memset(cli->outbuf, '\0', cli->bufsize);
+ memset(cli->inbuf, '\0', cli->bufsize);
+
cli->initialised = 1;
- return True;
+
+ return cli;
}
/****************************************************************************
void cli_shutdown(struct cli_state *cli)
{
if (cli->outbuf)
- free(cli->outbuf);
+ {
+ free(cli->outbuf);
+ }
if (cli->inbuf)
- free(cli->inbuf);
+ {
+ free(cli->inbuf);
+ }
#ifdef WITH_SSL
if (cli->fd != -1)
sslutil_disconnect(cli->fd);
for 32 bit "warnings", a return code of 0 is expected.
****************************************************************************/
-int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num)
+int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num, uint32 *nt_rpc_error)
{
int flgs2 = SVAL(cli->inbuf,smb_flg2);
char rcls;
if (eclass) *eclass = 0;
if (num ) *num = 0;
+ if (nt_rpc_error) *nt_rpc_error = cli->nt_error;
if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
/* 32 bit error codes detected */
case ERRnoaccess: return EACCES;
case ERRfilexists: return EEXIST;
case ERRrename: return EEXIST;
+ case ERRbadshare: return EBUSY;
+ case ERRlock: return EBUSY;
}
}
if (rcls == ERRSRV) {
switch (code) {
case ERRbadpw: return EPERM;
+ case ERRaccess: return EACCES;
+ case ERRnoresource: return ENOMEM;
+ case ERRinvdevice: return ENODEV;
+ case ERRinvnetname: return ENODEV;
}
}
/* for other cases */
}
/****************************************************************************
-establishes a connection right up to doing tconX, reading in a password.
+re-establishes a connection
****************************************************************************/
BOOL cli_reestablish_connection(struct cli_state *cli)
{
struct nmb_name calling;
struct nmb_name called;
fstring dest_host;
- struct in_addr dest_ip;
fstring share;
fstring dev;
BOOL do_tcon = False;
+ int oldfd = cli->fd;
if (!cli->initialised || cli->fd == -1)
{
memcpy(&called , &(cli->called ), sizeof(called ));
memcpy(&calling, &(cli->calling), sizeof(calling));
fstrcpy(dest_host, cli->full_dest_host_name);
- dest_ip = cli->dest_ip;
DEBUG(5,("cli_reestablish_connection: %s connecting to %s (ip %s) - %s [%s]\n",
- namestr(&calling), namestr(&called), inet_ntoa(dest_ip),
- cli->user_name, cli->domain));
+ nmb_namestr(&calling), nmb_namestr(&called),
+ inet_ntoa(cli->dest_ip),
+ cli->user_name, cli->domain));
+
+ cli->fd = -1;
- return cli_establish_connection(cli,
- dest_host, &dest_ip,
- &calling, &called,
- share, dev, False, do_tcon);
+ if (cli_establish_connection(cli,
+ dest_host, &cli->dest_ip,
+ &calling, &called,
+ share, dev, False, do_tcon)) {
+ if (cli->fd != oldfd) {
+ if (dup2(cli->fd, oldfd) == oldfd) {
+ close(cli->fd);
+ }
+ }
+ return True;
+ }
+ return False;
}
/****************************************************************************
BOOL do_shutdown, BOOL do_tcon)
{
DEBUG(5,("cli_establish_connection: %s connecting to %s (%s) - %s [%s]\n",
- namestr(calling), namestr(called), inet_ntoa(*dest_ip),
+ nmb_namestr(calling), nmb_namestr(called), inet_ntoa(*dest_ip),
cli->user_name, cli->domain));
/* establish connection */
if (!cli_connect(cli, dest_host, dest_ip))
{
DEBUG(1,("cli_establish_connection: failed to connect to %s (%s)\n",
- namestr(calling), inet_ntoa(*dest_ip)));
+ nmb_namestr(calling), inet_ntoa(*dest_ip)));
return False;
}
}
if (cli->pwd.cleartext || cli->pwd.null_pwd)
{
- /* attempt clear-text session */
-
fstring passwd;
+ int pass_len;
- pwd_get_cleartext(&(cli->pwd), passwd);
+ if (cli->pwd.null_pwd)
+ {
+ /* attempt null session */
+ passwd[0] = 0;
+ pass_len = 1;
+ }
+ else
+ {
+ /* attempt clear-text session */
+ pwd_get_cleartext(&(cli->pwd), passwd);
+ pass_len = strlen(passwd);
+ }
/* attempt clear-text session */
if (!cli_session_setup(cli, cli->user_name,
- passwd, strlen(passwd),
+ passwd, pass_len,
NULL, 0,
cli->domain))
{
DEBUG(1,("failed session setup\n"));
if (do_shutdown)
- cli_shutdown(cli);
+ {
+ cli_shutdown(cli);
+ }
return False;
}
if (do_tcon)
{
DEBUG(1,("failed tcon_X\n"));
if (do_shutdown)
- cli_shutdown(cli);
+ {
+ cli_shutdown(cli);
+ }
return False;
}
}
int rdrcnt,rprcnt, ret = -1;
pstring param;
- bzero(param,sizeof(param));
+ memset(param,'\0',sizeof(param));
p = param;
SSVAL(p,0,81); /* DosPrintJobDel() */
int result_code=0;
int i = -1;
- bzero(param,sizeof(param));
+ memset(param,'\0',sizeof(param));
p = param;
SSVAL(p,0,76); /* API function number 76 (DosPrintJobEnum) */
return i;
}
+
+/****************************************************************************
+check for existance of a dir
+****************************************************************************/
+BOOL cli_chkpath(struct cli_state *cli, char *path)
+{
+ pstring path2;
+ char *p;
+
+ safe_strcpy(path2,path,sizeof(pstring));
+ trim_string(path2,NULL,"\\");
+ if (!*path2) *path2 = '\\';
+
+ memset(cli->outbuf,'\0',smb_size);
+ set_message(cli->outbuf,0,4 + strlen(path2),True);
+ SCVAL(cli->outbuf,smb_com,SMBchkpth);
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+ p = smb_buf(cli->outbuf);
+ *p++ = 4;
+ safe_strcpy(p,path2,strlen(path2));
+ unix_to_dos(p,True);
+
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
+ return False;
+ }
+
+ if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+ return True;
+}
+
+
+/****************************************************************************
+start a message sequence
+****************************************************************************/
+BOOL cli_message_start(struct cli_state *cli, char *host, char *username,
+ int *grp)
+{
+ char *p;
+
+ /* send a SMBsendstrt command */
+ memset(cli->outbuf,'\0',smb_size);
+ set_message(cli->outbuf,0,0,True);
+ CVAL(cli->outbuf,smb_com) = SMBsendstrt;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ p = smb_buf(cli->outbuf);
+ *p++ = 4;
+ pstrcpy(p,username);
+ unix_to_dos(p,True);
+ p = skip_string(p,1);
+ *p++ = 4;
+ pstrcpy(p,host);
+ unix_to_dos(p,True);
+ p = skip_string(p,1);
+
+ set_message(cli->outbuf,0,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
+
+ cli_send_smb(cli);
+
+ if (!cli_receive_smb(cli)) {
+ return False;
+ }
+
+ if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+ *grp = SVAL(cli->inbuf,smb_vwv0);
+
+ return True;
+}
+
+
+/****************************************************************************
+send a message
+****************************************************************************/
+BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
+{
+ char *p;
+
+ memset(cli->outbuf,'\0',smb_size);
+ set_message(cli->outbuf,1,len+3,True);
+ CVAL(cli->outbuf,smb_com) = SMBsendtxt;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ SSVAL(cli->outbuf,smb_vwv0,grp);
+
+ p = smb_buf(cli->outbuf);
+ *p = 1;
+ SSVAL(p,1,len);
+ memcpy(p+3,msg,len);
+ cli_send_smb(cli);
+
+ if (!cli_receive_smb(cli)) {
+ return False;
+ }
+
+ if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+ return True;
+}
+
+/****************************************************************************
+end a message
+****************************************************************************/
+BOOL cli_message_end(struct cli_state *cli, int grp)
+{
+ memset(cli->outbuf,'\0',smb_size);
+ set_message(cli->outbuf,1,0,True);
+ CVAL(cli->outbuf,smb_com) = SMBsendend;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+
+ SSVAL(cli->outbuf,smb_vwv0,grp);
+
+ cli_setup_packet(cli);
+
+ cli_send_smb(cli);
+
+ if (!cli_receive_smb(cli)) {
+ return False;
+ }
+
+ if (cli_error(cli, NULL, NULL, NULL)) return False;
+
+ return True;
+}
+
+
+/****************************************************************************
+query disk space
+****************************************************************************/
+BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
+{
+ memset(cli->outbuf,'\0',smb_size);
+ set_message(cli->outbuf,0,0,True);
+ CVAL(cli->outbuf,smb_com) = SMBdskattr;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ cli_send_smb(cli);
+ if (!cli_receive_smb(cli)) {
+ return False;
+ }
+
+ *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
+ *total = SVAL(cli->inbuf,smb_vwv0);
+ *avail = SVAL(cli->inbuf,smb_vwv3);
+
+ return True;
+}
+
+/****************************************************************************
+ Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
+****************************************************************************/
+
+BOOL attempt_netbios_session_request(struct cli_state *cli, char *srchost, char *desthost,
+ struct in_addr *pdest_ip)
+{
+ struct nmb_name calling, called;
+
+ make_nmb_name(&calling, srchost, 0x0, scope);
+
+ /*
+ * If the called name is an IP address
+ * then use *SMBSERVER immediately.
+ */
+
+ if(is_ipaddress(desthost))
+ make_nmb_name(&called, "*SMBSERVER", 0x20, scope);
+ else
+ make_nmb_name(&called, desthost, 0x20, scope);
+
+ if (!cli_session_request(cli, &calling, &called)) {
+ struct nmb_name smbservername;
+
+ /*
+ * If the name wasn't *SMBSERVER then
+ * try with *SMBSERVER if the first name fails.
+ */
+
+ cli_shutdown(cli);
+
+ make_nmb_name(&smbservername , "*SMBSERVER", 0x20, scope);
+
+ if (!nmb_name_equal(&called, &smbservername) ||
+ !cli_initialise(cli) ||
+ !cli_connect(cli, desthost, pdest_ip) ||
+ !cli_session_request(cli, &calling, &smbservername)) {
+ DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER.\n",
+ desthost));
+ cli_shutdown(cli);
+ return False;
+ }
+ }
+
+ return True;
+}