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);
455 sqlite3_finalize(ifile);
460 /* flags 0, close mmap, flags 1, return mmap fd */
461 int zpm_hash(char *path, char *hash, uint32_t flags) {
467 unsigned char tmp[32];
470 fd = open(path, O_RDONLY);
472 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
475 if (fstat(fd, &sbuf) == -1) {
476 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
479 /* not a regular file? */
480 if (!S_ISREG(sbuf.st_mode)) {
481 /* TODO this is ok, just stored differently */
482 fprintf(stderr, "%s non-regular files unsupported %s\n", __FUNCTION__, path);
486 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
489 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
495 sha256_process(&md, content, sbuf.st_size);
496 sha256_done(&md, tmp);
498 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
501 munmap(content, sbuf.st_size);
502 return flags ? fd : 1;
505 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
509 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
510 if (rc != SQLITE_OK) {
511 SQLERROR(sqlite3_errmsg(db));
517 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
522 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
523 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
532 /* go ahead and set up elf information now */
533 /* clear existing for this hash */
534 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
536 rc = sqlite3_step(ifile);
538 if (rc == SQLITE_ROW) {
540 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
541 nc = sqlite3_column_count(ifile);
542 for (i = 0; i < nc; i++) {
544 r = sqlite3_column_text(ifile, i);
545 fprintf(stderr, ", %s", r);
547 fprintf(stderr, "\n");
550 } while (rc == SQLITE_ROW);
551 if (rc != SQLITE_DONE) {
552 SQLERROR(sqlite3_errmsg(db));
553 sqlite3_finalize(ifile);
554 fprintf(stderr, "error clearing elf info: %d\n", rc);
557 sqlite3_finalize(ifile);
558 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
560 rc = sqlite3_step(ifile);
561 } while (rc == SQLITE_ROW);
562 if (rc != SQLITE_DONE) {
563 SQLERROR(sqlite3_errmsg(db));
564 sqlite3_finalize(ifile);
565 fprintf(stderr, "error clearing elf library: %d\n", rc);
568 sqlite3_finalize(ifile);
569 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
571 rc = sqlite3_step(ifile);
572 } while (rc == SQLITE_ROW);
573 if (rc != SQLITE_DONE) {
574 SQLERROR(sqlite3_errmsg(db));
575 sqlite3_finalize(ifile);
576 fprintf(stderr, "error clearing elf needed\n");
579 sqlite3_finalize(ifile);
581 hdr = libelf_header(content);
582 /* if lib, set soname */
583 if (libelf_type(content) == ET_DYN) {
585 Elf64_Shdr *shdr, *dynsect, *dynstrtab = 0;
587 elf = (char *)content;
588 for (i = 0; i < hdr->e_shnum; i++) {
589 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
590 if (shdr->sh_type == SHT_DYNAMIC) {
592 } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
605 name = elf + dynstrtab->sh_offset;
606 for (i = 0; i < hdr->e_shnum; i++) {
607 shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
608 if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
618 dynname = elf + dyntab->sh_offset;
620 for (dent = (Elf64_Dyn *)(elf + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
621 if (dent->d_tag == DT_SONAME) {
622 char *soname = dynname + dent->d_un.d_val;
623 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
624 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
625 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
626 rc = sqlite3_step(ifile);
627 if (rc != SQLITE_DONE) {
628 SQLERROR(sqlite3_errmsg(db));
629 sqlite3_finalize(ifile);
630 fprintf(stderr, "error setting library soname\n");
633 sqlite3_finalize(ifile);
638 /* if exe, set neededs */
639 if (libelf_type(content) == ET_EXEC) {
643 elf = (char *)content;
644 /* find program header table */
645 for (i = 0; i < hdr->e_phnum; i++) {
646 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
647 if (phdr->p_type == PT_DYNAMIC) {
648 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
651 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
655 dyn = (Elf64_Dyn *)dsect;
657 dsect = libelf_section(elf, SHT_DYNAMIC);
660 strsect = libelf_section_n(elf, dsect->sh_link);
661 strtab = elf + strsect->sh_offset;
663 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
664 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
665 while (dyn->d_tag != DT_NULL) {
666 if (dyn->d_tag == DT_NEEDED) {
670 need = strtab + dyn->d_un.d_val;
671 if (strlen(need) == 0) continue;
672 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
673 fprintf(stderr, "%s needs %s\n", hash, need);
674 rc = sqlite3_step(ifile);
675 if (rc != SQLITE_DONE) {
676 SQLERROR(sqlite3_errmsg(db));
677 sqlite3_finalize(ifile);
678 fprintf(stderr, "error setting needed library\n");
681 sqlite3_reset(ifile);
685 sqlite3_finalize(ifile);
692 int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
696 unsigned char tmp[32];
699 int haverow = 0,havedata = 0;
707 if (!pkg || !pkg->db || !path) {
711 /* use local if caller didn't pass in room */
716 flags = 0; /* suppress warning, probably use to follow symlinks */
718 fd = open(path, O_RDONLY);
721 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
724 if (fstat(fd, &sbuf) == -1) {
726 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
729 /* not a regular file? */
730 if (!S_ISREG(sbuf.st_mode)) {
732 switch (sbuf.st_mode & S_IFMT) {
733 case S_IFSOCK: ftype = "socket"; break;
734 case S_IFLNK : ftype = "symlink"; break;
735 case S_IFBLK : ftype = "block device"; break;
736 case S_IFDIR : ftype = "directory"; break;
737 case S_IFCHR : ftype = "character device"; break;
738 case S_IFIFO : ftype = "fifo"; break;
739 default: ftype = "unknown file type"; break;
741 /* TODO this is ok, just stored differently */
742 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
747 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
751 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
757 sha256_process(&md, content, sbuf.st_size);
758 sha256_done(&md, tmp);
760 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
763 //fprintf(stderr, "file %s: %s\n", path, hash);
765 /* TODO check null */
766 sqlite3 *db = pkg->db;
768 /* prepare and bind */
770 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
771 if (rc != SQLITE_OK) {
772 SQLERROR(sqlite3_errmsg(db));
773 munmap(content, sbuf.st_size);
779 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
781 rc = sqlite3_step(ifile);
783 if (rc != SQLITE_DONE) {
784 if (rc != SQLITE_ROW) {
785 /* didn't find a row */
786 SQLERROR(sqlite3_errmsg(db));
788 munmap(content, sbuf.st_size);
792 // fprintf(stderr, "have row for hash\n");
793 type = sqlite3_column_type(ifile, 0);
794 if (type == SQLITE_NULL) {
795 /* TODO assert, this shouldn't be possible? */
796 fprintf(stderr, "no file size\n");
797 sqlite3_finalize(ifile);
798 munmap(content, sbuf.st_size);
801 type = sqlite3_column_type(ifile, 1);
802 if (type == SQLITE_NULL) {
803 /* TODO assert, this shouldn't be possible? */
804 fprintf(stderr, "no file data\n");
805 sqlite3_finalize(ifile);
806 munmap(content, sbuf.st_size);
808 /* which is fine, just need to update the row then */
810 havedata = sqlite3_column_int(ifile, 1);
813 sqlite3_finalize(ifile);
817 outbuf = compresslzma(content, sbuf.st_size, &outlen);
819 fprintf(stderr, "compresslzma failed\n");
820 munmap(content, sbuf.st_size);
823 //fprintf(stderr, "compressed to %zu\n", outlen);
825 /* start a transaction */
826 // do that outside of here
831 //fprintf(stderr, "adding file data\n");
832 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
834 //fprintf(stderr, "creating new data row\n");
835 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
837 if (rc != SQLITE_OK) {
838 SQLERROR(sqlite3_errmsg(db));
839 fprintf(stderr, "cant prepare data\n");
841 munmap(content, sbuf.st_size);
845 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
846 if (rc != SQLITE_OK) {
847 SQLERROR(sqlite3_errmsg(db));
848 fprintf(stderr, "cant bind size\n");
850 munmap(content, sbuf.st_size);
853 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
854 if (rc != SQLITE_OK) {
855 SQLERROR(sqlite3_errmsg(db));
856 fprintf(stderr, "cant bind content\n");
858 munmap(content, sbuf.st_size);
861 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
862 if (rc != SQLITE_OK) {
863 SQLERROR(sqlite3_errmsg(db));
864 fprintf(stderr, "cant bind hash\n");
866 munmap(content, sbuf.st_size);
869 rc = sqlite3_step(ifile);
870 if (rc != SQLITE_DONE) {
871 SQLERROR(sqlite3_errmsg(db));
872 sqlite3_finalize(ifile);
874 munmap(content, sbuf.st_size);
877 sqlite3_finalize(ifile);
882 /* don't need the original file now */
886 if (!set_elf_info(pkg->db, hash, content, sbuf.st_size)) {
887 fprintf(stderr, "setting elf info failed\n");
888 munmap(content, sbuf.st_size);
892 munmap(content, sbuf.st_size);
894 /* if package and not nopackage flag, add to package */
895 if (pkg->pkgname && (!ZPM_NOPACKAGE)) {
905 int main(int ac, char **av){
920 fprintf(stderr, "usage: db hash file\n");
924 rc = sqlite3_open(av[1], &db);
926 SQLERROR(sqlite3_errmsg(db));
935 Packages are sqlite databases
937 get application id and userver
939 Primitive operations:
942 associate path with package
943 associate blob with path?
945 * extract blob to a path
946 compare blob to filesystem path
947 create package with info
951 record elf information about blob
954 sign a package? What are we verifying?