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.
10
#include <stringtools.h>
11
#include <console_login.h>
18
inline const char* s3_userid() { return userid; }
19
inline const char* s3_key() { return key; }
21
int process_userpass(char *userpass, char **username, char **password) {
25
if(string_split(userpass,&nargs,&args)) {
26
if(!*username) *username = strdup(args[0]);
27
if(!*password) *password = strdup(args[1]);
34
int process_configfile(char *configfile, char **username, char **password) {
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);
45
return process_userpass(userpass, username, password);
49
void s3_initialize(int* argc, char** argv) {
50
int i, mod, result, prompt = 0;
51
char *username = NULL, *password = NULL, *configfile = NULL;
54
for(i = 0; i < *argc; i++) {
55
if(argv[i][0] == '-') {
58
if(username) free(username);
63
if(password) free(password);
71
if(configfile) free(configfile);
72
configfile = argv[i+1];
86
argv2 = malloc(*argc * sizeof(*argv2));
87
for(i = 0; i < *argc; i++) {
88
if(argv[i][0] == '-') {
90
if(c == 'u' || c == 'P' || c == 'c') { i++; continue; }
91
else if(c == 'p' || c == 'd') { continue; }
93
argv2[mod++] = argv[i];
95
for(i = 0; i < mod; i++) argv[i] = argv2[i];
101
if(!username || !password) {
103
env = getenv("S3_USER_KEY");
107
username = malloc(1024);
108
password = malloc(1024);
109
if(!username || !password) exit(-1);
110
console_login( "s3", username, 1024, password, 1024 );
112
password = malloc(1024);
113
if(!password) exit(-1);
114
console_input( "password:", password, 1024 );
117
} else if(env) process_userpass(env, &username, &password);
118
else if(configfile) process_configfile(configfile, &username, &password);
120
char default_configfile[2048];
121
sprintf(default_configfile, "%s/%s", getenv("HOME"), DEFAULT_CONFIGFILE_NAME);
122
process_configfile(default_configfile, &username, &password);
126
result = s3_register_userid(username, password);
128
fprintf(stderr, "Error: no username or password specified\n");
134
int s3_register_userid(const char *new_userid, const char* new_key) {
135
if(!new_userid || !new_key) return -1;
138
userid = strdup(new_userid);
139
key = strdup(new_key);
141
if(userid && key) return 0;
147
void s3_clear_userid() {
149
int length = strlen(userid);
150
memset(userid, 0, length);
155
int length = strlen(key);
156
memset(key, 0, length);