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

« back to all changes in this revision

Viewing changes to s3tools/src/s3common.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 <string.h>
 
7
#include <stdlib.h>
 
8
#include <stdio.h>
 
9
#include <sys/stat.h>
 
10
#include <stringtools.h>
 
11
#include <console_login.h>
 
12
#include "s3common.h"
 
13
 
 
14
 
 
15
char *userid = NULL;
 
16
char *key = NULL;
 
17
 
 
18
inline const char* s3_userid() { return userid; }
 
19
inline const char* s3_key() { return key; }
 
20
 
 
21
int process_userpass(char *userpass, char **username, char **password) {
 
22
        int nargs;
 
23
        char **args;
 
24
        
 
25
        if(string_split(userpass,&nargs,&args)) {
 
26
                if(!*username) *username = strdup(args[0]);
 
27
                if(!*password) *password = strdup(args[1]);
 
28
                free(args);
 
29
                return 0;
 
30
        }
 
31
        return -1;
 
32
}
 
33
 
 
34
int process_configfile(char *configfile, char **username, char **password) {
 
35
        char *userpass;
 
36
        struct stat st_buf;
 
37
        FILE* config;
 
38
 
 
39
        if(stat(configfile, &st_buf)) return -1;
 
40
        userpass = malloc(sizeof(*userpass) * st_buf.st_size);
 
41
        config = fopen(configfile, "r");
 
42
        fread(userpass, st_buf.st_size, 1, config);
 
43
        fclose(config);
 
44
 
 
45
        return process_userpass(userpass, username, password);
 
46
 
 
47
}
 
48
 
 
49
void s3_initialize(int* argc, char** argv) {
 
50
        int i, mod, result, prompt = 0;
 
51
        char *username = NULL, *password = NULL, *configfile = NULL;
 
52
        char **argv2;
 
53
 
 
54
        for(i = 0; i < *argc; i++) {
 
55
                if(argv[i][0] == '-') {
 
56
                        switch(argv[i][1]) {
 
57
                                case 'u':
 
58
                                        if(username) free(username);
 
59
                                        username = argv[i+1];
 
60
                                        i++;
 
61
                                        break;
 
62
                                case 'P':
 
63
                                        if(password) free(password);
 
64
                                        password = argv[i+1];
 
65
                                        i++;
 
66
                                        break;
 
67
                                case 'p':
 
68
                                        prompt = 1;
 
69
                                        break;
 
70
                                case 'c':
 
71
                                        if(configfile) free(configfile);
 
72
                                        configfile = argv[i+1];
 
73
                                        i++;
 
74
                                        break;
 
75
                                case 'd':
 
76
                        //              debug = 1;
 
77
                                        break;
 
78
                                default:
 
79
                                        continue;
 
80
                        }
 
81
                }
 
82
        }
 
83
 
 
84
        mod = 0;
 
85
        
 
86
        argv2 = malloc(*argc * sizeof(*argv2));
 
87
        for(i = 0; i < *argc; i++) {
 
88
                if(argv[i][0] == '-') {
 
89
                        char c = argv[i][1];
 
90
                        if(c == 'u' || c == 'P' || c == 'c') { i++; continue; }
 
91
                        else if(c == 'p' || c == 'd') { continue; }
 
92
                }
 
93
                argv2[mod++] = argv[i];
 
94
        }
 
95
        for(i = 0; i < mod; i++) argv[i] = argv2[i];
 
96
        free(argv2);
 
97
 
 
98
        *argc = mod;
 
99
 
 
100
 
 
101
        if(!username || !password) {
 
102
                char* env = NULL;
 
103
                env = getenv("S3_USER_KEY");
 
104
 
 
105
                if(prompt) {
 
106
                        if(!username) {
 
107
                                username = malloc(1024);
 
108
                                password = malloc(1024);
 
109
                                if(!username || !password) exit(-1);
 
110
                                console_login( "s3", username, 1024, password, 1024 );
 
111
                        } else {
 
112
                                password = malloc(1024);
 
113
                                if(!password) exit(-1);
 
114
                                console_input( "password:", password, 1024 );
 
115
                        }
 
116
 
 
117
                } else if(env) process_userpass(env, &username, &password);
 
118
                else if(configfile) process_configfile(configfile, &username, &password);
 
119
                else {
 
120
                        char default_configfile[2048];
 
121
                        sprintf(default_configfile, "%s/%s", getenv("HOME"), DEFAULT_CONFIGFILE_NAME);
 
122
                        process_configfile(default_configfile, &username, &password);
 
123
                }
 
124
        }
 
125
 
 
126
        result = s3_register_userid(username, password);
 
127
        if(result < 0) {
 
128
                fprintf(stderr, "Error: no username or password specified\n");
 
129
                exit(result);
 
130
        }
 
131
}
 
132
 
 
133
 
 
134
int s3_register_userid(const char *new_userid, const char* new_key) {
 
135
        if(!new_userid || !new_key) return -1;
 
136
 
 
137
        s3_clear_userid();
 
138
        userid = strdup(new_userid);
 
139
        key = strdup(new_key);
 
140
 
 
141
        if(userid && key) return 0;
 
142
 
 
143
        s3_clear_userid();
 
144
        return -1;
 
145
}
 
146
 
 
147
void s3_clear_userid() {
 
148
        if(userid) {
 
149
                int length = strlen(userid);
 
150
                memset(userid, 0, length);
 
151
                free(userid);
 
152
                userid = NULL;
 
153
        }
 
154
        if(key) {
 
155
                int length = strlen(key);
 
156
                memset(key, 0, length);
 
157
                free(key);
 
158
                key = NULL;
 
159
        }
 
160
}
 
161