1 #define _POSIX_C_SOURCE 200809L
21 char *path; /* path to package file */
25 time_t installed; /* install time, 0 for not installed */
35 char *hash; /* could be fixed length */
37 struct zpm_file *next; /* so you can make a linked list */
41 /* associate with a package ? if only one? first? */
42 int zpm_open(struct zpm *pkg, char *path);
43 int zpm_pkgname(char *base, char *version, int release); /* construct a package file name */
45 /* flags for preserving mode, owner, etc */
46 /* puts hash of import in hash */
47 /* path can be a hash, with an "INTERNAL" flag, i.e. internally import */
51 #define ZPM_INTERNAL 0x8
52 #define ZPM_NOBLOB 0x10
53 /* don't run scripts on install */
54 #define ZPM_NOSCRIPTS 0x10
55 /* don't associate the file with a package, just do a raw insert */
56 /* otherwise, associate it with the current package */
57 #define ZPM_NOPACKAGE 0x20
59 int zpm_import(struct zpm *zp, char *path, uint32_t flags, uint8_t *hash);
61 /* link and unlink hashes to packages */
62 int zpm_link(struct zpm *pkg, char *path, char *hash, struct zpm_file *fileinfo);
63 int zpm_unlink(struct zpm *pkg, char *path);
65 /* tag a file. relative to "current package" */
66 int zpm_tag(struct zpm *zp, char *path, char *tags);
67 /* should this be broken up into separage functions ? */
68 int zpm_md(struct zpm *zp, char *path, int mode, char *owner, char *group, time_t mtime);
70 /* export hash to dest */
71 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode);
73 /* export path to dest */
74 int zpm_export(struct zpm *zp, char *path, uint32_t flags, char *dest);
76 int zpm_close(struct zpm *zp);
78 /* attach a signature to a package */
79 int zpm_sign(struct zpm *z, size_t s, void *signature);
81 /* set the package info to the nth package, -1 to return count? */
82 /* further import/exports and such will be relative to this package */
83 int zpm_package(struct zpm *zp, int n);
85 /* get file information */
86 int zpm_stat(struct zpm *z, struct zpm_file *f, int n);
88 /* will also set the package context to the new package */
89 int zpm_newpkg(struct zpm *z, char *base, char *version, int release);
92 int zpm_begin(struct zpm *z);
93 int zpm_commit(struct zpm *z);
94 int zpm_rollback(struct zpm *z);
96 /* higher level operations */
98 /* install or uninstall the package */
99 /* flag for keeping the blobs in local */
100 /* what about tag filtering */
101 int zpm_install(struct zpm *z, struct zpm *local, uint32_t flags);
102 int zpm_uninstall(struct zpm *local);
104 /* slurp up the actual blobs */
105 /* what about versioning them if they change */
106 int zpm_preserve(struct zpm *local);
108 /* check file integrity */
109 int zpm_checkinstall(struct zpm *local);
111 int zpm_merge(struct zpm *z, struct zpm *src, uint32_t flags);
113 void uncompresslzma(void *buf, size_t bufsize, FILE *out);
114 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
117 static char *dupstr(char *s) {
130 int zpm_newpkg(struct zpm *z, char *base, char *version, int release) {
131 char *sql = "insert or ignore into packages (package,version,release) values (?,?,?)";
135 rc = sqlite3_prepare(db, sql, -1, &ifile,0);
136 if (rc != SQLITE_OK) {
137 SQLERROR(sqlite3_errmsg(db));
140 rc = sqlite3_bind_text(ifile, 1, base, strlen(base), SQLITE_STATIC);
141 if (rc != SQLITE_OK) {
142 SQLERROR(sqlite3_errmsg(db));
143 fprintf(stderr, "cant bind package name\n");
147 sqlite3_bind_text(ifile, 2, version, strlen(version), SQLITE_STATIC);
148 sqlite3_bind_int(ifile, 3, release)
150 rc = sqlite3_step(ifile);
152 if (rc != SQLITE_DONE) {
153 SQLERROR(sqlite3_errmsg(db));
154 sqlite3_finalize(ifile);
157 sqlite3_finalize(ifile);
158 z->pkg = dupstr(base);
159 z->version = dupstr(version);
160 z->release = release;
164 int zpm_begin(struct zpm *z) {
166 sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
168 fprintf(stderr, "sqlite begin error: %s\n", errstr);
169 sqlite3_free(errstr);
175 int zpm_commit(struct zpm *z) {
177 sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
179 fprintf(stderr, "sqlite commit error: %s\n", errstr);
180 sqlite3_free(errstr);
186 int zpm_rollback(struct zpm *z) {
188 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
190 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
191 sqlite3_free(errstr);
197 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
203 case 1: sql = "pragma application_id = ?;"; break;
204 case 2: sql = "pragma user_version = ?;"; break;
205 default: return -1; break;
208 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
210 if (rc != SQLITE_OK) {
211 SQLERROR(sqlite3_errmsg(db->db));
215 sqlite3_bind_int(s, 1, value);
216 if (rc != SQLITE_OK) {
217 SQLERROR(sqlite3_errmsg(db->db));
218 fprintf(stderr, "cant bind pragma value\n");
222 rc = sqlite3_step(s);
223 if (rc != SQLITE_DONE) {
224 SQLERROR(sqlite3_errmsg(db->db));
225 fprintf(stderr, "cant set pragma\n");
234 int zpm_db_pragma(struct zpm *db, int pragma) {
241 case 1: sql = "pragma application_id;"; break;
242 case 2: sql = "pragma user_version;"; break;
243 default: return -1; break;
246 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
248 if (rc != SQLITE_OK) {
249 SQLERROR(sqlite3_errmsg(db->db));
250 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
251 /* TODO just abort? */
255 rc = sqlite3_step(s);
256 if (rc == SQLITE_ROW) {
257 value = sqlite3_column_int(s, 0);
267 int zpm_db_initialize(struct zpm *pkg) {
268 //fprintf(stderr, "initializing zpm database\n");
270 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
271 case SQLITE_OK: break;
273 SQLERROR(sqlite3_errmsg(pkg->db));
274 fprintf(stderr, "error: %s\n", error);
283 int zpm_open(struct zpm *pkg, char *path) {
296 rc = sqlite3_open(path, &db);
298 SQLERROR(sqlite3_errmsg(db));
303 pkg->path = dupstr(path);
305 appid = zpm_db_pragma(pkg, 1);
306 dbver = zpm_db_pragma(pkg, 2);
308 //fprintf(stderr, "db appid = %x, dbver = %d\n", appid, dbver);
310 case 0: if (!zpm_db_initialize(pkg)) {
315 case 0x5a504442: break;
317 fprintf(stderr, "unknown database type\n");
323 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
328 sqlite3_exec(pkg->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
330 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
331 sqlite3_free(errstr);
337 /* TODO if this is a new database, create structures */
339 /* get a package. what if more than one? what if none? */
343 int zpm_close(struct zpm *pkg) {
345 sqlite3_close(pkg->db);
348 /* TODO free any malloced names and such */
352 /* set package struct variables, database, environment, then command line */
353 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
360 ev = getenv("ZPMPACKAGE");
362 pkg->pkgname = dupstr(ev);
364 ev = getenv("ZPMPKGREL");
366 pkg->release = strtol(ev, 0, 0);
368 ev = getenv("ZPMPKGVER");
370 pkg->version = dupstr(ev);
373 /* now, parse the options, return optind so the caller can adjust if needed */
378 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
388 /* TODO check null */
389 sqlite3 *db = pkg->db;
391 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
392 if (rc != SQLITE_OK) {
393 SQLERROR(sqlite3_errmsg(db));
399 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
401 rc = sqlite3_step(ifile);
403 if (rc == SQLITE_DONE) {
404 /* didn't find a row */
405 sqlite3_finalize(ifile);
407 fprintf(stderr, "no such hash\n");
410 /* either way we're done with this now */
412 if (rc != SQLITE_ROW) {
413 SQLERROR(sqlite3_errmsg(db));
414 sqlite3_finalize(ifile);
419 type = sqlite3_column_type(ifile, 0);
420 if (type == SQLITE_NULL) {
421 fprintf(stderr, "no file size\n");
422 sqlite3_finalize(ifile);
426 type = sqlite3_column_type(ifile, 1);
427 if (type == SQLITE_NULL) {
428 fprintf(stderr, "no file data\n");
429 sqlite3_finalize(ifile);
433 //size = sqlite3_column_int64(ifile, 0);
434 xzdata = (void *)sqlite3_column_blob(ifile, 1);
435 blobsize = sqlite3_column_bytes(ifile, 1);
437 out = fopen(path, "w");
439 fprintf(stderr, "can't open output file %s\n", path);
440 sqlite3_finalize(ifile);
444 //fwrite(xzdata, blobsize, 1, stdout);
446 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
447 uncompresslzma(xzdata, blobsize, out);
450 sqlite3_finalize(ifile);
456 /* flags 0, close mmap, flags 1, return mmap fd */
457 int zpm_hash(char *path, char *hash, uint32_t flags) {
463 unsigned char tmp[32];
466 fd = open(path, O_RDONLY);
468 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
471 if (fstat(fd, &sbuf) == -1) {
472 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
475 /* not a regular file? */
476 if (!S_ISREG(sbuf.st_mode)) {
477 /* TODO this is ok, just stored differently */
478 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
482 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
485 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
491 sha256_process(&md, content, sbuf.st_size);
492 sha256_done(&md, tmp);
494 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
497 munmap(content, sbuf.st_size);
501 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
505 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
506 if (rc != SQLITE_OK) {
507 SQLERROR(sqlite3_errmsg(db));
513 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
518 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
519 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
528 /* go ahead and set up elf information now */
529 /* clear existing for this hash */
530 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
532 rc = sqlite3_step(ifile);
534 if (rc == SQLITE_ROW) {
536 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
537 nc = sqlite3_column_count(ifile);
538 for (i = 0; i < nc; i++) {
540 r = sqlite3_column_text(ifile, i);
541 fprintf(stderr, ", %s", r);
543 fprintf(stderr, "\n");
546 } while (rc == SQLITE_ROW);
547 if (rc != SQLITE_DONE) {
548 SQLERROR(sqlite3_errmsg(db));
549 sqlite3_finalize(ifile);
550 fprintf(stderr, "error clearing elf info: %d\n", rc);
553 sqlite3_finalize(ifile);
554 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
556 rc = sqlite3_step(ifile);
557 } while (rc == SQLITE_ROW);
558 if (rc != SQLITE_DONE) {
559 SQLERROR(sqlite3_errmsg(db));
560 sqlite3_finalize(ifile);
561 fprintf(stderr, "error clearing elf library: %d\n", rc);
564 sqlite3_finalize(ifile);
565 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
567 rc = sqlite3_step(ifile);
568 } while (rc == SQLITE_ROW);
569 if (rc != SQLITE_DONE) {
570 SQLERROR(sqlite3_errmsg(db));
571 sqlite3_finalize(ifile);
572 fprintf(stderr, "error clearing elf needed\n");
575 sqlite3_finalize(ifile);
577 hdr = libelf_header(content);
578 /* if lib, set soname */
579 if (libelf_type(content) == ET_DYN) {
581 Elf64_Shdr *shdr, *dynsect, *dynstrtab = 0;
583 elf = (char *)content;
584 for (i = 0; i < hdr->e_shnum; i++) {
585 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
586 if (shdr->sh_type == SHT_DYNAMIC) {
588 } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
601 name = elf + dynstrtab->sh_offset;
602 for (i = 0; i < hdr->e_shnum; i++) {
603 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
604 if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
614 dynname = elf + dyntab->sh_offset;
616 for (dent = (Elf64_Dyn *)(elf + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
617 if (dent->d_tag == DT_SONAME) {
618 char *soname = dynname + dent->d_un.d_val;
619 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
620 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
621 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
622 rc = sqlite3_step(ifile);
623 if (rc != SQLITE_DONE) {
624 SQLERROR(sqlite3_errmsg(db));
625 sqlite3_finalize(ifile);
626 fprintf(stderr, "error setting library soname\n");
629 sqlite3_finalize(ifile);
634 /* if exe, set neededs */
635 if (libelf_type(content) == ET_EXEC) {
639 elf = (char *)content;
640 /* find program header table */
641 for (i = 0; i < hdr->e_phnum; i++) {
642 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
643 if (phdr->p_type == PT_DYNAMIC) {
644 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
647 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
651 dyn = (Elf64_Dyn *)dsect;
653 dsect = libelf_section(elf, SHT_DYNAMIC);
656 strsect = libelf_section_n(elf, dsect->sh_link);
657 strtab = elf + strsect->sh_offset;
659 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
660 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
661 while (dyn->d_tag != DT_NULL) {
662 if (dyn->d_tag == DT_NEEDED) {
666 need = strtab + dyn->d_un.d_val;
667 if (strlen(need) == 0) continue;
668 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
669 fprintf(stderr, "%s needs %s\n", hash, need);
670 rc = sqlite3_step(ifile);
671 if (rc != SQLITE_DONE) {
672 SQLERROR(sqlite3_errmsg(db));
673 sqlite3_finalize(ifile);
674 fprintf(stderr, "error setting needed library\n");
677 sqlite3_reset(ifile);
681 sqlite3_finalize(ifile);
688 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
692 unsigned char tmp[32];
695 int haverow = 0,havedata = 0;
703 if (!pkg || !pkg->db || !path) {
707 /* use local if caller didn't pass in room */
713 fd = open(path, O_RDONLY);
716 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
719 if (fstat(fd, &sbuf) == -1) {
721 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
724 /* not a regular file? */
725 if (!S_ISREG(sbuf.st_mode)) {
727 switch (sbuf.st_mode & S_IFMT) {
728 case S_IFSOCK: ftype = "socket"; break;
729 case S_IFLNK : ftype = "symlink"; break;
730 case S_IFBLK : ftype = "block device"; break;
731 case S_IFDIR : ftype = "directory"; break;
732 case S_IFCHR : ftype = "character device"; break;
733 case S_IFIFO : ftype = "fifo"; break;
734 default: ftype = "unknown file type"; break;
736 /* TODO this is ok, just stored differently */
737 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
742 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
746 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
752 sha256_process(&md, content, sbuf.st_size);
753 sha256_done(&md, tmp);
755 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
758 //fprintf(stderr, "file %s: %s\n", path, hash);
760 /* TODO check null */
761 sqlite3 *db = pkg->db;
763 /* prepare and bind */
765 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
766 if (rc != SQLITE_OK) {
767 SQLERROR(sqlite3_errmsg(db));
768 munmap(content, sbuf.st_size);
774 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
776 rc = sqlite3_step(ifile);
778 if (rc != SQLITE_DONE) {
779 if (rc != SQLITE_ROW) {
780 /* didn't find a row */
781 SQLERROR(sqlite3_errmsg(db));
783 munmap(content, sbuf.st_size);
787 // fprintf(stderr, "have row for hash\n");
788 type = sqlite3_column_type(ifile, 0);
789 if (type == SQLITE_NULL) {
790 /* TODO assert, this shouldn't be possible? */
791 fprintf(stderr, "no file size\n");
792 sqlite3_finalize(ifile);
793 munmap(content, sbuf.st_size);
796 type = sqlite3_column_type(ifile, 1);
797 if (type == SQLITE_NULL) {
798 /* TODO assert, this shouldn't be possible? */
799 fprintf(stderr, "no file data\n");
800 sqlite3_finalize(ifile);
801 munmap(content, sbuf.st_size);
803 /* which is fine, just need to update the row then */
805 havedata = sqlite3_column_int(ifile, 1);
808 sqlite3_finalize(ifile);
812 outbuf = compresslzma(content, sbuf.st_size, &outlen);
814 fprintf(stderr, "compresslzma failed\n");
815 munmap(content, sbuf.st_size);
818 //fprintf(stderr, "compressed to %zu\n", outlen);
820 /* start a transaction */
821 // do that outside of here
826 //fprintf(stderr, "adding file data\n");
827 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
829 //fprintf(stderr, "creating new data row\n");
830 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
832 if (rc != SQLITE_OK) {
833 SQLERROR(sqlite3_errmsg(db));
834 fprintf(stderr, "cant prepare data\n");
836 munmap(content, sbuf.st_size);
840 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
841 if (rc != SQLITE_OK) {
842 SQLERROR(sqlite3_errmsg(db));
843 fprintf(stderr, "cant bind size\n");
845 munmap(content, sbuf.st_size);
848 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
849 if (rc != SQLITE_OK) {
850 SQLERROR(sqlite3_errmsg(db));
851 fprintf(stderr, "cant bind content\n");
853 munmap(content, sbuf.st_size);
856 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
857 if (rc != SQLITE_OK) {
858 SQLERROR(sqlite3_errmsg(db));
859 fprintf(stderr, "cant bind hash\n");
861 munmap(content, sbuf.st_size);
864 rc = sqlite3_step(ifile);
865 if (rc != SQLITE_DONE) {
866 SQLERROR(sqlite3_errmsg(db));
867 sqlite3_finalize(ifile);
869 munmap(content, sbuf.st_size);
872 sqlite3_finalize(ifile);
877 /* don't need the original file now */
881 if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
882 fprintf(stderr, "setting elf info failed\n");
883 munmap(content, sbuf.st_size);
887 munmap(content, sbuf.st_size);
889 /* if package and not nopackage flag, add to package */
890 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
900 int main(int ac, char **av){
915 fprintf(stderr, "usage: db hash file\n");
919 rc = sqlite3_open(av[1], &db);
921 SQLERROR(sqlite3_errmsg(db));
930 Packages are sqlite databases
932 get application id and userver
934 Primitive operations:
937 associate path with package
938 associate blob with path?
940 * extract blob to a path
941 compare blob to filesystem path
942 create package with info
946 record elf information about blob
949 sign a package? What are we verifying?