~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to s3tools/src/s3setacl.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2010- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
#include <stringtools.h>
 
10
 
 
11
#include "s3common.h"
 
12
#include "s3c_acl.h"
 
13
 
 
14
int main(int argc, char** argv) {
 
15
        struct hash_table *acls;
 
16
        struct s3_acl_object *acl;
 
17
        char *bucket, *filename, *handle, *aclstr, *id;
 
18
        char remotename[FILENAME_MAX];
 
19
        char owner[FILENAME_MAX];
 
20
        int i;
 
21
        unsigned char mask = 0;
 
22
 
 
23
        s3_initialize(&argc, argv);
 
24
 
 
25
        if(argc < 4) {
 
26
                fprintf(stderr, "usage: s3setacl <bucket> [filename] <email | display name> [+|-]<acls>\n");
 
27
                return -1;
 
28
        }
 
29
 
 
30
        bucket = argv[1];
 
31
        if(argc == 5) {
 
32
                sprintf(remotename, "/%s", argv[2]);
 
33
                filename = remotename;
 
34
                handle = argv[3];
 
35
                aclstr = argv[4];
 
36
        } else {
 
37
                filename = NULL;
 
38
                handle = argv[2];
 
39
                aclstr = argv[3];
 
40
        }
 
41
 
 
42
        acls = hash_table_create(0, NULL);
 
43
        s3_getacl(bucket, filename, owner, acls, s3_userid(), s3_key());
 
44
 
 
45
        hash_table_firstkey(acls);
 
46
        while(hash_table_nextkey(acls, &id, (void**)&acl)) {
 
47
                if(!strcmp(id, handle)) break;
 
48
        }
 
49
        if(!acl) acl = hash_table_lookup(acls, handle);
 
50
 
 
51
        if(!acl && !strchr(handle, '@')) {
 
52
                fprintf(stderr, "Error: invalid handle (%s)\n", handle);
 
53
                exit(0);
 
54
        }
 
55
 
 
56
        if(!acl && aclstr[0] != '-') {
 
57
                acl = malloc(sizeof(*acl));
 
58
                acl->acl_type = S3_ACL_EMAIL;
 
59
                acl->perm = 0;
 
60
                acl->display_name = NULL;
 
61
                hash_table_insert(acls, handle, acl);
 
62
        }
 
63
        if(!acl) return 0;
 
64
 
 
65
        fprintf(stderr, "aclstr: %s\n", aclstr);
 
66
        for(i = strcspn(aclstr, "frwgs"); i < strlen(aclstr); i++) {
 
67
                switch(aclstr[i]) {
 
68
                        case 'f':       mask = mask | S3_ACL_FULL_CONTROL; break;
 
69
                        case 'r':       mask = mask | S3_ACL_READ; break;
 
70
                        case 'w':       mask = mask | S3_ACL_WRITE; break;
 
71
                        case 'g':       mask = mask | S3_ACL_READ_ACP; break;
 
72
                        case 's':       mask = mask | S3_ACL_WRITE_ACP; break;
 
73
                }
 
74
        }
 
75
 
 
76
        if(aclstr[0] == '+') {
 
77
                acl->perm = acl->perm | mask;
 
78
        } else if(aclstr[0] == '-') {
 
79
                acl->perm = acl->perm & ~mask;
 
80
        } else {
 
81
                acl->perm = mask;
 
82
        }
 
83
 
 
84
        s3_setacl(bucket, filename, owner, acls, s3_userid(), s3_key());
 
85
 
 
86
        return 0;
 
87
}
 
88