dav/main.c

changeset 494
3aed354185eb
parent 489
fb69eae42ef0
child 498
37af2eac3e6a
equal deleted inserted replaced
493:75a259ec8dea 494:3aed354185eb
1159 // download content 1159 // download content
1160 1160
1161 return tar_end_file(tar); 1161 return tar_end_file(tar);
1162 } 1162 }
1163 1163
1164 int cmd_put(CmdArgs *a, DavBool import) { 1164 static int check_encryption_key(CmdArgs *a, DavSession *sn) {
1165 if(a->argc != 2) {
1166 // TODO: change, when put supports multiple files (however it should do)
1167 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
1168 fprintf(stderr, "Usage: dav %s\n", find_usage_str("put"));
1169 return -1;
1170 }
1171 if(import) {
1172 ucx_map_cstr_put(a->options, "resursive", "");
1173 }
1174
1175 char *url = a->argv[0];
1176 char *file = a->argv[1];
1177 char *path = NULL;
1178 Repository *repo = url2repo(url, &path);
1179 DavSession *sn = connect_to_repo(repo, path, a);
1180
1181 if(set_session_config(sn, a)) {
1182 return -1;
1183 }
1184 set_session_lock(sn, a);
1185
1186 // override the session key if the -k option is specified 1165 // override the session key if the -k option is specified
1187 char *keyname = cmd_getoption(a, "key"); 1166 char *keyname = cmd_getoption(a, "key");
1188 if(keyname) { 1167 if(keyname) {
1189 DavKey *key = dav_context_get_key(ctx, keyname); 1168 DavKey *key = dav_context_get_key(ctx, keyname);
1190 if(key) { 1169 if(key) {
1191 sn->key = key; 1170 sn->key = key;
1192 } else { 1171 } else {
1193 fprintf(stderr, "Key %s not found!\nAbort.\n", keyname); 1172 fprintf(stderr, "Key %s not found!\nAbort.\n", keyname);
1194 // TODO: free 1173 return 1;
1195 return -1; 1174 }
1196 } 1175
1197 } 1176 /*
1198 1177 * If a key is explicitly specified, we can safely assume that the user
1199 // if encryption is requested, but we still don't know the key, abort 1178 * wants to encrypt. For security reasons we report an error, if no
1200 if (DAV_IS_ENCRYPTED(sn) && !(sn->key)) { 1179 * encryption is enabled.
1180 */
1181 if(!DAV_IS_ENCRYPTED(sn)) {
1182 fprintf(stderr, "A key has been explicitly specified, but no "
1183 "encryption is requested.\n"
1184 "You have the following options:\n"
1185 " - pass '-c' as command line argument to request encryption\n"
1186 " - activate encryption in the config.xml\n"
1187 " - don't use '-k <key>' "
1188 "(warning: encryption will NOT happen)\n");
1189 return 1;
1190 }
1191 }
1192
1193 // if encryption is requested, but we still don't know the key, report error
1194 if(DAV_IS_ENCRYPTED(sn) && !(sn->key)) {
1201 fprintf(stderr, "Encryption has been requested, " 1195 fprintf(stderr, "Encryption has been requested, "
1202 "but no default key is configured.\n" 1196 "but no default key is configured.\n"
1203 "You may specify a custom key with the '-k' option.\n"); 1197 "You may specify a custom key with the '-k' option.\n");
1204 return -1; 1198 return 1;
1205 } 1199 }
1206 1200
1201 return 0;
1202 }
1203
1204 int cmd_put(CmdArgs *a, DavBool import) {
1205 if(a->argc != 2) {
1206 // TODO: change, when put supports multiple files (however it should do)
1207 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
1208 fprintf(stderr, "Usage: dav %s\n", find_usage_str("put"));
1209 return -1;
1210 }
1211 if(import) {
1212 ucx_map_cstr_put(a->options, "resursive", "");
1213 }
1214
1215 char *url = a->argv[0];
1216 char *file = a->argv[1];
1217 char *path = NULL;
1218 Repository *repo = url2repo(url, &path);
1219 DavSession *sn = connect_to_repo(repo, path, a);
1220
1221 if(set_session_config(sn, a)) {
1222 return -1;
1223 }
1224 set_session_lock(sn, a);
1225
1226 if(check_encryption_key(a, sn)) {
1227 // TODO: free
1228 return -1;
1229 }
1207 1230
1208 int ret; 1231 int ret;
1209 if(!import) { 1232 if(!import) {
1210 if(!strcmp(file, "-")) { 1233 if(!strcmp(file, "-")) {
1211 FILE *in = stdin; 1234 FILE *in = stdin;
1485 if(set_session_config(sn, a)) { 1508 if(set_session_config(sn, a)) {
1486 return -1; 1509 return -1;
1487 } 1510 }
1488 set_session_lock(sn, a); 1511 set_session_lock(sn, a);
1489 1512
1490 // override the session key if the -k option is specified 1513 if(check_encryption_key(a, sn)) {
1491 char *keyname = cmd_getoption(a, "key"); 1514 // TODO: free
1492 if(keyname) { 1515 return -1;
1493 DavKey *key = dav_context_get_key(ctx, keyname);
1494 if(key) {
1495 sn->key = key;
1496 } else {
1497 fprintf(stderr, "Key %s not found!\nAbort.\n", keyname);
1498 // TODO: free
1499 return -1;
1500 }
1501 } 1516 }
1502 1517
1503 DavResource *res = dav_resource_new(sn, path); 1518 DavResource *res = dav_resource_new(sn, path);
1504 if(!res) { 1519 if(!res) {
1505 fprintf(stderr, "error\n"); 1520 fprintf(stderr, "error\n");
1521 // TODO: free
1506 return -1; 1522 return -1;
1507 } 1523 }
1508 res->iscollection = 1; 1524 res->iscollection = 1;
1509 1525
1510 if(dav_create(res)) { 1526 if(dav_create(res)) {
1511 print_resource_error(sn, res->path); 1527 print_resource_error(sn, res->path);
1512 fprintf(stderr, "Cannot create collection.\n"); 1528 fprintf(stderr, "Cannot create collection.\n");
1529 // TODO: free
1513 return -1; 1530 return -1;
1514 } 1531 }
1515 1532
1516 free(path); 1533 free(path);
1517 return 0; 1534 return 0;

mercurial