Apache2-AuthenNTLM

 view release on metacpan or  search on metacpan

smb/smbval/smblib.c  view on Meta::CPAN

  if (RFCNB_Set_Sock_NoDelay(Con_Handle -> Trans_Connect, yn) < 0) {

#ifdef DEBUG
#endif

    fprintf(stderr, "Setting no-delay on TCP socket failed ...\n");

  }

  return(0);

}

/* SMB_Connect_Server: Connect to a server, but don't negotiate protocol */
/* or anything else ...                                                  */

SMB_Handle_Type SMB_Connect_Server(SMB_Handle_Type Con_Handle,
				   char *server, char *NTdomain)

{ SMB_Handle_Type con;
  char temp[80], called[80], calling[100], *address, buf [20];
  int i;

  /* Get a connection structure if one does not exist */

  con = Con_Handle;

  if (Con_Handle == NULL) {

    if ((con = (struct SMB_Connect_Def *)malloc(sizeof(struct SMB_Connect_Def))) == NULL) {


      SMBlib_errno = SMBlibE_NoSpace;
      return NULL;
    }

  }

  /* Init some things ... */

  strcpy(con -> service, "");
  strcpy(con -> username, "");
  strcpy(con -> password, "");
  strcpy(con -> sock_options, "");
  strcpy(con -> address, "");
  strcpy(con -> desthost, server);
  strcpy(con -> PDomain, NTdomain);
  strcpy(con -> OSName, SMBLIB_DEFAULT_OSNAME);
  strcpy(con -> LMType, SMBLIB_DEFAULT_LMTYPE);
  con -> first_tree = con -> last_tree = NULL; 

  SMB_Get_My_Name(con -> myname, sizeof(con -> myname));

  con -> port = 0;                    /* No port selected */

  /* Get some things we need for the SMB Header */

  con -> pid = getpid();
  con -> mid = con -> pid;      /* This will do for now ... */
  con -> uid = 0;               /* Until we have done a logon, no uid */
  con -> gid = getgid();


  /* Now connect to the remote end, but first upper case the name of the
     service we are going to call, sine some servers want it in uppercase */

  for (i=0; i < strlen(server); i++)
    called[i] = toupper(server[i]);
		       
  called[strlen(server)] = 0;    /* Make it a string */

  for (i=0; i < strlen(con -> myname); i++)
    calling[i] = toupper(con -> myname[i]);
		       
  calling[strlen(con -> myname)] = 0;    /* Make it a string */

  if (strcmp(con -> address, "") == 0)
    address = con -> desthost;
  else
    address = con -> address;

  con -> Trans_Connect = RFCNB_Call(called,
				    calling,
				    address, /* Protocol specific */
				    con -> port);

  /* Did we get one? */

  if (con -> Trans_Connect == NULL) {

    if (Con_Handle == NULL) {
      Con_Handle = NULL;
      free(con);
    }
    SMBlib_errno = -SMBlibE_CallFailed;
    return NULL;

  }

  return(con);

}

/* SMB_Connect: Connect to the indicated server                       */
/* If Con_Handle == NULL then create a handle and connect, otherwise  */
/* use the handle passed                                              */

char *SMB_Prots_Restrict[] = {"PC NETWORK PROGRAM 1.0",
			      NULL};


SMB_Handle_Type SMB_Connect(SMB_Handle_Type Con_Handle,
			    SMB_Tree_Handle *tree,
			    char *service, 
			    char *username, 
			    char *password)

{ SMB_Handle_Type con;
  char *host, *address;
  char temp[80], called[80], calling[80];
  int i;

  /* Get a connection structure if one does not exist */

  con = Con_Handle;

  if (Con_Handle == NULL) {

    if ((con = (struct SMB_Connect_Def *)malloc(sizeof(struct SMB_Connect_Def))) == NULL) {

      SMBlib_errno = SMBlibE_NoSpace;
      return NULL;
    }

  }

  /* Init some things ... */

  strcpy(con -> service, service);
  strcpy(con -> username, username);
  strcpy(con -> password, password);
  strcpy(con -> sock_options, "");
  strcpy(con -> address, "");
  strcpy(con -> PDomain, SMBLIB_DEFAULT_DOMAIN);
  strcpy(con -> OSName, SMBLIB_DEFAULT_OSNAME);
  strcpy(con -> LMType, SMBLIB_DEFAULT_LMTYPE);
  con -> first_tree = con -> last_tree = NULL;

  SMB_Get_My_Name(con -> myname, sizeof(con -> myname));

  con -> port = 0;                    /* No port selected */

  /* Get some things we need for the SMB Header */

  con -> pid = getpid();
  con -> mid = con -> pid;      /* This will do for now ... */
  con -> uid = 0;               /* Until we have done a logon, no uid */
  con -> gid = getgid();

  /* Now figure out the host portion of the service */

  strcpy(temp, service);
  host = strtok(temp, "/\\");     /* Separate host name portion */
  strcpy(con -> desthost, host);

  /* Now connect to the remote end, but first upper case the name of the
     service we are going to call, sine some servers want it in uppercase */

  for (i=0; i < strlen(host); i++)
    called[i] = toupper(host[i]);
		       
  called[strlen(host)] = 0;    /* Make it a string */

  for (i=0; i < strlen(con -> myname); i++)
    calling[i] = toupper(con -> myname[i]);
		       
  calling[strlen(con -> myname)] = 0;    /* Make it a string */

  if (strcmp(con -> address, "") == 0)
    address = con -> desthost;
  else
    address = con -> address;

  
  con -> Trans_Connect = RFCNB_Call(called,
				    calling,
				    address, /* Protocol specific */
				    con -> port);

  /* Did we get one? */

  if (con -> Trans_Connect == NULL) {

    if (Con_Handle == NULL) {
      free(con);
      Con_Handle = NULL;
    }
    SMBlib_errno = -SMBlibE_CallFailed;
    return NULL;

  }

  /* Now, negotiate the protocol */

  if (SMB_Negotiate(con, SMB_Prots_Restrict) < 0) {

    /* Hmmm what should we do here ... We have a connection, but could not
       negotiate ...                                                      */

    return NULL;

  }

  /* Now connect to the service ... */

  if ((*tree = SMB_TreeConnect(con, NULL, service, password, "A:")) == NULL) {

    return NULL;



( run in 0.701 second using v1.01-cache-2.11-cpan-5735350b133 )