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 if (strcmp(path, "-")) {
438 out = fopen(path, "w");
443 fprintf(stderr, "can't open output file %s\n", path);
444 sqlite3_finalize(ifile);
448 //fwrite(xzdata, blobsize, 1, stdout);
450 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
451 uncompresslzma(xzdata, blobsize, out);
454 sqlite3_finalize(ifile);
459 /* flags 0, close mmap, flags 1, return mmap fd */
460 int zpm_hash(char *path, char *hash, uint32_t flags) {
466 unsigned char tmp[32];
469 fd = open(path, O_RDONLY);
471 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
474 if (fstat(fd, &sbuf) == -1) {
475 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
478 /* not a regular file? */
479 if (!S_ISREG(sbuf.st_mode)) {
480 /* TODO this is ok, just stored differently */
481 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
485 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
488 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
494 sha256_process(&md, content, sbuf.st_size);
495 sha256_done(&md, tmp);
497 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
500 munmap(content, sbuf.st_size);
504 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
508 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
509 if (rc != SQLITE_OK) {
510 SQLERROR(sqlite3_errmsg(db));
516 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
521 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
522 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
531 /* go ahead and set up elf information now */
532 /* clear existing for this hash */
533 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
535 rc = sqlite3_step(ifile);
537 if (rc == SQLITE_ROW) {
539 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
540 nc = sqlite3_column_count(ifile);
541 for (i = 0; i < nc; i++) {
543 r = sqlite3_column_text(ifile, i);
544 fprintf(stderr, ", %s", r);
546 fprintf(stderr, "\n");
549 } while (rc == SQLITE_ROW);
550 if (rc != SQLITE_DONE) {
551 SQLERROR(sqlite3_errmsg(db));
552 sqlite3_finalize(ifile);
553 fprintf(stderr, "error clearing elf info: %d\n", rc);
556 sqlite3_finalize(ifile);
557 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
559 rc = sqlite3_step(ifile);
560 } while (rc == SQLITE_ROW);
561 if (rc != SQLITE_DONE) {
562 SQLERROR(sqlite3_errmsg(db));
563 sqlite3_finalize(ifile);
564 fprintf(stderr, "error clearing elf library: %d\n", rc);
567 sqlite3_finalize(ifile);
568 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
570 rc = sqlite3_step(ifile);
571 } while (rc == SQLITE_ROW);
572 if (rc != SQLITE_DONE) {
573 SQLERROR(sqlite3_errmsg(db));
574 sqlite3_finalize(ifile);
575 fprintf(stderr, "error clearing elf needed\n");
578 sqlite3_finalize(ifile);
580 hdr = libelf_header(content);
581 /* if lib, set soname */
582 if (libelf_type(content) == ET_DYN) {
584 Elf64_Shdr *shdr, *dynsect, *dynstrtab = 0;
586 elf = (char *)content;
587 for (i = 0; i < hdr->e_shnum; i++) {
588 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
589 if (shdr->sh_type == SHT_DYNAMIC) {
591 } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
604 name = elf + dynstrtab->sh_offset;
605 for (i = 0; i < hdr->e_shnum; i++) {
606 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
607 if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
617 dynname = elf + dyntab->sh_offset;
619 for (dent = (Elf64_Dyn *)(elf + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
620 if (dent->d_tag == DT_SONAME) {
621 char *soname = dynname + dent->d_un.d_val;
622 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
623 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
624 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
625 rc = sqlite3_step(ifile);
626 if (rc != SQLITE_DONE) {
627 SQLERROR(sqlite3_errmsg(db));
628 sqlite3_finalize(ifile);
629 fprintf(stderr, "error setting library soname\n");
632 sqlite3_finalize(ifile);
637 /* if exe, set neededs */
638 if (libelf_type(content) == ET_EXEC) {
642 elf = (char *)content;
643 /* find program header table */
644 for (i = 0; i < hdr->e_phnum; i++) {
645 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
646 if (phdr->p_type == PT_DYNAMIC) {
647 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
650 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
654 dyn = (Elf64_Dyn *)dsect;
656 dsect = libelf_section(elf, SHT_DYNAMIC);
659 strsect = libelf_section_n(elf, dsect->sh_link);
660 strtab = elf + strsect->sh_offset;
662 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
663 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
664 while (dyn->d_tag != DT_NULL) {
665 if (dyn->d_tag == DT_NEEDED) {
669 need = strtab + dyn->d_un.d_val;
670 if (strlen(need) == 0) continue;
671 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
672 fprintf(stderr, "%s needs %s\n", hash, need);
673 rc = sqlite3_step(ifile);
674 if (rc != SQLITE_DONE) {
675 SQLERROR(sqlite3_errmsg(db));
676 sqlite3_finalize(ifile);
677 fprintf(stderr, "error setting needed library\n");
680 sqlite3_reset(ifile);
684 sqlite3_finalize(ifile);
691 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
695 unsigned char tmp[32];
698 int haverow = 0,havedata = 0;
706 if (!pkg || !pkg->db || !path) {
710 /* use local if caller didn't pass in room */
716 fd = open(path, O_RDONLY);
719 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
722 if (fstat(fd, &sbuf) == -1) {
724 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
727 /* not a regular file? */
728 if (!S_ISREG(sbuf.st_mode)) {
730 switch (sbuf.st_mode & S_IFMT) {
731 case S_IFSOCK: ftype = "socket"; break;
732 case S_IFLNK : ftype = "symlink"; break;
733 case S_IFBLK : ftype = "block device"; break;
734 case S_IFDIR : ftype = "directory"; break;
735 case S_IFCHR : ftype = "character device"; break;
736 case S_IFIFO : ftype = "fifo"; break;
737 default: ftype = "unknown file type"; break;
739 /* TODO this is ok, just stored differently */
740 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
745 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
749 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
755 sha256_process(&md, content, sbuf.st_size);
756 sha256_done(&md, tmp);
758 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
761 //fprintf(stderr, "file %s: %s\n", path, hash);
763 /* TODO check null */
764 sqlite3 *db = pkg->db;
766 /* prepare and bind */
768 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
769 if (rc != SQLITE_OK) {
770 SQLERROR(sqlite3_errmsg(db));
771 munmap(content, sbuf.st_size);
777 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
779 rc = sqlite3_step(ifile);
781 if (rc != SQLITE_DONE) {
782 if (rc != SQLITE_ROW) {
783 /* didn't find a row */
784 SQLERROR(sqlite3_errmsg(db));
786 munmap(content, sbuf.st_size);
790 // fprintf(stderr, "have row for hash\n");
791 type = sqlite3_column_type(ifile, 0);
792 if (type == SQLITE_NULL) {
793 /* TODO assert, this shouldn't be possible? */
794 fprintf(stderr, "no file size\n");
795 sqlite3_finalize(ifile);
796 munmap(content, sbuf.st_size);
799 type = sqlite3_column_type(ifile, 1);
800 if (type == SQLITE_NULL) {
801 /* TODO assert, this shouldn't be possible? */
802 fprintf(stderr, "no file data\n");
803 sqlite3_finalize(ifile);
804 munmap(content, sbuf.st_size);
806 /* which is fine, just need to update the row then */
808 havedata = sqlite3_column_int(ifile, 1);
811 sqlite3_finalize(ifile);
815 outbuf = compresslzma(content, sbuf.st_size, &outlen);
817 fprintf(stderr, "compresslzma failed\n");
818 munmap(content, sbuf.st_size);
821 //fprintf(stderr, "compressed to %zu\n", outlen);
823 /* start a transaction */
824 // do that outside of here
829 //fprintf(stderr, "adding file data\n");
830 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
832 //fprintf(stderr, "creating new data row\n");
833 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
835 if (rc != SQLITE_OK) {
836 SQLERROR(sqlite3_errmsg(db));
837 fprintf(stderr, "cant prepare data\n");
839 munmap(content, sbuf.st_size);
843 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
844 if (rc != SQLITE_OK) {
845 SQLERROR(sqlite3_errmsg(db));
846 fprintf(stderr, "cant bind size\n");
848 munmap(content, sbuf.st_size);
851 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
852 if (rc != SQLITE_OK) {
853 SQLERROR(sqlite3_errmsg(db));
854 fprintf(stderr, "cant bind content\n");
856 munmap(content, sbuf.st_size);
859 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
860 if (rc != SQLITE_OK) {
861 SQLERROR(sqlite3_errmsg(db));
862 fprintf(stderr, "cant bind hash\n");
864 munmap(content, sbuf.st_size);
867 rc = sqlite3_step(ifile);
868 if (rc != SQLITE_DONE) {
869 SQLERROR(sqlite3_errmsg(db));
870 sqlite3_finalize(ifile);
872 munmap(content, sbuf.st_size);
875 sqlite3_finalize(ifile);
880 /* don't need the original file now */
884 if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
885 fprintf(stderr, "setting elf info failed\n");
886 munmap(content, sbuf.st_size);
890 munmap(content, sbuf.st_size);
892 /* if package and not nopackage flag, add to package */
893 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
903 int main(int ac, char **av){
918 fprintf(stderr, "usage: db hash file\n");
922 rc = sqlite3_open(av[1], &db);
924 SQLERROR(sqlite3_errmsg(db));
933 Packages are sqlite databases
935 get application id and userver
937 Primitive operations:
940 associate path with package
941 associate blob with path?
943 * extract blob to a path
944 compare blob to filesystem path
945 create package with info
949 record elf information about blob
952 sign a package? What are we verifying?