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 /* wrapper for sqlite3_exec */
187 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
188 return sqlite3_exec(z->db, sql, callback, arg, errmsg);
191 int zpm_rollback(struct zpm *z) {
193 sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
195 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
196 sqlite3_free(errstr);
202 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
208 case 1: sql = "pragma application_id = ?;"; break;
209 case 2: sql = "pragma user_version = ?;"; break;
210 default: return -1; break;
213 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
215 if (rc != SQLITE_OK) {
216 SQLERROR(sqlite3_errmsg(db->db));
220 sqlite3_bind_int(s, 1, value);
221 if (rc != SQLITE_OK) {
222 SQLERROR(sqlite3_errmsg(db->db));
223 fprintf(stderr, "cant bind pragma value\n");
227 rc = sqlite3_step(s);
228 if (rc != SQLITE_DONE) {
229 SQLERROR(sqlite3_errmsg(db->db));
230 fprintf(stderr, "cant set pragma\n");
239 int zpm_db_pragma(struct zpm *db, int pragma) {
246 case 1: sql = "pragma application_id;"; break;
247 case 2: sql = "pragma user_version;"; break;
248 default: return -1; break;
251 rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
253 if (rc != SQLITE_OK) {
254 SQLERROR(sqlite3_errmsg(db->db));
255 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
256 /* TODO just abort? */
260 rc = sqlite3_step(s);
261 if (rc == SQLITE_ROW) {
262 value = sqlite3_column_int(s, 0);
272 int zpm_db_initialize(struct zpm *pkg) {
273 //fprintf(stderr, "initializing zpm database\n");
275 switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
276 case SQLITE_OK: break;
278 SQLERROR(sqlite3_errmsg(pkg->db));
279 fprintf(stderr, "error: %s\n", error);
288 /* assumes pkg->db is set */
289 static int setupdb(struct zpm *zpm) {
295 appid = zpm_db_pragma(zpm, 1);
296 dbver = zpm_db_pragma(zpm, 2);
298 if (appid != 0x5a504442) {
299 fprintf(stderr, "unknown database type\n");
305 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
310 sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
314 zpm->errmsg = strdup(errstr);
315 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
316 sqlite3_free(errstr);
321 /* TODO add vercmp */
326 struct zpm *zpm_clearmem(struct zpm *zpm) {
328 zpm = malloc(sizeof *zpm);
332 *zpm = (struct zpm){0};
338 int zpm_init(struct zpm *pkg, char *path) {
345 rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
347 SQLERROR(sqlite3_errmsg(db));
352 pkg->path = strdup(path);
354 appid = zpm_db_pragma(pkg, 1);
357 case 0: if (!zpm_db_initialize(pkg)) {
363 /* already initialized */
366 fprintf(stderr, "unknown database type\n");
381 int zpm_open(struct zpm *zpm, char *path) {
387 rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
389 SQLERROR(sqlite3_errmsg(db));
392 fprintf(stderr, "path = %s\n", path);
396 zpm->path = strdup(path);
408 int zpm_close(struct zpm *pkg) {
410 sqlite3_close(pkg->db);
413 /* TODO free any malloced names and such */
417 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
418 int lenb, const void *b) {
419 /* not sure what the ints are, possibly string lengths */
420 if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
422 if (lena == 0 && lenb > 0) return 1;
423 return zpm_vercmp(a, b);
426 int zpm_addvercmp(struct zpm *pkg) {
427 return sqlite3_create_collation(
428 pkg->db, "vercmp", SQLITE_UTF8, NULL,
433 /* set package struct variables, database, environment, then command line */
434 int zpm_readopts(struct zpm *zpm, int ac, char **av) {
436 struct zpm_package *pkg;
442 pkg = zpm->current_package;
445 ev = getenv("ZPMPACKAGE");
447 pkg->name = dupstr(ev);
449 ev = getenv("ZPMPKGREL");
451 pkg->release = strtol(ev, 0, 0);
453 ev = getenv("ZPMPKGVER");
455 pkg->version = dupstr(ev);
459 /* now, parse the options, return optind so the caller can adjust if needed */
464 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
474 /* TODO check null */
475 sqlite3 *db = pkg->db;
477 rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
478 if (rc != SQLITE_OK) {
479 SQLERROR(sqlite3_errmsg(db));
485 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
487 rc = sqlite3_step(ifile);
489 if (rc == SQLITE_DONE) {
490 /* didn't find a row */
491 sqlite3_finalize(ifile);
493 fprintf(stderr, "no such hash: %s\n", hash);
496 /* either way we're done with this now */
498 if (rc != SQLITE_ROW) {
499 SQLERROR(sqlite3_errmsg(db));
500 sqlite3_finalize(ifile);
505 type = sqlite3_column_type(ifile, 0);
506 if (type == SQLITE_NULL) {
507 fprintf(stderr, "no file size\n");
508 sqlite3_finalize(ifile);
512 type = sqlite3_column_type(ifile, 1);
513 if (type == SQLITE_NULL) {
514 fprintf(stderr, "no file data\n");
515 sqlite3_finalize(ifile);
519 //size = sqlite3_column_int64(ifile, 0);
520 xzdata = (void *)sqlite3_column_blob(ifile, 1);
521 blobsize = sqlite3_column_bytes(ifile, 1);
523 if (strcmp(path, "-")) {
524 out = fopen(path, "w");
529 fprintf(stderr, "can't open output file %s\n", path);
530 sqlite3_finalize(ifile);
534 //fwrite(xzdata, blobsize, 1, stdout);
536 //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
537 uncompresslzma(xzdata, blobsize, out);
541 sqlite3_finalize(ifile);
546 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
550 rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
551 if (rc != SQLITE_OK) {
552 SQLERROR(sqlite3_errmsg(db));
558 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
563 static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
564 if (length >= sizeof (Elf64_Ehdr) && libelf_iself(content)) {
573 /* go ahead and set up elf information now */
574 /* clear existing for this hash */
575 ifile = run_for_hash(db, "delete from elfinfo where file = ?", hash);
577 rc = sqlite3_step(ifile);
579 if (rc == SQLITE_ROW) {
581 fprintf(stderr, "delete row has %d columns: ", sqlite3_column_count(ifile));
582 nc = sqlite3_column_count(ifile);
583 for (i = 0; i < nc; i++) {
585 r = sqlite3_column_text(ifile, i);
586 fprintf(stderr, ", %s", r);
588 fprintf(stderr, "\n");
591 } while (rc == SQLITE_ROW);
592 if (rc != SQLITE_DONE) {
593 SQLERROR(sqlite3_errmsg(db));
594 sqlite3_finalize(ifile);
595 fprintf(stderr, "error clearing elf info: %d\n", rc);
598 sqlite3_finalize(ifile);
599 ifile = run_for_hash(db, "delete from elflibraries where file = ?", hash);
601 rc = sqlite3_step(ifile);
602 } while (rc == SQLITE_ROW);
603 if (rc != SQLITE_DONE) {
604 SQLERROR(sqlite3_errmsg(db));
605 sqlite3_finalize(ifile);
606 fprintf(stderr, "error clearing elf library: %d\n", rc);
609 sqlite3_finalize(ifile);
610 ifile = run_for_hash(db, "delete from elfneeded where file = ?", hash);
612 rc = sqlite3_step(ifile);
613 } while (rc == SQLITE_ROW);
614 if (rc != SQLITE_DONE) {
615 SQLERROR(sqlite3_errmsg(db));
616 sqlite3_finalize(ifile);
617 fprintf(stderr, "error clearing elf needed\n");
620 sqlite3_finalize(ifile);
622 hdr = libelf_header(content);
623 /* if lib, set soname */
624 if (libelf_type(content) == ET_DYN) {
625 char *soname = libelf_soname(content);
628 sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
629 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
630 sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
631 rc = sqlite3_step(ifile);
632 if (rc != SQLITE_DONE) {
633 SQLERROR(sqlite3_errmsg(db));
634 sqlite3_finalize(ifile);
635 fprintf(stderr, "error setting library soname\n");
638 sqlite3_finalize(ifile);
640 fprintf(stderr, "can't find soname\n");
644 /* if exe, set neededs */
645 if (libelf_type(content) == ET_EXEC) {
649 elf = (char *)content;
650 /* find program header table */
651 for (i = 0; i < hdr->e_phnum; i++) {
652 phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
653 if (phdr->p_type == PT_DYNAMIC) {
654 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
657 dyn = (Elf64_Dyn *)(elf + dsect->sh_offset);
661 dyn = (Elf64_Dyn *)dsect;
663 dsect = libelf_section(elf, SHT_DYNAMIC);
666 strsect = libelf_section_n(elf, dsect->sh_link);
667 strtab = elf + strsect->sh_offset;
669 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
670 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
671 while (dyn->d_tag != DT_NULL) {
672 if (dyn->d_tag == DT_NEEDED) {
676 need = strtab + dyn->d_un.d_val;
677 if (strlen(need) == 0) continue;
678 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
680 fprintf(stderr, "%s needs %s\n", hash, need);
682 rc = sqlite3_step(ifile);
683 if (rc != SQLITE_DONE) {
684 SQLERROR(sqlite3_errmsg(db));
685 sqlite3_finalize(ifile);
686 fprintf(stderr, "error setting needed library\n");
689 sqlite3_reset(ifile);
693 sqlite3_finalize(ifile);
700 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
704 unsigned char tmp[32];
705 struct sha256_state md;
707 int haverow = 0,havedata = 0;
715 if (!zpm || !zpm->db || !path) {
719 /* use local if caller didn't pass in room */
725 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
728 fd = open(path, O_RDONLY);
731 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
734 if (fstat(fd, &sbuf) == -1) {
736 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
739 /* not a regular file? */
740 if (!S_ISREG(sbuf.st_mode)) {
742 switch (sbuf.st_mode & S_IFMT) {
743 case S_IFSOCK: ftype = "socket"; break;
744 case S_IFLNK : ftype = "symlink"; break;
745 case S_IFBLK : ftype = "block device"; break;
746 case S_IFDIR : ftype = "directory"; break;
747 case S_IFCHR : ftype = "character device"; break;
748 case S_IFIFO : ftype = "fifo"; break;
749 default: ftype = "unknown file type"; break;
751 /* TODO this is ok, just stored differently */
752 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
757 content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
761 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
767 sha256_process(&md, content, sbuf.st_size);
768 sha256_done(&md, tmp);
770 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
773 //fprintf(stderr, "file %s: %s\n", path, hash);
775 /* TODO check null */
776 sqlite3 *db = zpm->db;
778 /* prepare and bind */
780 rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
781 if (rc != SQLITE_OK) {
782 SQLERROR(sqlite3_errmsg(db));
783 munmap(content, sbuf.st_size);
789 sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
791 rc = sqlite3_step(ifile);
793 if (rc != SQLITE_DONE) {
794 if (rc != SQLITE_ROW) {
795 /* didn't find a row */
796 SQLERROR(sqlite3_errmsg(db));
798 munmap(content, sbuf.st_size);
802 // fprintf(stderr, "have row for hash\n");
803 type = sqlite3_column_type(ifile, 0);
804 if (type == SQLITE_NULL) {
805 /* TODO assert, this shouldn't be possible? */
806 fprintf(stderr, "no file size\n");
807 sqlite3_finalize(ifile);
808 munmap(content, sbuf.st_size);
811 type = sqlite3_column_type(ifile, 1);
812 if (type == SQLITE_NULL) {
813 /* TODO assert, this shouldn't be possible? */
814 fprintf(stderr, "no file data\n");
815 sqlite3_finalize(ifile);
816 munmap(content, sbuf.st_size);
818 /* which is fine, just need to update the row then */
820 havedata = sqlite3_column_int(ifile, 1);
823 sqlite3_finalize(ifile);
827 outbuf = compresslzma(content, sbuf.st_size, &outlen);
829 fprintf(stderr, "compresslzma failed\n");
830 munmap(content, sbuf.st_size);
833 //fprintf(stderr, "compressed to %zu\n", outlen);
835 /* start a transaction */
836 // do that outside of here
841 //fprintf(stderr, "adding file data\n");
842 rc = sqlite3_prepare(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
844 //fprintf(stderr, "creating new data row\n");
845 rc = sqlite3_prepare(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
847 if (rc != SQLITE_OK) {
848 SQLERROR(sqlite3_errmsg(db));
849 fprintf(stderr, "cant prepare data\n");
851 munmap(content, sbuf.st_size);
855 sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
856 if (rc != SQLITE_OK) {
857 SQLERROR(sqlite3_errmsg(db));
858 fprintf(stderr, "cant bind size\n");
860 munmap(content, sbuf.st_size);
863 sqlite3_bind_blob64(ifile, 2, outbuf, (sqlite3_int64)outlen, SQLITE_STATIC);
864 if (rc != SQLITE_OK) {
865 SQLERROR(sqlite3_errmsg(db));
866 fprintf(stderr, "cant bind content\n");
868 munmap(content, sbuf.st_size);
871 sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
872 if (rc != SQLITE_OK) {
873 SQLERROR(sqlite3_errmsg(db));
874 fprintf(stderr, "cant bind hash\n");
876 munmap(content, sbuf.st_size);
879 rc = sqlite3_step(ifile);
880 if (rc != SQLITE_DONE) {
881 SQLERROR(sqlite3_errmsg(db));
882 sqlite3_finalize(ifile);
884 munmap(content, sbuf.st_size);
887 sqlite3_finalize(ifile);
892 /* don't need the original file now */
896 if (!set_elf_info(zpm->db, hash, content, sbuf.st_size)) {
897 fprintf(stderr, "setting elf info failed\n");
898 munmap(content, sbuf.st_size);
902 munmap(content, sbuf.st_size);
904 /* if package and not nopackage flag, add to package */
905 if (zpm->current_package->name && (!ZPM_NOPACKAGE)) {
915 int main(int ac, char **av){
930 fprintf(stderr, "usage: db hash file\n");
934 rc = sqlite3_open(av[1], &db);
936 SQLERROR(sqlite3_errmsg(db));
945 Packages are sqlite databases
947 get application id and userver
949 Primitive operations:
952 associate path with package
953 associate blob with path?
955 * extract blob to a path
956 compare blob to filesystem path
957 create package with info
961 record elf information about blob
964 sign a package? What are we verifying?