Logo Search packages:      
Sourcecode: samba-doc-ja version File versions

int smbc_opendir ( const char *  durl  ) 

Open a directory used to obtain directory entries.

Parameters:
durl The smb url of the directory to open
Returns:
Valid directory handle. < 0 on error with errno set:
  • EACCES Permission denied.
  • EINVAL A NULL file/URL was passed, or the URL would not parse, or was of incorrect form or smbc_init not called.
  • ENOENT durl does not exist, or name is an
  • ENOMEM Insufficient memory to complete the operation.
  • ENOTDIR name is not a directory.
  • EPERM the workgroup could not be found.
  • ENODEV the workgroup or server could not be found.
See also:
smbc_getdents(), smbc_readdir(), smbc_closedir()

Definition at line 1537 of file libsmbclient.c.

Referenced by smbc_open().

{
      fstring server, share, user, password, workgroup;
      pstring path;
      struct smbc_server *srv = NULL;
      struct in_addr rem_ip;
      int slot = 0;

      if (!smbc_initialized) {

            errno = EINVAL;
            return -1;

      }

      if (!fname) {
    
            errno = EINVAL;
            return -1;

      }

      if (smbc_parse_path(fname, server, share, path, user, password)) {

            errno = EINVAL;
            return -1;

      }

      if (user[0] == (char)0) pstrcpy(user, smbc_user);

      pstrcpy(workgroup, lp_workgroup());

      /* Get a file entry ... */

      slot = 0;

      while (smbc_file_table[slot])
            slot++;

      if (slot > SMBC_MAX_FD) {

            errno = ENOMEM;
            return -1; /* FIXME, ... move into a func */
      
      }

      smbc_file_table[slot] = (struct smbc_file *)malloc(sizeof(struct smbc_file));

      if (!smbc_file_table[slot]) {

            errno = ENOMEM;
            return -1;

      }

      smbc_file_table[slot]->cli_fd   = 0;
      smbc_file_table[slot]->smbc_fd  = slot + smbc_start_fd;
      smbc_file_table[slot]->fname    = strdup(fname);
      smbc_file_table[slot]->srv      = NULL;
      smbc_file_table[slot]->offset   = 0;
      smbc_file_table[slot]->file     = False;
      smbc_file_table[slot]->dir_list = 
            smbc_file_table[slot]->dir_next =
            smbc_file_table[slot]->dir_end = NULL;

      if (server[0] == (char)0) {

            if (share[0] != (char)0 || path[0] != (char)0) {
    
                  errno = EINVAL;
                  if (smbc_file_table[slot]) {
                        SAFE_FREE(smbc_file_table[slot]->fname);
                        SAFE_FREE(smbc_file_table[slot]);
                  }
                  return -1;

            }

            /* We have server and share and path empty ... so list the workgroups */
                /* first try to get the LMB for our workgroup, and if that fails,     */
                /* try the DMB                                                        */

            if (!(resolve_name(lp_workgroup(), &rem_ip, 0x1d) ||
                      resolve_name(lp_workgroup(), &rem_ip, 0x1b))) {
      
                  errno = EINVAL;  /* Something wrong with smb.conf? */
                  return -1;

            }

            smbc_file_table[slot]->dir_type = SMBC_WORKGROUP;

            /* find the name of the server ... */

            if (!name_status_find("*", 0, 0, rem_ip, server)) {

                  DEBUG(0,("Could not get the name of local/domain master browser for server %s\n", server));
                  errno = EINVAL;
                  return -1;

            }

            /*
             * Get a connection to IPC$ on the server if we do not already have one
             */

            srv = smbc_server(server, "IPC$", workgroup, user, password);

            if (!srv) {

                  if (smbc_file_table[slot]) {
                        SAFE_FREE(smbc_file_table[slot]->fname);
                        SAFE_FREE(smbc_file_table[slot]);
                  }
                  
                  return -1;

            }

            smbc_file_table[slot]->srv = srv;

            /* Now, list the stuff ... */

            if (!cli_NetServerEnum(&srv->cli, workgroup, 0x80000000, list_fn,
                               (void *)smbc_file_table[slot])) {

                  if (smbc_file_table[slot]) {
                        SAFE_FREE(smbc_file_table[slot]->fname);
                        SAFE_FREE(smbc_file_table[slot]);
                  }
                  errno = cli_errno(&srv->cli);

                  return -1;

            }
      }
      else { /* Server not an empty string ... Check the rest and see what gives */

            if (share[0] == (char)0) {

                  if (path[0] != (char)0) { /* Should not have empty share with path */

                        errno = EINVAL;
                        if (smbc_file_table[slot]) {
                              SAFE_FREE(smbc_file_table[slot]->fname);
                              SAFE_FREE(smbc_file_table[slot]);
                        }
                        return -1;
      
                  }

                  /* Check to see if <server><1D>, <server><1B>, or <server><20> translates */
                  /* However, we check to see if <server> is an IP address first */

                  if (!is_ipaddress(server) &&  /* Not an IP addr so check next */
                      (resolve_name(server, &rem_ip, 0x1d) ||   /* Found LMB */
                                    resolve_name(server, &rem_ip, 0x1b) )) { /* Found DMB */
                        pstring buserver;

                        smbc_file_table[slot]->dir_type = SMBC_SERVER;

                        /*
                         * Get the backup list ...
                         */


                        if (!name_status_find("*", 0, 0, rem_ip, buserver)) {

                              DEBUG(0, ("Could not get name of local/domain master browser for server %s\n", server));
                              errno = EPERM;  /* FIXME, is this correct */
                              return -1;

                        }

                        /*
                         * Get a connection to IPC$ on the server if we do not already have one
                         */

                        srv = smbc_server(buserver, "IPC$", workgroup, user, password);

                        if (!srv) {

                              if (smbc_file_table[slot]) {
                                    SAFE_FREE(smbc_file_table[slot]->fname);
                                    SAFE_FREE(smbc_file_table[slot]);
                              }
                              return -1;

                        }

                        smbc_file_table[slot]->srv = srv;

                        /* Now, list the servers ... */

                        if (!cli_NetServerEnum(&srv->cli, server, 0x0000FFFE, list_fn,
                                           (void *)smbc_file_table[slot])) {

                              if (smbc_file_table[slot]) {
                                    SAFE_FREE(smbc_file_table[slot]->fname);
                                    SAFE_FREE(smbc_file_table[slot]);
                              }
                              errno = cli_errno(&srv->cli);
                              return -1;
                              
                        }

                  }
                  else {

                        if (resolve_name(server, &rem_ip, 0x20)) {

                              /* Now, list the shares ... */

                              smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;

                              srv = smbc_server(server, "IPC$", workgroup, user, password);

                              if (!srv) {

                                    if (smbc_file_table[slot]) {
                                          SAFE_FREE(smbc_file_table[slot]->fname);
                                          SAFE_FREE(smbc_file_table[slot]);
                                    }
                                    return -1;

                              }

                              smbc_file_table[slot]->srv = srv;

                              /* Now, list the servers ... */

                              if (cli_RNetShareEnum(&srv->cli, list_fn, 
                                                (void *)smbc_file_table[slot]) < 0) {

                                    errno = cli_errno(&srv->cli);
                                    if (smbc_file_table[slot]) {
                                          SAFE_FREE(smbc_file_table[slot]->fname);
                                          SAFE_FREE(smbc_file_table[slot]);
                                    }
                                    return -1;

                              }

                        }
                        else {

                              errno = ENODEV;   /* Neither the workgroup nor server exists */
                              if (smbc_file_table[slot]) {
                                    SAFE_FREE(smbc_file_table[slot]->fname);
                                    SAFE_FREE(smbc_file_table[slot]);
                              }
                              return -1;

                        }

                  }

            }
            else { /* The server and share are specified ... work from there ... */

                  /* Well, we connect to the server and list the directory */

                  smbc_file_table[slot]->dir_type = SMBC_FILE_SHARE;

                  srv = smbc_server(server, share, workgroup, user, password);

                  if (!srv) {

                        if (smbc_file_table[slot]) {
                              SAFE_FREE(smbc_file_table[slot]->fname);
                              SAFE_FREE(smbc_file_table[slot]);
                        }
                        return -1;

                  }

                  smbc_file_table[slot]->srv = srv;

                  /* Now, list the files ... */

                  pstrcat(path, "\\*");

                  if (cli_list(&srv->cli, path, aDIR | aSYSTEM | aHIDDEN, dir_list_fn, 
                             (void *)smbc_file_table[slot]) < 0) {

                        if (smbc_file_table[slot]) {
                              SAFE_FREE(smbc_file_table[slot]->fname);
                              SAFE_FREE(smbc_file_table[slot]);
                        }
                        errno = smbc_errno(&srv->cli);
                        return -1;

                  }
            }

      }

      return smbc_file_table[slot]->smbc_fd;

}


Generated by  Doxygen 1.6.0   Back to index