test: guess_raw_type: plain signed incoming message
[libisds.git] / client / common.c
blob9a2e4a0533b5b3d871b452f659862d4894184522
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 username[] = "jrfh7i";*/
17 /*char password[] = "Ab123456";*/
18 /*char password[] = "Ac123456";*/
20 char username[] = "5s59sd";
21 /*char password[] = "Ab123456";*/
22 char password[] = "Ac123456";
25 void print_DbState(const long int state) {
26 switch(state) {
27 case DBSTATE_ACCESSIBLE: printf("ACCESSIBLE\n"); break;
28 case DBSTATE_TEMP_UNACCESSIBLE: printf("TEMP_UNACCESSIBLE\n"); break;
29 case DBSTATE_NOT_YET_ACCESSIBLE: printf("NOT_YET_ACCESSIBLE\n"); break;
30 case DBSTATE_PERM_UNACCESSIBLE: printf("PERM_UNACCESSIBLE\n"); break;
31 case DBSTATE_REMOVED: printf("REMOVED\n"); break;
32 default: printf("<unknown state %ld>\n", state);
36 void print_DbType(const long int *type) {
37 if (!type) printf("NULL\n");
38 else
39 switch(*type) {
40 case DBTYPE_FO: printf("FO\n"); break;
41 case DBTYPE_PFO: printf("PFO\n"); break;
42 case DBTYPE_PFO_ADVOK: printf("PFO_ADVOK\n"); break;
43 case DBTYPE_PFO_DANPOR: printf("PFO_DAPOR\n"); break;
44 case DBTYPE_PFO_INSSPR: printf("PFO_INSSPR\n"); break;
45 case DBTYPE_PO: printf("PO\n"); break;
46 case DBTYPE_PO_ZAK: printf("PO_ZAK\n"); break;
47 case DBTYPE_PO_REQ: printf("PO_REQ\n"); break;
48 case DBTYPE_OVM: printf("OVM\n"); break;
49 case DBTYPE_OVM_NOTAR: printf("OVM_NOTAR\n"); break;
50 case DBTYPE_OVM_EXEKUT: printf("OVM_EXEKUT\n"); break;
51 case DBTYPE_OVM_REQ: printf("OVM_REQ\n"); break;
52 default: printf("<unknown type %ld>\n", *type);
57 void print_UserType(const long int *type) {
58 if (!type) printf("NULL\n");
59 else
60 switch(*type) {
61 case USERTYPE_PRIMARY: printf("PRIMARY\n"); break;
62 case USERTYPE_ENTRUSTED: printf("ENTRUSTED\n"); break;
63 case USERTYPE_ADMINISTRATOR: printf("ADMINISTRATOR\n"); break;
64 case USERTYPE_OFFICIAL: printf("OFFICIAL\n"); break;
65 default: printf("<unknown type %ld>\n", *type);
70 void print_UserPrivils(const long int *privils) {
72 const char *priviledges[] = {
73 "READ_NON_PERSONAL",
74 "READ_ALL",
75 "CREATE_DM",
76 "VIEW_INFO",
77 "SEARCH_DB",
78 "OWNER_ADM",
79 "READ_VAULT",
80 "ERASE_VAULT"
82 const int priviledges_count = sizeof(priviledges)/sizeof(priviledges[0]);
84 if (!privils) printf("NULL\n");
85 else {
86 printf("%ld (", *privils);
88 for (int i = 0; i < priviledges_count; i++) {
89 if (*privils & (1<<i)) {
90 printf(
91 ((i + 1) == priviledges_count) ? "%s" : "%s|",
92 priviledges[i]);
96 printf(")\n");
101 void print_hash(const struct isds_hash *hash) {
102 if (!hash) {
103 printf("NULL\n");
104 return;
107 switch(hash->algorithm) {
108 case HASH_ALGORITHM_MD5: printf("MD5 "); break;
109 case HASH_ALGORITHM_SHA_1: printf("SHA-1 "); break;
110 case HASH_ALGORITHM_SHA_256: printf("SHA-256 "); break;
111 case HASH_ALGORITHM_SHA_512: printf("SHA-512 "); break;
112 default: printf("<Unknown hash algorithm %d> ", hash->algorithm);
113 break;
116 if (!hash->value) printf("<NULL>");
117 else
118 for (int i = 0; i < hash->length; i++) {
119 if (i > 0) printf(":");
120 printf("%02x", ((uint8_t *)(hash->value))[i]);
123 printf("\n");
127 void print_raw_type(const isds_raw_type type) {
128 switch(type) {
129 case RAWTYPE_INCOMING_MESSAGE:
130 printf("INCOMING_MESSAGE\n"); break;
131 case RAWTYPE_PLAIN_SIGNED_INCOMING_MESSAGE:
132 printf("PLAIN_SIGNED_INCOMING_MESSAGE\n"); break;
133 case RAWTYPE_CMS_SIGNED_INCOMING_MESSAGE:
134 printf("CMS_SIGNED_INCOMING_MESSAGE\n"); break;
135 case RAWTYPE_PLAIN_SIGNED_OUTGOING_MESSAGE:
136 printf("PLAIN_SIGNED_OUTGOING_MESSAGE\n"); break;
137 case RAWTYPE_CMS_SIGNED_OUTGOING_MESSAGE:
138 printf("CMS_SIGNED_OUTGOING_MESSAGE\n"); break;
139 case RAWTYPE_DELIVERYINFO:
140 printf("DELIVERYINFO\n"); break;
141 case RAWTYPE_PLAIN_SIGNED_DELIVERYINFO:
142 printf("PLAIN_SIGNED_DELIVERYINFO\n"); break;
143 case RAWTYPE_CMS_SIGNED_DELIVERYINFO:
144 printf("CMS_SIGNED_DELIVERYINFO\n"); break;
145 default:
146 printf("<Unknown raw type %d> ", type);
147 break;
152 void print_bool(const _Bool *boolean) {
153 printf("%s\n", (!boolean) ? "NULL" : ((*boolean)? "true" : "false") );
157 void print_longint(const long int *number) {
158 if (!number) printf("NULL\n");
159 else printf("%ld\n", *number);
163 void print_PersonName(const struct isds_PersonName *personName) {
164 printf("\tpersonName = ");
165 if (!personName) printf("NULL\n");
166 else {
167 printf("{\n");
168 printf("\t\tpnFirstName = %s\n", personName->pnFirstName);
169 printf("\t\tpnMiddleName = %s\n", personName->pnMiddleName);
170 printf("\t\tpnLastName = %s\n", personName->pnLastName);
171 printf("\t\tpnLastNameAtBirth = %s\n", personName->pnLastNameAtBirth);
172 printf("\t}\n");
177 void print_Address(const struct isds_Address *address) {
178 printf("\taddress = ");
179 if (!address) printf("NULL\n");
180 else {
181 printf("{\n");
182 printf("\t\tadCity = %s\n", address->adCity);
183 printf("\t\tadStreet = %s\n", address->adStreet);
184 printf("\t\tadNumberInStreet = %s\n", address->adNumberInStreet);
185 printf("\t\tadNumberInMunicipality = %s\n",
186 address->adNumberInMunicipality);
187 printf("\t\tadZipCode = %s\n", address->adZipCode);
188 printf("\t\tadState = %s\n", address->adState);
189 printf("\t}\n");
194 void print_date(const struct tm *date) {
195 if (!date) printf("NULL\n");
196 else printf("%s", asctime(date));
200 void print_DbOwnerInfo(const struct isds_DbOwnerInfo *info) {
201 printf("dbOwnerInfo = ");
203 if (!info) {
204 printf("NULL\n");
205 return;
208 printf("{\n");
209 printf("\tdbID = %s\n", info->dbID);
211 printf("\tdbType = ");
212 print_DbType((long int *) (info->dbType));
213 printf("\tic = %s\n", info->ic);
215 print_PersonName(info->personName);
217 printf("\tfirmName = %s\n", info->firmName);
219 printf("\tbirthInfo = ");
220 if (!info->birthInfo) printf("NULL\n");
221 else {
222 printf("{\n");
224 printf("\t\tbiDate = ");
225 print_date(info->birthInfo->biDate);
227 printf("\t\tbiCity = %s\n", info->birthInfo->biCity);
228 printf("\t\tbiCounty = %s\n", info->birthInfo->biCounty);
229 printf("\t\tbiState = %s\n", info->birthInfo->biState);
230 printf("\t}\n");
233 print_Address(info->address);
235 printf("\tnationality = %s\n", info->nationality);
236 printf("\temail = %s\n", info->email);
237 printf("\ttelNumber = %s\n", info->telNumber);
238 printf("\tidentifier = %s\n", info->identifier);
239 printf("\tregistryCode = %s\n", info->registryCode);
241 printf("\tdbState = ");
242 if (!info->dbState) printf("NULL\n");
243 else print_DbState(*(info->dbState));
245 printf("\tdbEffectiveOVM = ");
246 print_bool(info->dbEffectiveOVM);
248 printf("\tdbOpenAddressing = ");
249 print_bool(info->dbOpenAddressing);
251 printf("}\n");
256 void print_DbUserInfo(const struct isds_DbUserInfo *info) {
257 printf("dbUserInfo = ");
259 if (!info) {
260 printf("NULL\n");
261 return;
264 printf("{\n");
265 printf("\tuserID = %s\n", info->userID);
267 printf("\tuserType = ");
268 print_UserType((long int *) (info->userType));
270 printf("\tuserPrivils = ");
271 print_UserPrivils(info->userPrivils);
273 print_PersonName(info->personName);
274 print_Address(info->address);
276 printf("\tbiDate = ");
277 print_date(info->biDate);
279 printf("\tic = %s\n", info->ic);
280 printf("\tfirmName = %s\n", info->firmName);
282 printf("\tcaStreet = %s\n", info->caStreet);
283 printf("\tcaCity = %s\n", info->caCity);
284 printf("\tcaZipCode = %s\n", info->caZipCode);
286 printf("}\n");
290 void print_timeval(const struct timeval *time) {
291 struct tm broken;
292 char buffer[128];
294 if (!time) {
295 printf("NULL\n");
296 return;
299 if (!localtime_r(&(time->tv_sec), &broken)) goto error;
300 if (!strftime(buffer, sizeof(buffer)/sizeof(char), "%c", &broken))
301 goto error;
302 printf("%s, %ld us\n", buffer, time->tv_usec);
303 return;
305 error:
306 printf("<Error while formating>\n>");
307 return;
311 void print_event_type(const isds_event_type *type) {
312 if (!type) {
313 printf("NULL");
314 return;
316 switch (*type) {
317 case EVENT_UKNOWN: printf("UNKNOWN\n"); break;
318 case EVENT_ACCEPTED_BY_RECIPIENT:
319 printf("ACCEPTED_BY_RECIPIENT\n"); break;
320 case EVENT_ACCEPTED_BY_FICTION:
321 printf("DELIVERED_BY_FICTION\n"); break;
322 case EVENT_UNDELIVERABLE:
323 printf("UNDELIVERABLE\n"); break;
324 default: printf("<unknown type %d>\n", *type);
329 void print_events(const struct isds_list *events) {
330 const struct isds_list *item;
331 const struct isds_event *event;
333 if (!events) {
334 printf("NULL\n");
335 return;
338 printf("{\n");
340 for (item = events; item; item = item->next) {
341 event = (struct isds_event *) item->data;
342 printf("\t\t\tevent = ");
343 if (!event) printf("NULL");
344 else {
345 printf("{\n");
347 printf("\t\t\t\ttype = ");
348 print_event_type(event->type);
350 printf("\t\t\t\tdescription = %s\n", event->description);
352 printf("\t\t\t\ttime = ");
353 print_timeval(event->time);
355 printf("\t\t\t}\n");
359 printf("\t\t}\n");
363 void print_envelope(const struct isds_envelope *envelope) {
364 printf("\tenvelope = ");
366 if (!envelope) {
367 printf("NULL\n");
368 return;
370 printf("{\n");
372 printf("\t\tdmID = %s\n", envelope->dmID);
373 printf("\t\tdbIDSender = %s\n", envelope->dbIDSender);
374 printf("\t\tdmSender = %s\n", envelope->dmSender);
375 printf("\t\tdmSenderAddress = %s\n", envelope->dmSenderAddress);
376 printf("\t\tdmSenderType = ");
377 print_DbType(envelope->dmSenderType);
378 printf("\t\tdmRecipient = %s\n", envelope->dmRecipient);
379 printf("\t\tdmRecipientAddress = %s\n", envelope->dmRecipientAddress);
380 printf("\t\tdmAmbiguousRecipient = ");
381 print_bool(envelope->dmAmbiguousRecipient);
382 printf("\t\tdmType = %s\n", envelope->dmType);
384 printf("\t\tdmSenderOrgUnit = %s\n", envelope->dmSenderOrgUnit);
385 printf("\t\tdmSenderOrgUnitNum = ");
386 print_longint(envelope->dmSenderOrgUnitNum);
387 printf("\t\tdbIDRecipient = %s\n", envelope->dbIDRecipient);
388 printf("\t\tdmRecipientOrgUnit = %s\n", envelope->dmRecipientOrgUnit);
389 printf("\t\tdmRecipientOrgUnitNum = ");
390 print_longint(envelope->dmRecipientOrgUnitNum);
391 printf("\t\tdmToHands = %s\n", envelope->dmToHands);
392 printf("\t\tdmAnnotation = %s\n", envelope->dmAnnotation);
393 printf("\t\tdmRecipientRefNumber = %s\n", envelope->dmRecipientRefNumber);
394 printf("\t\tdmSenderRefNumber = %s\n", envelope->dmSenderRefNumber);
395 printf("\t\tdmRecipientIdent = %s\n", envelope->dmRecipientIdent);
396 printf("\t\tdmSenderIdent = %s\n", envelope->dmSenderIdent);
398 printf("\t\tdmLegalTitleLaw = ");
399 print_longint(envelope->dmLegalTitleLaw);
400 printf("\t\tdmLegalTitleYear = ");
401 print_longint(envelope->dmLegalTitleYear);
402 printf("\t\tdmLegalTitleSect = %s\n", envelope->dmLegalTitleSect);
403 printf("\t\tdmLegalTitlePar = %s\n", envelope->dmLegalTitlePar);
404 printf("\t\tdmLegalTitlePoint = %s\n", envelope->dmLegalTitlePoint);
406 printf("\t\tdmPersonalDelivery = ");
407 print_bool(envelope->dmPersonalDelivery);
408 printf("\t\tdmAllowSubstDelivery = ");
409 print_bool(envelope->dmAllowSubstDelivery);
410 printf("\t\tdmOVM = ");
411 print_bool(envelope->dmOVM);
413 printf("\t\tdmOrdinal = ");
414 if (!envelope->dmOrdinal) printf("NULL\n");
415 else printf("%lu\n", *(envelope->dmOrdinal));
417 printf("\t\tdmMessageStatus = ");
418 if (!envelope->dmMessageStatus) printf("NULL\n");
419 else
420 switch(*(envelope->dmMessageStatus)) {
421 case MESSAGESTATE_SENT: printf("SENT\n"); break;
422 case MESSAGESTATE_STAMPED: printf("STAMPED\n"); break;
423 case MESSAGESTATE_INFECTED: printf("INFECTED\n"); break;
424 case MESSAGESTATE_DELIVERED: printf("DELIVERED\n"); break;
425 case MESSAGESTATE_SUBSTITUTED: printf("SUBSTITUTED\n"); break;
426 case MESSAGESTATE_RECEIVED: printf("RECEIVED\n"); break;
427 case MESSAGESTATE_READ: printf("READ\n"); break;
428 case MESSAGESTATE_UNDELIVERABLE: printf("UNDELIVERABLE\n"); break;
429 case MESSAGESTATE_REMOVED: printf("REMOVED\n"); break;
430 case MESSAGESTATE_IN_SAFE: printf("IN_SAFE\n"); break;
431 default: printf("<unknown type %d>\n",
432 *(envelope->dmMessageStatus));
435 printf("\t\tdmAttachmentSize = ");
436 if (!envelope->dmAttachmentSize) printf("NULL\n");
437 else printf("%lu kB\n", *(envelope->dmAttachmentSize));
439 printf("\t\tdmDeliveryTime = ");
440 print_timeval(envelope->dmDeliveryTime);
442 printf("\t\tdmAcceptanceTime = ");
443 print_timeval(envelope->dmAcceptanceTime);
445 printf("\t\thash = ");
446 print_hash(envelope->hash);
448 printf("\t\ttimestamp = %p\n", envelope->timestamp);
449 printf("\t\ttimestamp_length = %zu\n", envelope->timestamp_length);
451 printf("\t\tevents = ");
452 print_events(envelope->events);
454 printf("\t}\n");
458 void print_document(const struct isds_document *document) {
459 printf("\t\tdocument = ");
461 if (!document) {
462 printf("NULL\n");
463 return;
465 printf("\{\n");
467 printf("\t\t\tdata = %p\n", document->data);
468 printf("\t\t\tdata_length = %zu\n", document->data_length);
469 printf("\t\t\tdmMimeType = %s\n", document->dmMimeType);
471 printf("\t\t\tdmFileMetaType = ");
472 switch(document->dmFileMetaType) {
473 case FILEMETATYPE_MAIN: printf("MAIN\n"); break;
474 case FILEMETATYPE_ENCLOSURE: printf("ENCLOSURE\n"); break;
475 case FILEMETATYPE_SIGNATURE: printf("SIGNATURE\n"); break;
476 case FILEMETATYPE_META: printf("META\n"); break;
477 default: printf("<unknown type %d>\n", document->dmFileMetaType);
480 printf("\t\t\tdmFileGuid = %s\n", document->dmFileGuid);
481 printf("\t\t\tdmUpFileGuid = %s\n", document->dmUpFileGuid);
482 printf("\t\t\tdmFileDescr = %s\n", document->dmFileDescr);
483 printf("\t\t\tdmFormat = %s\n", document->dmFormat);
484 printf("\t\t}\n");
488 void print_documents(const struct isds_list *documents) {
489 const struct isds_list *item;
491 printf("\tdocuments = ");
493 if (!documents) {
494 printf("NULL\n");
495 return;
497 printf("{\n");
499 for (item = documents; item; item = item->next) {
500 print_document((struct isds_document *) (item->data));
503 printf("\t}\n");
507 void print_message(const struct isds_message *message) {
508 printf("message = ");
510 if (!message) {
511 printf("NULL\n");
512 return;
515 printf("{\n");
517 printf("\traw = %p\n", message->raw);
518 printf("\traw_length = %zu\n", message->raw_length);
519 printf("\traw_type = ");
520 print_raw_type(message->raw_type);
521 print_envelope(message->envelope);
522 print_documents(message->documents);
524 printf("}\n");
527 void print_copies(const struct isds_list *copies) {
528 const struct isds_list *item;
529 struct isds_message_copy *copy;
531 printf("Copies = ");
532 if (!copies) {
533 printf("<NULL>\n");
534 return;
537 printf("{\n");
538 for (item = copies; item; item = item->next) {
539 copy = (struct isds_message_copy *) item->data;
540 printf("\tCopy = ");
542 if (!copy)
543 printf("<NULL>\n");
544 else {
545 printf("{\n");
546 printf("\t\tdbIDRecipient = %s\n", copy->dbIDRecipient);
547 printf("\t\tdmRecipientOrgUnit = %s\n", copy->dmRecipientOrgUnit);
549 printf("\t\tdmRecipientOrgUnitNum = ");
550 if (copy->dmRecipientOrgUnitNum)
551 printf("%ld\n", *copy->dmRecipientOrgUnitNum);
552 else
553 printf("<NULL>\n");
554 printf("\t\tdmToHands = %s\n", copy->dmToHands);
556 printf("\t\terror = %s\n", isds_strerror(copy->error));
557 printf("\t\tdmStatus = %s\n", copy->dmStatus);
558 printf("\t\tdmID = %s\n", copy->dmID);
559 printf("\t}\n");
562 printf("}\n");
566 void compare_hashes(const struct isds_hash *hash1,
567 const struct isds_hash *hash2) {
568 isds_error err;
570 printf("Comparing hashes... ");
571 err = isds_hash_cmp(hash1, hash2);
572 if (err == IE_SUCCESS)
573 printf("Hashes equal\n");
574 else if
575 (err == IE_NOTEQUAL) printf("Hashes differ\n");
576 else
577 printf("isds_hash_cmp() failed: %s\n", isds_strerror(err));
581 int progressbar(double upload_total, double upload_current,
582 double download_total, double download_current,
583 void *data) {
585 printf("Progress: upload %0f/%0f, download %0f/%0f, data=%p\n",
586 upload_current, upload_total, download_current, download_total,
587 data);
588 if (data) {
589 printf("Aborting transfer...\n");
590 return 1;
592 return 0;
596 int mmap_file(const char *file, int *fd, void **buffer, size_t *length) {
597 struct stat file_info;
599 if (!file || !fd || !buffer || !length) return -1;
602 *fd = open(file, O_RDONLY);
603 if (*fd == -1) {
604 fprintf(stderr, "%s: Could not open file: %s\n", file, strerror(errno));
605 return -1;
608 if (-1 == fstat(*fd, &file_info)) {
609 fprintf(stderr, "%s: Could not get file size: %s\n", file,
610 strerror(errno));
611 close(*fd);
612 return -1;
614 if (file_info.st_size < 0) {
615 fprintf(stderr, "File `%s' has negative size: %jd\n", file,
616 (intmax_t) file_info.st_size);
617 close(*fd);
618 return -1;
620 *length = file_info.st_size;
622 *buffer = mmap(NULL, *length, PROT_READ, MAP_PRIVATE, *fd, 0);
623 if (*buffer == MAP_FAILED) {
624 fprintf(stderr, "%s: Could not map file to memory: %s\n", file,
625 strerror(errno));
626 close(*fd);
627 return -1;
630 return 0;
634 int munmap_file(int fd, void *buffer, size_t length) {
635 int err = 0;
636 long int page_size = sysconf(_SC_PAGE_SIZE);
637 size_t pages = (length % page_size) ?
638 ((length / page_size) + 1) * page_size:
639 length;
641 err = munmap(buffer, pages);
642 if (err) {
643 fprintf(stderr, "Could not unmap memory at %p and length %zu: %s\n",
644 buffer, pages, strerror(errno));
647 err = close(fd);
648 if (err) {
649 fprintf(stderr, "Could close file descriptor %d: %s\n", fd,
650 strerror(errno));
653 return err;
657 static int save_data_to_file(const char *file, const void *data,
658 const size_t length) {
659 int fd;
660 ssize_t written, left = length;
662 if (!file) return -1;
663 if (length > 0 && !data) return -1;
665 fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0666);
666 if (fd == -1) {
667 fprintf(stderr, "%s: Could not open file for writing: %s\n",
668 file, strerror(errno));
669 return -1;
672 printf("Writing %zu bytes to file `%s'...\n", length, file);
673 while (left) {
674 written = write(fd, data + length - left, left);
675 if (written == -1) {
676 fprintf(stderr, "%s: Could not save file: %s\n",
677 file, strerror(errno));
678 close(fd);
679 return -1;
681 left-=written;
684 if (-1 == close(fd)) {
685 fprintf(stderr, "%s: Closing file failed: %s\n",
686 file, strerror(errno));
687 return -1;
690 printf("Done.\n");
691 return 0;
695 int save_data(const char *message, const void *data, const size_t length) {
696 if (message)
697 printf("%s\n", message);
698 return save_data_to_file("output", data, length);