Implement DisableDataBoxExternally
[libisds.git] / client / common.c
blob93227e0b01f27bf3e2e8be2275defa701871c107
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 /*#include <locale.h>*/
5 #include <time.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <isds.h>
16 char url[] = "https://www.czebox.cz/DS/";
17 char username[] = "jrfh7i";
18 /*char password[] = "Ab123456";*/
19 char password[] = "Ac123456";
22 void print_DbState(const long int state) {
23 switch(state) {
24 case DBSTATE_ACCESSIBLE: printf("ACCESSIBLE\n"); break;
25 case DBSTATE_TEMP_UNACCESSIBLE: printf("TEMP_UNACCESSIBLE\n"); break;
26 case DBSTATE_NOT_YET_ACCESSIBLE: printf("NOT_YET_ACCESSIBLE\n"); break;
27 case DBSTATE_PERM_UNACCESSIBLE: printf("PERM_UNACCESSIBLE\n"); break;
28 case DBSTATE_REMOVED: printf("REMOVED\n"); break;
29 default: printf("<unknown state %ld>\n", state);
33 void print_DbType(const long int *type) {
34 if (!type) printf("NULL\n");
35 else
36 switch(*type) {
37 case DBTYPE_FO: printf("FO\n"); break;
38 case DBTYPE_PFO: printf("PFO\n"); break;
39 case DBTYPE_PFO_ADVOK: printf("PFO_ADVOK\n"); break;
40 case DBTYPE_PFO_DANPOR: printf("PFO_DAPOR\n"); break;
41 case DBTYPE_PFO_INSSPR: printf("PFO_INSSPR\n"); break;
42 case DBTYPE_PO: printf("PO\n"); break;
43 case DBTYPE_PO_ZAK: printf("PO_ZAK\n"); break;
44 case DBTYPE_PO_REQ: printf("PO_REQ\n"); break;
45 case DBTYPE_OVM: printf("OVM\n"); break;
46 case DBTYPE_OVM_NOTAR: printf("OVM_NOTAR\n"); break;
47 case DBTYPE_OVM_EXEKUT: printf("OVM_EXEKUT\n"); break;
48 case DBTYPE_OVM_REQ: printf("OVM_REQ\n"); break;
49 default: printf("<unknown type %ld>\n", *type);
54 void print_UserType(const long int *type) {
55 if (!type) printf("NULL\n");
56 else
57 switch(*type) {
58 case USERTYPE_PRIMARY: printf("PRIMARY\n"); break;
59 case USERTYPE_ENTRUSTED: printf("ENTRUSTED\n"); break;
60 case USERTYPE_ADMINISTRATOR: printf("ADMINISTRATOR\n"); break;
61 case USERTYPE_OFFICIAL: printf("OFFICIAL\n"); break;
62 default: printf("<unknown type %ld>\n", *type);
67 void print_UserPrivils(const long int *privils) {
69 const char *priviledges[] = {
70 "READ_NON_PERSONAL",
71 "READ_ALL",
72 "CREATE_DM",
73 "VIEW_INFO",
74 "SEARCH_DB",
75 "OWNER_ADM",
76 "READ_VAULT",
77 "ERASE_VAULT"
79 const int priviledges_count = sizeof(priviledges)/sizeof(priviledges[0]);
81 if (!privils) printf("NULL\n");
82 else {
83 printf("%ld (", *privils);
85 for (int i = 0; i < priviledges_count; i++) {
86 if (*privils & (1<<i)) {
87 printf(
88 ((i + 1) == priviledges_count) ? "%s" : "%s|",
89 priviledges[i]);
93 printf(")\n");
98 void print_hash(const struct isds_hash *hash) {
99 if (!hash) {
100 printf("NULL\n");
101 return;
104 switch(hash->algorithm) {
105 case HASH_ALGORITHM_MD5: printf("MD5 "); break;
106 case HASH_ALGORITHM_SHA_1: printf("SHA-1 "); break;
107 case HASH_ALGORITHM_SHA_256: printf("SHA-256 "); break;
108 case HASH_ALGORITHM_SHA_512: printf("SHA-512 "); break;
109 default: printf("<Unknown hash algorithm %d> ", hash->algorithm);
110 break;
113 if (!hash->value) printf("<NULL>");
114 else
115 for (int i = 0; i < hash->length; i++) {
116 if (i > 0) printf(":");
117 printf("%02x", ((uint8_t *)(hash->value))[i]);
120 printf("\n");
124 void print_raw_type(const isds_raw_type type) {
125 switch(type) {
126 case RAWTYPE_INCOMING_MESSAGE:
127 printf("INCOMING_MESSAGE\n"); break;
128 case RAWTYPE_PLAIN_SIGNED_INCOMING_MESSAGE:
129 printf("PLAIN_SIGNED_INCOMING_MESSAGE\n"); break;
130 case RAWTYPE_CMS_SIGNED_INCOMING_MESSAGE:
131 printf("CMS_SIGNED_INCOMING_MESSAGE\n"); break;
132 case RAWTYPE_PLAIN_SIGNED_OUTGOING_MESSAGE:
133 printf("PLAIN_SIGNED_OUTGOING_MESSAGE\n"); break;
134 case RAWTYPE_CMS_SIGNED_OUTGOING_MESSAGE:
135 printf("CMS_SIGNED_OUTGOING_MESSAGE\n"); break;
136 case RAWTYPE_DELIVERYINFO:
137 printf("DELIVERYINFO\n"); break;
138 case RAWTYPE_PLAIN_SIGNED_DELIVERYINFO:
139 printf("PLAIN_SIGNED_DELIVERYINFO\n"); break;
140 case RAWTYPE_CMS_SIGNED_DELIVERYINFO:
141 printf("CMS_SIGNED_DELIVERYINFO\n"); break;
142 default:
143 printf("<Unknown raw type %d> ", type);
144 break;
149 void print_bool(const _Bool *boolean) {
150 printf("%s\n", (!boolean) ? "NULL" : ((*boolean)? "true" : "false") );
154 void print_longint(const long int *number) {
155 if (!number) printf("NULL\n");
156 else printf("%ld\n", *number);
160 void print_PersonName(const struct isds_PersonName *personName) {
161 printf("\tpersonName = ");
162 if (!personName) printf("NULL\n");
163 else {
164 printf("{\n");
165 printf("\t\tpnFirstName = %s\n", personName->pnFirstName);
166 printf("\t\tpnMiddleName = %s\n", personName->pnMiddleName);
167 printf("\t\tpnLastName = %s\n", personName->pnLastName);
168 printf("\t\tpnLastNameAtBirth = %s\n", personName->pnLastNameAtBirth);
169 printf("\t}\n");
174 void print_Address(const struct isds_Address *address) {
175 printf("\taddress = ");
176 if (!address) printf("NULL\n");
177 else {
178 printf("{\n");
179 printf("\t\tadCity = %s\n", address->adCity);
180 printf("\t\tadStreet = %s\n", address->adStreet);
181 printf("\t\tadNumberInStreet = %s\n", address->adNumberInStreet);
182 printf("\t\tadNumberInMunicipality = %s\n",
183 address->adNumberInMunicipality);
184 printf("\t\tadZipCode = %s\n", address->adZipCode);
185 printf("\t\tadState = %s\n", address->adState);
186 printf("\t}\n");
191 void print_date(const struct tm *date) {
192 if (!date) printf("NULL\n");
193 else printf("%s", asctime(date));
197 void print_DbOwnerInfo(const struct isds_DbOwnerInfo *info) {
198 printf("dbOwnerInfo = ");
200 if (!info) {
201 printf("NULL\n");
202 return;
205 printf("{\n");
206 printf("\tdbID = %s\n", info->dbID);
208 printf("\tdbType = ");
209 print_DbType((long int *) (info->dbType));
210 printf("\tic = %s\n", info->ic);
212 print_PersonName(info->personName);
214 printf("\tfirmName = %s\n", info->firmName);
216 printf("\tbirthInfo = ");
217 if (!info->birthInfo) printf("NULL\n");
218 else {
219 printf("{\n");
221 printf("\t\tbiDate = ");
222 print_date(info->birthInfo->biDate);
224 printf("\t\tbiCity = %s\n", info->birthInfo->biCity);
225 printf("\t\tbiCounty = %s\n", info->birthInfo->biCounty);
226 printf("\t\tbiState = %s\n", info->birthInfo->biState);
227 printf("\t}\n");
230 print_Address(info->address);
232 printf("\tnationality = %s\n", info->nationality);
233 printf("\temail = %s\n", info->email);
234 printf("\ttelNumber = %s\n", info->telNumber);
235 printf("\tidentifier = %s\n", info->identifier);
236 printf("\tregistryCode = %s\n", info->registryCode);
238 printf("\tdbState = ");
239 if (!info->dbState) printf("NULL\n");
240 else print_DbState(*(info->dbState));
242 printf("\tdbEffectiveOVM = ");
243 print_bool(info->dbEffectiveOVM);
245 printf("\tdbOpenAddressing = ");
246 print_bool(info->dbOpenAddressing);
248 printf("}\n");
253 void print_DbUserInfo(const struct isds_DbUserInfo *info) {
254 printf("dbUserInfo = ");
256 if (!info) {
257 printf("NULL\n");
258 return;
261 printf("{\n");
262 printf("\tuserID = %s\n", info->userID);
264 printf("\tuserType = ");
265 print_UserType((long int *) (info->userType));
267 printf("\tuserPrivils = ");
268 print_UserPrivils(info->userPrivils);
270 print_PersonName(info->personName);
271 print_Address(info->address);
273 printf("\tbiDate = ");
274 print_date(info->biDate);
276 printf("\tic = %s\n", info->ic);
277 printf("\tfirmName = %s\n", info->firmName);
279 printf("\tcaStreet = %s\n", info->caStreet);
280 printf("\tcaCity = %s\n", info->caCity);
281 printf("\tcaZipCode = %s\n", info->caZipCode);
283 printf("}\n");
287 void print_timeval(const struct timeval *time) {
288 struct tm broken;
289 char buffer[128];
291 if (!time) {
292 printf("NULL\n");
293 return;
296 if (!localtime_r(&(time->tv_sec), &broken)) goto error;
297 if (!strftime(buffer, sizeof(buffer)/sizeof(char), "%c", &broken))
298 goto error;
299 printf("%s, %ld us\n", buffer, time->tv_usec);
300 return;
302 error:
303 printf("<Error while formating>\n>");
304 return;
308 void print_event_type(const isds_event_type *type) {
309 if (!type) {
310 printf("NULL");
311 return;
313 switch (*type) {
314 case EVENT_UKNOWN: printf("UNKNOWN\n"); break;
315 case EVENT_ACCEPTED_BY_RECIPIENT:
316 printf("ACCEPTED_BY_RECIPIENT\n"); break;
317 case EVENT_ACCEPTED_BY_FICTION:
318 printf("DELIVERED_BY_FICTION\n"); break;
319 case EVENT_UNDELIVERABLE:
320 printf("UNDELIVERABLE\n"); break;
321 default: printf("<unknown type %d>\n", *type);
326 void print_events(const struct isds_list *events) {
327 const struct isds_list *item;
328 const struct isds_event *event;
330 if (!events) {
331 printf("NULL\n");
332 return;
335 printf("{\n");
337 for (item = events; item; item = item->next) {
338 event = (struct isds_event *) item->data;
339 printf("\t\t\tevent = ");
340 if (!event) printf("NULL");
341 else {
342 printf("{\n");
344 printf("\t\t\t\ttype = ");
345 print_event_type(event->type);
347 printf("\t\t\t\tdescription = %s\n", event->description);
349 printf("\t\t\t\ttime = ");
350 print_timeval(event->time);
352 printf("\t\t\t}\n");
356 printf("\t\t}\n");
360 void print_envelope(const struct isds_envelope *envelope) {
361 printf("\tenvelope = ");
363 if (!envelope) {
364 printf("NULL\n");
365 return;
367 printf("{\n");
369 printf("\t\tdmID = %s\n", envelope->dmID);
370 printf("\t\tdbIDSender = %s\n", envelope->dbIDSender);
371 printf("\t\tdmSender = %s\n", envelope->dmSender);
372 printf("\t\tdmSenderAddress = %s\n", envelope->dmSenderAddress);
373 printf("\t\tdmSenderType = ");
374 print_DbType(envelope->dmSenderType);
375 printf("\t\tdmRecipient = %s\n", envelope->dmRecipient);
376 printf("\t\tdmRecipientAddress = %s\n", envelope->dmRecipientAddress);
377 printf("\t\tdmAmbiguousRecipient = ");
378 print_bool(envelope->dmAmbiguousRecipient);
379 printf("\t\tdmType = %s\n", envelope->dmType);
381 printf("\t\tdmSenderOrgUnit = %s\n", envelope->dmSenderOrgUnit);
382 printf("\t\tdmSenderOrgUnitNum = ");
383 print_longint(envelope->dmSenderOrgUnitNum);
384 printf("\t\tdbIDRecipient = %s\n", envelope->dbIDRecipient);
385 printf("\t\tdmRecipientOrgUnit = %s\n", envelope->dmRecipientOrgUnit);
386 printf("\t\tdmRecipientOrgUnitNum = ");
387 print_longint(envelope->dmRecipientOrgUnitNum);
388 printf("\t\tdmToHands = %s\n", envelope->dmToHands);
389 printf("\t\tdmAnnotation = %s\n", envelope->dmAnnotation);
390 printf("\t\tdmRecipientRefNumber = %s\n", envelope->dmRecipientRefNumber);
391 printf("\t\tdmSenderRefNumber = %s\n", envelope->dmSenderRefNumber);
392 printf("\t\tdmRecipientIdent = %s\n", envelope->dmRecipientIdent);
393 printf("\t\tdmSenderIdent = %s\n", envelope->dmSenderIdent);
395 printf("\t\tdmLegalTitleLaw = ");
396 print_longint(envelope->dmLegalTitleLaw);
397 printf("\t\tdmLegalTitleYear = ");
398 print_longint(envelope->dmLegalTitleYear);
399 printf("\t\tdmLegalTitleSect = %s\n", envelope->dmLegalTitleSect);
400 printf("\t\tdmLegalTitlePar = %s\n", envelope->dmLegalTitlePar);
401 printf("\t\tdmLegalTitlePoint = %s\n", envelope->dmLegalTitlePoint);
403 printf("\t\tdmPersonalDelivery = ");
404 print_bool(envelope->dmPersonalDelivery);
405 printf("\t\tdmAllowSubstDelivery = ");
406 print_bool(envelope->dmAllowSubstDelivery);
407 printf("\t\tdmOVM = ");
408 print_bool(envelope->dmOVM);
410 printf("\t\tdmOrdinal = ");
411 if (!envelope->dmOrdinal) printf("NULL\n");
412 else printf("%lu\n", *(envelope->dmOrdinal));
414 printf("\t\tdmMessageStatus = ");
415 if (!envelope->dmMessageStatus) printf("NULL\n");
416 else
417 switch(*(envelope->dmMessageStatus)) {
418 case MESSAGESTATE_SENT: printf("SENT\n"); break;
419 case MESSAGESTATE_STAMPED: printf("STAMPED\n"); break;
420 case MESSAGESTATE_INFECTED: printf("INFECTED\n"); break;
421 case MESSAGESTATE_DELIVERED: printf("DELIVERED\n"); break;
422 case MESSAGESTATE_SUBSTITUTED: printf("SUBSTITUTED\n"); break;
423 case MESSAGESTATE_RECEIVED: printf("RECEIVED\n"); break;
424 case MESSAGESTATE_READ: printf("READ\n"); break;
425 case MESSAGESTATE_UNDELIVERABLE: printf("UNDELIVERABLE\n"); break;
426 case MESSAGESTATE_REMOVED: printf("REMOVED\n"); break;
427 case MESSAGESTATE_IN_SAFE: printf("IN_SAFE\n"); break;
428 default: printf("<unknown type %d>\n",
429 *(envelope->dmMessageStatus));
432 printf("\t\tdmAttachmentSize = ");
433 if (!envelope->dmAttachmentSize) printf("NULL\n");
434 else printf("%lu kB\n", *(envelope->dmAttachmentSize));
436 printf("\t\tdmDeliveryTime = ");
437 print_timeval(envelope->dmDeliveryTime);
439 printf("\t\tdmAcceptanceTime = ");
440 print_timeval(envelope->dmAcceptanceTime);
442 printf("\t\thash = ");
443 print_hash(envelope->hash);
445 printf("\t\ttimestamp = %p\n", envelope->timestamp);
446 printf("\t\ttimestamp_length = %zu\n", envelope->timestamp_length);
448 printf("\t\tevents = ");
449 print_events(envelope->events);
451 printf("\t}\n");
455 void print_document(const struct isds_document *document) {
456 printf("\t\tdocument = ");
458 if (!document) {
459 printf("NULL\n");
460 return;
462 printf("\{\n");
464 printf("\t\t\tdata = %p\n", document->data);
465 printf("\t\t\tdata_length = %zu\n", document->data_length);
466 printf("\t\t\tdmMimeType = %s\n", document->dmMimeType);
468 printf("\t\t\tdmFileMetaType = ");
469 switch(document->dmFileMetaType) {
470 case FILEMETATYPE_MAIN: printf("MAIN\n"); break;
471 case FILEMETATYPE_ENCLOSURE: printf("ENCLOSURE\n"); break;
472 case FILEMETATYPE_SIGNATURE: printf("SIGNATURE\n"); break;
473 case FILEMETATYPE_META: printf("META\n"); break;
474 default: printf("<unknown type %d>\n", document->dmFileMetaType);
477 printf("\t\t\tdmFileGuid = %s\n", document->dmFileGuid);
478 printf("\t\t\tdmUpFileGuid = %s\n", document->dmUpFileGuid);
479 printf("\t\t\tdmFileDescr = %s\n", document->dmFileDescr);
480 printf("\t\t\tdmFormat = %s\n", document->dmFormat);
481 printf("\t\t}\n");
485 void print_documents(const struct isds_list *documents) {
486 const struct isds_list *item;
488 printf("\tdocuments = ");
490 if (!documents) {
491 printf("NULL\n");
492 return;
494 printf("{\n");
496 for (item = documents; item; item = item->next) {
497 print_document((struct isds_document *) (item->data));
500 printf("\t}\n");
504 void print_message(const struct isds_message *message) {
505 printf("message = ");
507 if (!message) {
508 printf("NULL\n");
509 return;
512 printf("{\n");
514 printf("\traw = %p\n", message->raw);
515 printf("\traw_length = %zu\n", message->raw_length);
516 printf("\traw_type = ");
517 print_raw_type(message->raw_type);
518 print_envelope(message->envelope);
519 print_documents(message->documents);
521 printf("}\n");
524 void print_copies(const struct isds_list *copies) {
525 const struct isds_list *item;
526 struct isds_message_copy *copy;
528 printf("Copies = ");
529 if (!copies) {
530 printf("<NULL>\n");
531 return;
534 printf("{\n");
535 for (item = copies; item; item = item->next) {
536 copy = (struct isds_message_copy *) item->data;
537 printf("\tCopy = ");
539 if (!copy)
540 printf("<NULL>\n");
541 else {
542 printf("{\n");
543 printf("\t\tdbIDRecipient = %s\n", copy->dbIDRecipient);
544 printf("\t\tdmRecipientOrgUnit = %s\n", copy->dmRecipientOrgUnit);
546 printf("\t\tdmRecipientOrgUnitNum = ");
547 if (copy->dmRecipientOrgUnitNum)
548 printf("%ld\n", *copy->dmRecipientOrgUnitNum);
549 else
550 printf("<NULL>\n");
551 printf("\t\tdmToHands = %s\n", copy->dmToHands);
553 printf("\t\terror = %s\n", isds_strerror(copy->error));
554 printf("\t\tdmStatus = %s\n", copy->dmStatus);
555 printf("\t\tdmID = %s\n", copy->dmID);
556 printf("\t}\n");
559 printf("}\n");
563 void compare_hashes(const struct isds_hash *hash1,
564 const struct isds_hash *hash2) {
565 isds_error err;
567 printf("Comparing hashes... ");
568 err = isds_hash_cmp(hash1, hash2);
569 if (err == IE_SUCCESS)
570 printf("Hashes equal\n");
571 else if
572 (err == IE_NOTEQUAL) printf("Hashes differ\n");
573 else
574 printf("isds_hash_cmp() failed: %s\n", isds_strerror(err));
578 int progressbar(double upload_total, double upload_current,
579 double download_total, double download_current,
580 void *data) {
582 printf("Progress: upload %0f/%0f, download %0f/%0f, data=%p\n",
583 upload_current, upload_total, download_current, download_total,
584 data);
585 return 0;
589 int mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
590 struct stat file_info;
592 if (!file || !fd || !buffer || !length) return -1;
595 *fd = open(file, O_RDONLY);
596 if (*fd == -1) {
597 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
598 return -1;
601 if (-1 == fstat(*fd, &file_info)) {
602 fprintf(stderr, "%s: Could not get file size: %s\n", file,
603 strerror(errno));
604 close(*fd);
605 return -1;
607 if (file_info.st_size < 0) {
608 fprintf(stderr, "File `%s' has negative size: %jd\n", file,
609 file_info.st_size);
610 close(*fd);
611 return -1;
613 *length = file_info.st_size;
615 *buffer = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, *fd, 0);
616 if (*buffer == MAP_FAILED) {
617 fprintf(stderr, "%s: Could not map file to memmory: %s\n", file,
618 strerror(errno));
619 close(*fd);
620 return -1;
623 return 0;
627 int munmap_file(int fd, void *buffer, size_t length) {
628 int err = 0;
629 long int page_size = sysconf(_SC_PAGE_SIZE);
630 size_t pages = (length % page_size) ?
631 ((length / page_size) + 1) * page_size:
632 length;
634 err = munmap(buffer, pages);
635 if (err) {
636 fprintf(stderr, "Could not unmap memory at %p and length %zu: %s\n",
637 buffer, pages, strerror(errno));
640 err = close(fd);
641 if (err) {
642 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
643 strerror(errno));
646 return err;
650 static int save_data_to_file(const char *file, const void *data,
651 const size_t length) {
652 int fd;
653 ssize_t written, left = length;
655 if (!file) return -1;
656 if (length > 0 && !data) return -1;
658 fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0666);
659 if (fd == -1) {
660 fprintf(stderr, "%s: Could not open file for writing: %s\n",
661 file, strerror(errno));
662 return -1;
665 printf("Writing %zu bytes to file `%s'...\n", length, file);
666 while (left) {
667 written = write(fd, data + length - left, left);
668 if (written == -1) {
669 fprintf(stderr, "%s: Could not save file: %s\n",
670 file, strerror(errno));
671 close(fd);
672 return -1;
674 left-=written;
677 if (-1 == close(fd)) {
678 fprintf(stderr, "%s: Closing file failed: %s\n",
679 file, strerror(errno));
680 return -1;
683 printf("Done.\n");
684 return 0;
688 int save_data(const char *message, const void *data, const size_t length) {
689 if (message)
690 printf("%s\n", message);
691 return save_data_to_file("output", data, length);