1 /* UNIX SMBlib NetBIOS implementation
6 Copyright (C) Richard Sharpe 1996
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "smblib-priv.h"
32 #define uchar unsigned char
36 SMB_State_Types SMBlib_State;
38 /* Initialize the SMBlib package */
44 SMBlib_State = SMB_State_Started;
46 signal(SIGPIPE, SIG_IGN); /* Ignore these ... */
48 /* If SMBLIB_Instrument is defines, turn on the instrumentation stuff */
49 #ifdef SMBLIB_INSTRUMENT
51 SMBlib_Instrument_Init();
63 #ifdef SMBLIB_INSTRUMENT
65 SMBlib_Instrument_Term(); /* Clean up and print results */
73 /* SMB_Create: Create a connection structure and return for later use */
74 /* We have other helper routines to set variables */
76 SMB_Handle_Type SMB_Create_Con_Handle()
80 SMBlib_errno = SMBlibE_NotImpl;
85 int SMBlib_Set_Sock_NoDelay(SMB_Handle_Type Con_Handle, BOOL yn)
90 if (RFCNB_Set_Sock_NoDelay(Con_Handle -> Trans_Connect, yn) < 0) {
95 fprintf(stderr, "Setting no-delay on TCP socket failed ...\n");
103 /* SMB_Connect_Server: Connect to a server, but don't negotiate protocol */
104 /* or anything else ... */
106 SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
107 char *server, char *NTdomain)
111 char called[80], calling[80], *address;
114 /* Get a connection structure if one does not exist */
118 if (Con_Handle == NULL) {
120 if ((con = (struct SMB_Connect_Def *)malloc(sizeof(struct SMB_Connect_Def))) == NULL) {
123 SMBlib_errno = SMBlibE_NoSpace;
129 /* Init some things ... */
131 strcpy(con -> service, "");
132 strcpy(con -> username, "");
133 strcpy(con -> password, "");
134 strcpy(con -> sock_options, "");
135 strcpy(con -> address, "");
136 strcpy(con -> desthost, server);
137 strcpy(con -> PDomain, NTdomain);
138 strcpy(con -> OSName, SMBLIB_DEFAULT_OSNAME);
139 strcpy(con -> LMType, SMBLIB_DEFAULT_LMTYPE);
140 con -> first_tree = con -> last_tree = NULL;
142 SMB_Get_My_Name(con -> myname, sizeof(con -> myname));
144 con -> port = 0; /* No port selected */
146 /* Get some things we need for the SMB Header */
148 con -> pid = getpid();
149 con -> mid = con -> pid; /* This will do for now ... */
150 con -> uid = 0; /* Until we have done a logon, no uid ... */
151 con -> gid = getgid();
153 /* Now connect to the remote end, but first upper case the name of the
154 service we are going to call, sine some servers want it in uppercase */
156 for (i=0; i < strlen(server); i++)
157 called[i] = toupper(server[i]);
159 called[strlen(server)] = 0; /* Make it a string */
161 for (i=0; i < strlen(con -> myname); i++)
162 calling[i] = toupper(con -> myname[i]);
164 calling[strlen(con -> myname)] = 0; /* Make it a string */
166 if (strcmp(con -> address, "") == 0)
167 address = con -> desthost;
169 address = con -> address;
171 con -> Trans_Connect = RFCNB_Call(called,
173 address, /* Protocol specific */
176 /* Did we get one? */
178 if (con -> Trans_Connect == NULL) {
180 if (Con_Handle == NULL) {
184 SMBlib_errno = -SMBlibE_CallFailed;
193 /* SMB_Connect: Connect to the indicated server */
194 /* If Con_Handle == NULL then create a handle and connect, otherwise */
195 /* use the handle passed */
197 static const char *SMB_Prots_Restrict[] = {"PC NETWORK PROGRAM 1.0",
201 SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
202 SMB_Tree_Handle *tree,
207 { SMB_Handle_Type con;
208 char *host, *address;
209 char temp[80], called[80], calling[80];
212 /* Get a connection structure if one does not exist */
216 if (Con_Handle == NULL) {
218 if ((con = (struct SMB_Connect_Def *)malloc(sizeof(struct SMB_Connect_Def))) == NULL) {
220 SMBlib_errno = SMBlibE_NoSpace;
226 /* Init some things ... */
228 strcpy(con -> service, service);
229 strcpy(con -> username, username);
230 strcpy(con -> password, password);
231 strcpy(con -> sock_options, "");
232 strcpy(con -> address, "");
233 strcpy(con -> PDomain, SMBLIB_DEFAULT_DOMAIN);
234 strcpy(con -> OSName, SMBLIB_DEFAULT_OSNAME);
235 strcpy(con -> LMType, SMBLIB_DEFAULT_LMTYPE);
236 con -> first_tree = con -> last_tree = NULL;
238 SMB_Get_My_Name(con -> myname, sizeof(con -> myname));
240 con -> port = 0; /* No port selected */
242 /* Get some things we need for the SMB Header */
244 con -> pid = getpid();
245 con -> mid = con -> pid; /* This will do for now ... */
246 con -> uid = 0; /* Until we have done a logon, no uid */
247 con -> gid = getgid();
249 /* Now figure out the host portion of the service */
251 strcpy(temp, service);
252 host = strtok(temp, "/\\"); /* Separate host name portion */
253 strcpy(con -> desthost, host);
255 /* Now connect to the remote end, but first upper case the name of the
256 service we are going to call, sine some servers want it in uppercase */
258 for (i=0; i < strlen(host); i++)
259 called[i] = toupper(host[i]);
261 called[strlen(host)] = 0; /* Make it a string */
263 for (i=0; i < strlen(con -> myname); i++)
264 calling[i] = toupper(con -> myname[i]);
266 calling[strlen(con -> myname)] = 0; /* Make it a string */
268 if (strcmp(con -> address, "") == 0)
269 address = con -> desthost;
271 address = con -> address;
273 con -> Trans_Connect = RFCNB_Call(called,
275 address, /* Protocol specific */
278 /* Did we get one? */
280 if (con -> Trans_Connect == NULL) {
282 if (Con_Handle == NULL) {
286 SMBlib_errno = -SMBlibE_CallFailed;
291 /* Now, negotiate the protocol */
293 if (SMB_Negotiate(con, SMB_Prots_Restrict) < 0) {
295 /* Hmmm what should we do here ... We have a connection, but could not
302 /* Now connect to the service ... */
304 if ((*tree = SMB_TreeConnect(con, NULL, service, password, "A:")) == NULL) {
314 /* Logon to the server. That is, do a session setup if we can. We do not do */
317 int SMB_Logon_Server(SMB_Handle_Type Con_Handle, char *UserName,
321 struct RFCNB_Pkt *pkt;
322 int param_len, pkt_len, pass_len;
325 /* First we need a packet etc ... but we need to know what protocol has */
326 /* been negotiated to figure out if we can do it and what SMB format to */
329 if (Con_Handle -> protocol < SMB_P_LanMan1) {
331 SMBlib_errno = SMBlibE_ProtLow;
336 strcpy(pword, PassWord);
337 if (Con_Handle -> encrypt_passwords)
340 SMBencrypt((uchar *) PassWord, (uchar *)Con_Handle -> Encrypt_Key,(uchar *)pword);
343 pass_len=strlen(pword);
346 /* Now build the correct structure */
348 if (Con_Handle -> protocol < SMB_P_NT1) {
350 param_len = strlen(UserName) + 1 + pass_len + 1 +
351 strlen(Con_Handle -> PDomain) + 1 +
352 strlen(Con_Handle -> OSName) + 1;
354 pkt_len = SMB_ssetpLM_len + param_len;
356 pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len);
360 SMBlib_errno = SMBlibE_NoSpace;
361 return(SMBlibE_BAD); /* Should handle the error */
365 bzero(SMB_Hdr(pkt), SMB_ssetpLM_len);
366 SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */
367 *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBsesssetupX;
368 SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Con_Handle -> pid);
369 SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, 0);
370 SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Con_Handle -> mid);
371 SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Con_Handle -> uid);
372 *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 10;
373 *(SMB_Hdr(pkt) + SMB_hdr_axc_offset) = 0xFF; /* No extra command */
374 SSVAL(SMB_Hdr(pkt), SMB_hdr_axo_offset, 0);
376 SSVAL(SMB_Hdr(pkt), SMB_ssetpLM_mbs_offset, SMBLIB_MAX_XMIT);
377 SSVAL(SMB_Hdr(pkt), SMB_ssetpLM_mmc_offset, 2);
378 SSVAL(SMB_Hdr(pkt), SMB_ssetpLM_vcn_offset, Con_Handle -> pid);
379 SIVAL(SMB_Hdr(pkt), SMB_ssetpLM_snk_offset, 0);
380 SSVAL(SMB_Hdr(pkt), SMB_ssetpLM_pwl_offset, pass_len + 1);
381 SIVAL(SMB_Hdr(pkt), SMB_ssetpLM_res_offset, 0);
382 SSVAL(SMB_Hdr(pkt), SMB_ssetpLM_bcc_offset, param_len);
384 /* Now copy the param strings in with the right stuff */
386 p = (char *)(SMB_Hdr(pkt) + SMB_ssetpLM_buf_offset);
388 /* Copy in password, then the rest. Password has a null at end */
390 memcpy(p, pword, pass_len);
392 p = p + pass_len + 1;
395 p = p + strlen(UserName);
400 strcpy(p, Con_Handle -> PDomain);
401 p = p + strlen(Con_Handle -> PDomain);
405 strcpy(p, Con_Handle -> OSName);
406 p = p + strlen(Con_Handle -> OSName);
412 /* We don't admit to UNICODE support ... */
414 param_len = strlen(UserName) + 1 + pass_len +
415 strlen(Con_Handle -> PDomain) + 1 +
416 strlen(Con_Handle -> OSName) + 1 +
417 strlen(Con_Handle -> LMType) + 1;
419 pkt_len = SMB_ssetpNTLM_len + param_len;
421 pkt = (struct RFCNB_Pkt *)RFCNB_Alloc_Pkt(pkt_len);
425 SMBlib_errno = SMBlibE_NoSpace;
426 return(-1); /* Should handle the error */
430 bzero(SMB_Hdr(pkt), SMB_ssetpNTLM_len);
431 SIVAL(SMB_Hdr(pkt), SMB_hdr_idf_offset, SMB_DEF_IDF); /* Plunk in IDF */
432 *(SMB_Hdr(pkt) + SMB_hdr_com_offset) = SMBsesssetupX;
433 SSVAL(SMB_Hdr(pkt), SMB_hdr_pid_offset, Con_Handle -> pid);
434 SSVAL(SMB_Hdr(pkt), SMB_hdr_tid_offset, 0);
435 SSVAL(SMB_Hdr(pkt), SMB_hdr_mid_offset, Con_Handle -> mid);
436 SSVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset, Con_Handle -> uid);
437 *(SMB_Hdr(pkt) + SMB_hdr_wct_offset) = 13;
438 *(SMB_Hdr(pkt) + SMB_hdr_axc_offset) = 0xFF; /* No extra command */
439 SSVAL(SMB_Hdr(pkt), SMB_hdr_axo_offset, 0);
441 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_mbs_offset, SMBLIB_MAX_XMIT);
442 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_mmc_offset, 0);
443 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_vcn_offset, 0);
444 SIVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_snk_offset, 0);
445 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_cipl_offset, pass_len);
446 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_cspl_offset, 0);
447 SIVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_res_offset, 0);
448 SIVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_cap_offset, 0);
449 SSVAL(SMB_Hdr(pkt), SMB_ssetpNTLM_bcc_offset, param_len);
451 /* Now copy the param strings in with the right stuff */
453 p = (char *)(SMB_Hdr(pkt) + SMB_ssetpNTLM_buf_offset);
455 /* Copy in password, then the rest. Password has no null at end */
457 memcpy(p, pword, pass_len);
462 p = p + strlen(UserName);
467 strcpy(p, Con_Handle -> PDomain);
468 p = p + strlen(Con_Handle -> PDomain);
472 strcpy(p, Con_Handle -> OSName);
473 p = p + strlen(Con_Handle -> OSName);
477 strcpy(p, Con_Handle -> LMType);
478 p = p + strlen(Con_Handle -> LMType);
483 /* Now send it and get a response */
485 if (RFCNB_Send(Con_Handle -> Trans_Connect, pkt, pkt_len) < 0){
488 fprintf(stderr, "Error sending SessSetupX request\n");
492 SMBlib_errno = SMBlibE_SendFailed;
497 /* Now get the response ... */
499 if (RFCNB_Recv(Con_Handle -> Trans_Connect, pkt, pkt_len) < 0) {
502 fprintf(stderr, "Error receiving response to SessSetupAndX\n");
506 SMBlib_errno = SMBlibE_RecvFailed;
511 /* Check out the response type ... */
513 if (CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset) != SMBC_SUCCESS) { /* Process error */
516 fprintf(stderr, "SMB_SessSetupAndX failed with errorclass = %i, Error Code = %i\n",
517 CVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset),
518 SVAL(SMB_Hdr(pkt), SMB_hdr_err_offset));
521 SMBlib_SMB_Error = IVAL(SMB_Hdr(pkt), SMB_hdr_rcls_offset);
523 SMBlib_errno = SMBlibE_Remote;
527 /** @@@ mdz: check for guest login { **/
528 if (SVAL(SMB_Hdr(pkt), SMB_ssetpr_act_offset) & 0x1)
530 /* do we allow guest login? NO! */
538 fprintf(stderr, "SessSetupAndX response. Action = %i\n",
539 SVAL(SMB_Hdr(pkt), SMB_ssetpr_act_offset));
542 /* Now pick up the UID for future reference ... */
544 Con_Handle -> uid = SVAL(SMB_Hdr(pkt), SMB_hdr_uid_offset);
552 /* Disconnect from the server, and disconnect all tree connects */
554 int SMB_Discon(SMB_Handle_Type Con_Handle, BOOL KeepHandle)
558 /* We just disconnect the connection for now ... */
560 RFCNB_Hangup(Con_Handle -> Trans_Connect);