Exemplo n.º 1
0
/*
 * Get a new empty handle to fill in with your own info
 */
SMBCCTX *
smbc_new_context(void)
{
        SMBCCTX *context;
        
        /* The first call to this function should initialize the module */
        SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);

        /*
         * All newly added context fields should be placed in
         * SMBC_internal_data, not directly in SMBCCTX.
         */
        context = SMB_MALLOC_P(SMBCCTX);
        if (!context) {
                errno = ENOMEM;
                return NULL;
        }
        
        ZERO_STRUCTP(context);
        
        context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
        if (!context->internal) {
                SAFE_FREE(context);
                errno = ENOMEM;
                return NULL;
        }
        
        /* Initialize the context and establish reasonable defaults */
        ZERO_STRUCTP(context->internal);
        
        smbc_setDebug(context, 0);
        smbc_setTimeout(context, 20000);
        
        smbc_setOptionFullTimeNames(context, False);
        smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
        smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
        smbc_setOptionCaseSensitive(context, False);
        smbc_setOptionBrowseMaxLmbCount(context, 3);    /* # LMBs to query */
        smbc_setOptionUrlEncodeReaddirEntries(context, False);
        smbc_setOptionOneSharePerServer(context, False);
        
        smbc_setFunctionAuthData(context, SMBC_get_auth_data);
        smbc_setFunctionCheckServer(context, SMBC_check_server);
        smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
        
        smbc_setOptionUserData(context, NULL);
        smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
        smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
        smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
        smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
        
        smbc_setFunctionOpen(context, SMBC_open_ctx);
        smbc_setFunctionCreat(context, SMBC_creat_ctx);
        smbc_setFunctionRead(context, SMBC_read_ctx);
        smbc_setFunctionWrite(context, SMBC_write_ctx);
        smbc_setFunctionClose(context, SMBC_close_ctx);
        smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
        smbc_setFunctionRename(context, SMBC_rename_ctx);
        smbc_setFunctionLseek(context, SMBC_lseek_ctx);
        smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
        smbc_setFunctionStat(context, SMBC_stat_ctx);
        smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
        smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
        smbc_setFunctionFstat(context, SMBC_fstat_ctx);
        smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
        smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
        smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
        smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
        smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
        smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
        smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
        smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
        smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
        smbc_setFunctionChmod(context, SMBC_chmod_ctx);
        smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
        smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
        smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
        smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
        smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
        
        smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
        smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
        smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
        smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
        
        return context;
}
Exemplo n.º 2
0
int main(int argc, const char *argv[])
{
    int opt;
    int flags;
    int debug = 0;
    int numeric = 0;
    int stat_and_retry = 0;
    int full_time_names = 0;
    enum acl_mode mode = SMB_ACL_LIST;
    static const char *the_acl = NULL;
    int ret;
    char *p;
    const char *debugstr;
    char path[1024];
    char value[1024];
    poptContext pc;
    struct stat st;
    struct poptOption long_options[] =
        {
            POPT_AUTOHELP
            {
                "numeric", 'n', POPT_ARG_NONE, &numeric,
                1, "Don't resolve sids or masks to names"
            },
            {
                "debug", 'd', POPT_ARG_INT, &debug,
                0, "Set debug level (0-100)"
            },
            {
                "full_time_names", 'f', POPT_ARG_NONE, &full_time_names,
                1,
                "Use new style xattr names, which include CREATE_TIME"
            },
            {
                "delete", 'D', POPT_ARG_STRING, NULL,
                'D', "Delete an acl", "ACL"
            },
            {
                "modify", 'M', POPT_ARG_STRING, NULL,
                'M', "Modify an acl", "ACL"
            },
            {
                "add", 'a', POPT_ARG_STRING, NULL,
                'a', "Add an acl", "ACL"
            },
            {
                "set", 'S', POPT_ARG_STRING, NULL,
                'S', "Set acls", "ACLS"
            },
            {
                "chown", 'C', POPT_ARG_STRING, NULL,
                'C', "Change ownership of a file", "USERNAME"
            },
            {
                "chgrp", 'G', POPT_ARG_STRING, NULL,
                'G', "Change group ownership of a file", "GROUPNAME"
            },
            {
                "get", 'g', POPT_ARG_STRING, NULL,
                'g', "Get a specific acl attribute", "ACL"
            },
            {
                "stat_and_retry", 'R', POPT_ARG_NONE, &stat_and_retry,
                1, "After 'get' do 'stat' and another 'get'"
            },
            {
                NULL
            }
        };
    
    setbuf(stdout, NULL);

    pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
    
    poptSetOtherOptionHelp(pc, "smb://server1/share1/filename");
    
    while ((opt = poptGetNextOpt(pc)) != -1) {
        switch (opt) {
        case 'S':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_SET;
            break;
            
        case 'D':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_DELETE;
            break;
            
        case 'M':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_MODIFY;
            break;
            
        case 'a':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_ADD;
            break;

        case 'g':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_GET;
            break;

        case 'C':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_CHOWN;
            break;

        case 'G':
            the_acl = strdup(poptGetOptArg(pc));
            mode = SMB_ACL_CHGRP;
            break;
        }
    }
    
    /* Make connection to server */
    if(!poptPeekArg(pc)) { 
        poptPrintUsage(pc, stderr, 0);
        return 1;
    }
    
    strcpy(path, poptGetArg(pc));
    
    if (smbc_init(get_auth_data_fn, debug) != 0)
    {
        printf("Could not initialize smbc_ library\n");
        return 1;
    }

    if (full_time_names) {
        SMBCCTX *context = smbc_set_context(NULL);
        smbc_setOptionFullTimeNames(context, 1);
    }
    
    /* Perform requested action */
    
    switch(mode)
    {
    case SMB_ACL_LIST:
        ret = smbc_listxattr(path, value, sizeof(value)-2);
        if (ret < 0)
        {
            printf("Could not get attribute list for [%s] %d: %s\n",
                   path, errno, strerror(errno));
            return 1;
        }

        /*
         * The list of attributes has a series of null-terminated strings.
         * The list of strings terminates with an extra null byte, thus two in
         * a row.  Ensure that our buffer, which is conceivably shorter than
         * the list of attributes, actually ends with two null bytes in a row.
         */
        value[sizeof(value) - 2] = '\0';
        value[sizeof(value) - 1] = '\0';
        printf("Supported attributes:\n");
        for (p = value; *p; p += strlen(p) + 1)
        {
            printf("\t%s\n", p);
        }
        break;

    case SMB_ACL_GET:
        do
        {
            if (the_acl == NULL)
            {
                if (numeric)
                {
                    the_acl = "system.*";
                }
                else
                {
                    the_acl = "system.*+";
                }
            }
            ret = smbc_getxattr(path, the_acl, value, sizeof(value));
            if (ret < 0)
            {
                printf("Could not get attributes for [%s] %d: %s\n",
                       path, errno, strerror(errno));
                return 1;
            }
        
            printf("Attributes for [%s] are:\n%s\n", path, value);

            if (stat_and_retry)
            {
                if (smbc_stat(path, &st) < 0)
                {
                    perror("smbc_stat");
                    return 1;
                }
            }

            --stat_and_retry;
        } while (stat_and_retry >= 0);
        break;

    case SMB_ACL_ADD:
        flags = SMBC_XATTR_FLAG_CREATE;
        debugstr = "add attributes";
        goto do_set;
        
    case SMB_ACL_MODIFY:
        flags = SMBC_XATTR_FLAG_REPLACE;
        debugstr = "modify attributes";
        goto do_set;

    case SMB_ACL_CHOWN:
        snprintf(value, sizeof(value),
                 "system.nt_sec_desc.owner%s:%s",
                 numeric ? "" : "+", the_acl);
        the_acl = value;
        debugstr = "chown owner";
        goto do_set;

    case SMB_ACL_CHGRP:
        snprintf(value, sizeof(value),
                 "system.nt_sec_desc.group%s:%s",
                 numeric ? "" : "+", the_acl);
        the_acl = value;
        debugstr = "change group";
        goto do_set;

    case SMB_ACL_SET:
        flags = 0;
        debugstr = "set attributes";
        
      do_set:
        if ((p = strchr(the_acl, ':')) == NULL)
        {
            printf("Missing value.  ACL must be name:value pair\n");
            return 1;
        }

        *p++ = '\0';
        
        ret = smbc_setxattr(path, the_acl, p, strlen(p), flags);
        if (ret < 0)
        {
            printf("Could not %s for [%s] %d: %s\n",
                   debugstr, path, errno, strerror(errno));
            return 1;
        }
        break;

    case SMB_ACL_DELETE:
        ret = smbc_removexattr(path, the_acl);
        if (ret < 0)
        {
            printf("Could not remove attribute %s for [%s] %d:%s\n",
                   the_acl, path, errno, strerror(errno));
            return 1;
        }
        break;

    default:
        printf("operation not yet implemented\n");
        break;
    }
    
    return 0;
}