comparison pwfile.c @ 10:17fb30016e64

Enable access to record and file metadata Add info command to show file metadata. Enable display of creation and modification dates of records.
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Fri, 28 Jul 2017 15:53:57 +0200
parents 60c8ab006e55
children cf81eb0c2d5a
comparison
equal deleted inserted replaced
9:60c8ab006e55 10:17fb30016e64
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */ 22 */
23 23
24 #include "compat.h" 24 #include "compat.h"
25 25
26 #include <ctype.h>
26 #ifdef HAVE_ERR_H 27 #ifdef HAVE_ERR_H
27 #include <err.h> 28 #include <err.h>
28 #endif /* HAVE_ERR_H */ 29 #endif /* HAVE_ERR_H */
29 #include <errno.h> 30 #include <errno.h>
30 #include <stdio.h> 31 #include <stdio.h>
595 } 596 }
596 597
597 free(list); 598 free(list);
598 } 599 }
599 600
601 static int
602 parse_user_host(const char *user_host, char **userp, char **hostp)
603 {
604 size_t user_host_len;
605 size_t i;
606 unsigned int user_len;
607
608 user_host_len = strlen(user_host);
609 if (user_host_len < 4) {
610 return (-1);
611 }
612 for (i = 0; i < 4; i++) {
613 if (!isxdigit(user_host[i])) {
614 return (-1);
615 }
616 }
617 if (sscanf(user_host, "%04x", &user_len) != 1) {
618 return (-1);
619 }
620 if (4 + (size_t)user_len > user_host_len) {
621 return (-1);
622 }
623
624 xasprintf(userp, "%.*s", (int)user_len, user_host + 4);
625 xasprintf(hostp, "%s", user_host + 4 + user_len);
626
627 return (0);
628 }
629
630 struct metadata *
631 pwfile_get_metadata(struct pwm_ctx *ctx)
632 {
633 struct metadata *metadata;
634 struct pws3_field *version_field;
635 struct pws3_field *save_app_field;
636 struct pws3_field *save_timestamp_field;
637 struct pws3_field *save_user_field;
638 struct pws3_field *save_host_field;
639 struct pws3_field *save_user_host_field;
640
641 metadata = xmalloc(sizeof (struct metadata));
642
643 version_field = pws3_file_get_header_field(ctx->file,
644 PWS3_HEADER_FIELD_VERSION);
645 metadata->version = pws3_field_get_uint16(version_field);
646
647 save_app_field = pws3_file_get_header_field(ctx->file,
648 PWS3_HEADER_FIELD_SAVE_APPLICATION);
649 metadata->application = (save_app_field != NULL) ?
650 xstrdup(pws3_field_get_text(save_app_field)) : NULL;
651
652 save_timestamp_field = pws3_file_get_header_field(ctx->file,
653 PWS3_HEADER_FIELD_SAVE_TIMESTAMP);
654 metadata->timestamp = (save_timestamp_field != NULL) ?
655 pws3_field_get_time(save_timestamp_field) : 0;
656
657 save_user_field = pws3_file_get_header_field(ctx->file,
658 PWS3_HEADER_FIELD_SAVE_USER);
659 save_host_field = pws3_file_get_header_field(ctx->file,
660 PWS3_HEADER_FIELD_SAVE_HOST);
661 save_user_host_field = pws3_file_get_header_field(ctx->file,
662 PWS3_HEADER_FIELD_SAVE_USER_HOST);
663 metadata->user = NULL;
664 metadata->host = NULL;
665 if ((save_user_field != NULL) && (save_host_field != NULL)) {
666 metadata->user = xstrdup(pws3_field_get_text(save_user_field));
667 metadata->host = xstrdup(pws3_field_get_text(save_host_field));
668 } else if (save_user_host_field != NULL) {
669 parse_user_host(pws3_field_get_text(save_user_host_field),
670 &metadata->user, &metadata->host);
671 }
672
673 return (metadata);
674 }
675
676 void
677 pwfile_destroy_metadata(struct metadata *metadata)
678 {
679 if (metadata == NULL) {
680 return;
681 }
682
683 free(metadata->user);
684 free(metadata->host);
685 free(metadata->application);
686 free(metadata);
687 }
688
600 static void 689 static void
601 update_record(struct pws3_record *pws3_record, struct record *record) 690 update_record(struct pws3_record *pws3_record, struct record *record)
602 { 691 {
603 time_t now; 692 time_t now;
604 struct pws3_field *ctime_field; 693 struct pws3_field *ctime_field;
818 pwfile_get_record(struct pwm_ctx *ctx, unsigned int id) 907 pwfile_get_record(struct pwm_ctx *ctx, unsigned int id)
819 { 908 {
820 struct record *record; 909 struct record *record;
821 const unsigned char *uuid; 910 const unsigned char *uuid;
822 struct pws3_record *pws3_record; 911 struct pws3_record *pws3_record;
912 struct pws3_field *ctime_field;
913 struct pws3_field *mtime_field;
823 struct pws3_field *title_field; 914 struct pws3_field *title_field;
824 struct pws3_field *group_field; 915 struct pws3_field *group_field;
825 struct pws3_field *username_field; 916 struct pws3_field *username_field;
826 struct pws3_field *password_field; 917 struct pws3_field *password_field;
827 struct pws3_field *notes_field; 918 struct pws3_field *notes_field;
832 return (NULL); 923 return (NULL);
833 } 924 }
834 pws3_record = pws3_file_get_record(ctx->file, uuid); 925 pws3_record = pws3_file_get_record(ctx->file, uuid);
835 926
836 record = xmalloc(sizeof (struct record)); 927 record = xmalloc(sizeof (struct record));
928
929 ctime_field = pws3_record_get_field(pws3_record,
930 PWS3_RECORD_FIELD_CREATION_TIME);
931 record->ctime = (ctime_field != NULL) ?
932 pws3_field_get_time(ctime_field) : (time_t)0;
933
934 mtime_field = pws3_record_get_field(pws3_record,
935 PWS3_RECORD_FIELD_MODIFICATION_TIME);
936 record->mtime = (mtime_field != NULL) ?
937 pws3_field_get_time(mtime_field) : (time_t)0;
837 938
838 title_field = pws3_record_get_field(pws3_record, 939 title_field = pws3_record_get_field(pws3_record,
839 PWS3_RECORD_FIELD_TITLE); 940 PWS3_RECORD_FIELD_TITLE);
840 record->title = (title_field != NULL) ? 941 record->title = (title_field != NULL) ?
841 xstrdup(pws3_field_get_text(title_field)) : NULL; 942 xstrdup(pws3_field_get_text(title_field)) : NULL;