rpm  4.5
rpmio_internal.h
Go to the documentation of this file.
1 #ifndef H_RPMIO_INTERNAL
2 #define H_RPMIO_INTERNAL
3 
9 #include <rpmio.h>
10 #include <rpmurl.h>
11 
12 #if USE_INTERNAL_BEECRYPT
13 #if HAVE_BEECRYPT_API_H
14 #include <api.h>
15 #else
16 #include <beecrypt.api.h>
17 #endif
18 #else /* use system beecrypt */
19 #if HAVE_BEECRYPT_API_H
20 #include <beecrypt/api.h>
21 #else
22 #include <beecrypt/beecrypt.api.h>
23 #endif
24 #endif
25 
26 #include <rpmpgp.h>
27 #include <rpmsw.h>
28 
29 /* Drag in the beecrypt includes. */
30 #if USE_INTERNAL_BEECRYPT
31 #include <beecrypt.h>
32 #include <base64.h>
33 #include <dsa.h>
34 #include <endianness.h>
35 #include <md5.h>
36 #include <mp.h>
37 #include <rsa.h>
38 #include <rsapk.h>
39 #include <sha1.h>
40 #if HAVE_BEECRYPT_API_H
41 #include <sha256.h>
42 #include <sha384.h>
43 #include <sha512.h>
44 #endif
45 #else /* use system beecrypt */
46 #include <beecrypt/beecrypt.h>
47 #include <beecrypt/base64.h>
48 #include <beecrypt/dsa.h>
49 #include <beecrypt/endianness.h>
50 #include <beecrypt/md5.h>
51 #include <beecrypt/mp.h>
52 #include <beecrypt/rsa.h>
53 #include <beecrypt/rsapk.h>
54 #include <beecrypt/sha1.h>
55 #if HAVE_BEECRYPT_API_H
56 #include <beecrypt/sha256.h>
57 #include <beecrypt/sha384.h>
58 #include <beecrypt/sha512.h>
59 #endif
60 #endif
61 
66 /*@only@*/ /*@null@*/
67  const char * userid;
68 /*@only@*/ /*@null@*/
69  const byte * hash;
70  const char * params[4];
72 
74  byte time[4];
83 #define PGPDIG_SAVED_TIME (1 << 0)
84 #define PGPDIG_SAVED_ID (1 << 1)
85 
86 };
87 
91 struct pgpDig_s {
94 
95  byte ** ppkts;
96  int npkts;
97  size_t nbytes;
99 /*@only@*/ /*@null@*/
101 /*@only@*/ /*@null@*/
103 /*@only@*/ /*@null@*/
104  void * sha1;
105  size_t sha1len;
107 /*@only@*/ /*@null@*/
109 /*@only@*/ /*@null@*/
111 /*@only@*/ /*@null@*/
112  void * md5;
113  size_t md5len;
115  /* DSA parameters. */
116  mpbarrett p;
117  mpbarrett q;
118  mpnumber g;
119  mpnumber y;
120  mpnumber hm;
121  mpnumber r;
122  mpnumber s;
123 
124  /* RSA parameters. */
125  rsapk rsa_pk;
126  mpnumber m;
127  mpnumber c;
128  mpnumber rsahm;
129 };
130 
133 typedef struct _FDSTACK_s {
134 /*@exposed@*/
136 /*@dependent@*/
137  void * fp;
138  int fdno;
139 } FDSTACK_t;
140 
144 typedef enum fdOpX_e {
151 } fdOpX;
152 
156 typedef /*@abstract@*/ struct {
158 } * FDSTAT_t;
159 
162 typedef struct _FDDIGEST_s {
165 } * FDDIGEST_t;
166 
170 struct _FD_s {
171 /*@refs@*/
172  int nrefs;
173  int flags;
174 #define RPMIO_DEBUG_IO 0x40000000
175 #define RPMIO_DEBUG_REFS 0x20000000
176  int magic;
177 #define FDMAGIC 0x04463138
178  int nfps;
180  int urlType; /* ufdio: */
181 
182 /*@dependent@*/
183  void * url; /* ufdio: URL info */
184 /*@relnull@*/
185 
186  int rd_timeoutsecs; /* ufdRead: per FD_t timer */
187  ssize_t bytesRemain; /* ufdio: */
188  ssize_t contentLength; /* ufdio: */
189  int persist; /* ufdio: */
190  int wr_chunked; /* ufdio: */
191 
192  int syserrno; /* last system errno encountered */
193 /*@observer@*/
194  const void *errcookie; /* gzdio/bzdio/ufdio: */
195 
196 /*null@*/
197  const char *opath; /* open(2) args. */
198  int oflags;
199  mode_t omode;
200 
201  FDSTAT_t stats; /* I/O statistics */
202 
203  int ndigests;
204 #define FDDIGEST_MAX 32
206 
207  int ftpFileDoneNeeded; /* ufdio: (FTP) */
208  unsigned long long fd_cpioPos; /* cpio: */
209 };
210 /*@access FD_t@*/
211 
212 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
213 
214 /*@-redecl@*/
215 /*@unchecked@*/
216 extern int _rpmio_debug;
217 /*@=redecl@*/
218 
219 /*@-redecl@*/
220 /*@unchecked@*/
221 extern int _av_debug;
222 /*@=redecl@*/
223 
224 /*@-redecl@*/
225 /*@unchecked@*/
226 extern int _ftp_debug;
227 /*@=redecl@*/
228 
229 /*@-redecl@*/
230 /*@unchecked@*/
231 extern int _dav_debug;
232 /*@=redecl@*/
233 
234 #define DBG(_f, _m, _x) \
235  /*@-modfilesys@*/ \
236  if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
237  /*@=modfilesys@*/
238 
239 #if defined(__LCLINT__XXX)
240 #define DBGIO(_f, _x)
241 #define DBGREFS(_f, _x)
242 #else
243 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
244 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
245 #endif
246 
247 #ifdef __cplusplus
248 extern "C" {
249 #endif
250 
253 /*@observer@*/ const char * fdbg(/*@null@*/ FD_t fd)
254  /*@*/;
255 
258 int fdFgets(FD_t fd, char * buf, size_t len)
259  /*@globals errno, fileSystem @*/
260  /*@modifies *buf, fd, errno, fileSystem @*/;
261 
264 /*@null@*/ FD_t ftpOpen(const char *url, /*@unused@*/ int flags,
265  /*@unused@*/ mode_t mode, /*@out@*/ urlinfo *uret)
266  /*@globals h_errno, fileSystem, internalState @*/
267  /*@modifies *uret, fileSystem, internalState @*/;
268 
271 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
272  /*@globals fileSystem, internalState @*/
273  /*@modifies data, fileSystem, internalState @*/;
274 
277 int ftpCmd(const char * cmd, const char * url, const char * arg2)
278  /*@globals h_errno, fileSystem, internalState @*/
279  /*@modifies fileSystem, internalState @*/;
280 
283 int ufdClose( /*@only@*/ void * cookie)
284  /*@globals fileSystem, internalState @*/
285  /*@modifies cookie, fileSystem, internalState @*/;
286 
289 /*@unused@*/ static inline
290 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
291  /*@modifies fd @*/
292 {
293  FDSANE(fd);
294  if (fd->opath != NULL) {
295  free((void *)fd->opath);
296  fd->opath = NULL;
297  }
298  fd->opath = xstrdup(path);
299  fd->oflags = flags;
300  fd->omode = mode;
301 }
302 
305 /*@unused@*/ static inline
306 /*@null@*/ /*@observer@*/ const char * fdGetOPath(FD_t fd)
307  /*@*/
308 {
309  FDSANE(fd);
310  return fd->opath;
311 }
312 
315 /*@unused@*/ static inline
317  /*@*/
318 {
319  FDSANE(fd);
320  return fd->oflags;
321 }
322 
325 /*@unused@*/ static inline
326 mode_t fdGetOMode(FD_t fd)
327  /*@*/
328 {
329  FDSANE(fd);
330  return fd->omode;
331 }
332 
335 /*@unused@*/ static inline
336 /*@null@*/ FDIO_t fdGetIo(FD_t fd)
337  /*@*/
338 {
339  FDSANE(fd);
340 /*@-boundsread@*/
341  return fd->fps[fd->nfps].io;
342 /*@=boundsread@*/
343 }
344 
347 /*@-nullstate@*/ /* FIX: io may be NULL */
348 /*@unused@*/ static inline
349 void fdSetIo(FD_t fd, /*@kept@*/ /*@null@*/ FDIO_t io)
350  /*@modifies fd @*/
351 {
352  FDSANE(fd);
353 /*@-boundswrite@*/
354  /*@-assignexpose@*/
355  fd->fps[fd->nfps].io = io;
356  /*@=assignexpose@*/
357 /*@=boundswrite@*/
358 }
359 /*@=nullstate@*/
360 
363 /*@unused@*/ static inline
364 /*@exposed@*/ /*@dependent@*/ /*@null@*/ FILE * fdGetFILE(FD_t fd)
365  /*@*/
366 {
367  FDSANE(fd);
368 /*@-boundsread@*/
369  /*@+voidabstract@*/
370  return ((FILE *)fd->fps[fd->nfps].fp);
371  /*@=voidabstract@*/
372 /*@=boundsread@*/
373 }
374 
377 /*@unused@*/ static inline
378 /*@exposed@*/ /*@dependent@*/ /*@null@*/ void * fdGetFp(FD_t fd)
379  /*@*/
380 {
381  FDSANE(fd);
382 /*@-boundsread@*/
383  return fd->fps[fd->nfps].fp;
384 /*@=boundsread@*/
385 }
386 
389 /*@-nullstate@*/ /* FIX: fp may be NULL */
390 /*@unused@*/ static inline
391 void fdSetFp(FD_t fd, /*@kept@*/ /*@null@*/ void * fp)
392  /*@modifies fd @*/
393 {
394  FDSANE(fd);
395 /*@-boundswrite@*/
396  /*@-assignexpose@*/
397  fd->fps[fd->nfps].fp = fp;
398  /*@=assignexpose@*/
399 /*@=boundswrite@*/
400 }
401 /*@=nullstate@*/
402 
405 /*@unused@*/ static inline
407  /*@*/
408 {
409  FDSANE(fd);
410 /*@-boundsread@*/
411  return fd->fps[fd->nfps].fdno;
412 /*@=boundsread@*/
413 }
414 
417 /*@unused@*/ static inline
418 void fdSetFdno(FD_t fd, int fdno)
419  /*@modifies fd @*/
420 {
421  FDSANE(fd);
422 /*@-boundswrite@*/
423  fd->fps[fd->nfps].fdno = fdno;
424 /*@=boundswrite@*/
425 }
426 
429 /*@unused@*/ static inline
430 void fdSetContentLength(FD_t fd, ssize_t contentLength)
431  /*@modifies fd @*/
432 {
433  FDSANE(fd);
434  fd->contentLength = fd->bytesRemain = contentLength;
435 }
436 
439 /*@unused@*/ static inline
440 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
441  /*@modifies fd @*/
442 {
443  FDSANE(fd);
444  if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
445  return;
446  fd->nfps++;
447  fdSetIo(fd, io);
448  fdSetFp(fd, fp);
449  fdSetFdno(fd, fdno);
450 }
451 
454 /*@unused@*/ static inline
455 void fdPop(FD_t fd)
456  /*@modifies fd @*/
457 {
458  FDSANE(fd);
459  if (fd->nfps < 0) return;
460  fdSetIo(fd, NULL);
461  fdSetFp(fd, NULL);
462  fdSetFdno(fd, -1);
463  fd->nfps--;
464 }
465 
468 /*@unused@*/ static inline /*@null@*/
469 rpmop fdstat_op(/*@null@*/ FD_t fd, fdOpX opx)
470  /*@*/
471 {
472  rpmop op = NULL;
473 
474 /*@-boundsread@*/
475  if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
476  op = fd->stats->ops + opx;
477 /*@=boundsread@*/
478  return op;
479 }
480 
483 /*@unused@*/ static inline
484 void fdstat_enter(/*@null@*/ FD_t fd, int opx)
485  /*@globals internalState @*/
486  /*@modifies internalState @*/
487 {
488  if (fd == NULL) return;
489  if (fd->stats != NULL)
490  (void) rpmswEnter(fdstat_op(fd, opx), 0);
491 }
492 
495 /*@unused@*/ static inline
496 void fdstat_exit(/*@null@*/ FD_t fd, int opx, ssize_t rc)
497  /*@globals internalState @*/
498  /*@modifies fd, internalState @*/
499 {
500  if (fd == NULL) return;
501  if (rc == -1)
502  fd->syserrno = errno;
503  else if (rc > 0 && fd->bytesRemain > 0)
504  switch (opx) {
505  case FDSTAT_READ:
506  case FDSTAT_WRITE:
507  fd->bytesRemain -= rc;
508  break;
509  default:
510  break;
511  }
512  if (fd->stats != NULL)
513  (void) rpmswExit(fdstat_op(fd, opx), rc);
514 }
515 
518 /*@-boundsread@*/
519 /*@unused@*/ static inline
520 void fdstat_print(/*@null@*/ FD_t fd, const char * msg, FILE * fp)
521  /*@globals fileSystem @*/
522  /*@modifies *fp, fileSystem @*/
523 {
524  static int usec_scale = (1000*1000);
525  int opx;
526 
527  if (fd == NULL || fd->stats == NULL) return;
528  for (opx = 0; opx < 4; opx++) {
529  rpmop op = &fd->stats->ops[opx];
530  if (op->count <= 0) continue;
531  switch (opx) {
532  case FDSTAT_READ:
533  if (msg) fprintf(fp, "%s:", msg);
534  fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
535  op->count, (unsigned long)op->bytes,
536  (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
537  /*@switchbreak@*/ break;
538  case FDSTAT_WRITE:
539  if (msg) fprintf(fp, "%s:", msg);
540  fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
541  op->count, (unsigned long)op->bytes,
542  (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
543  /*@switchbreak@*/ break;
544  case FDSTAT_SEEK:
545  /*@switchbreak@*/ break;
546  case FDSTAT_CLOSE:
547  /*@switchbreak@*/ break;
548  }
549  }
550 }
551 /*@=boundsread@*/
552 
555 /*@unused@*/ static inline
556 void fdSetSyserrno(FD_t fd, int syserrno, /*@kept@*/ const void * errcookie)
557  /*@modifies fd @*/
558 {
559  FDSANE(fd);
560  fd->syserrno = syserrno;
561  /*@-assignexpose@*/
562  fd->errcookie = errcookie;
563  /*@=assignexpose@*/
564 }
565 
568 /*@unused@*/ static inline
570  /*@*/
571 {
572  FDSANE(fd);
573  return fd->rd_timeoutsecs;
574 }
575 
578 /*@unused@*/ static inline
579 unsigned long long fdGetCpioPos(FD_t fd)
580  /*@*/
581 {
582  FDSANE(fd);
583  return fd->fd_cpioPos;
584 }
585 
588 /*@unused@*/ static inline
589 void fdSetCpioPos(FD_t fd, long int cpioPos)
590  /*@modifies fd @*/
591 {
592  FDSANE(fd);
593  fd->fd_cpioPos = cpioPos;
594 }
595 
598 /*@mayexit@*/ /*@unused@*/ static inline
599 FD_t c2f(/*@null@*/ void * cookie)
600  /*@*/
601 {
602  /*@-castexpose@*/
603  FD_t fd = (FD_t) cookie;
604  /*@=castexpose@*/
605  FDSANE(fd);
606  /*@-refcounttrans -retalias@*/ return fd; /*@=refcounttrans =retalias@*/
607 }
608 
612 /*@unused@*/ static inline
613 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
614  /*@globals internalState @*/
615  /*@modifies fd, internalState @*/
616 {
617  FDDIGEST_t fddig = fd->digests + fd->ndigests;
618  if (fddig != (fd->digests + FDDIGEST_MAX)) {
619  fd->ndigests++;
620  fddig->hashalgo = hashalgo;
622  fddig->hashctx = rpmDigestInit(hashalgo, flags);
623  fdstat_exit(fd, FDSTAT_DIGEST, 0);
624  }
625 }
626 
630 /*@unused@*/ static inline
631 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
632  /*@globals internalState @*/
633  /*@modifies fd, internalState @*/
634 {
635  int i;
636 
637  if (buf != NULL && buflen > 0)
638  for (i = fd->ndigests - 1; i >= 0; i--) {
639  FDDIGEST_t fddig = fd->digests + i;
640  if (fddig->hashctx == NULL)
641  continue;
643  (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
644  fdstat_exit(fd, FDSTAT_DIGEST, buflen);
645  }
646 }
647 
650 /*@unused@*/ static inline
652  /*@null@*/ /*@out@*/ void * datap,
653  /*@null@*/ /*@out@*/ size_t * lenp,
654  int asAscii)
655  /*@globals internalState @*/
656  /*@modifies fd, *datap, *lenp, internalState @*/
657 {
658  int imax = -1;
659  int i;
660 
661  for (i = fd->ndigests - 1; i >= 0; i--) {
662  FDDIGEST_t fddig = fd->digests + i;
663  if (fddig->hashctx == NULL)
664  continue;
665  if (i > imax) imax = i;
666  if (fddig->hashalgo != hashalgo)
667  continue;
669  (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
670  fdstat_exit(fd, FDSTAT_DIGEST, 0);
671  fddig->hashctx = NULL;
672  break;
673  }
674 /*@-boundswrite@*/
675  if (i < 0) {
676  if (datap) *(void **)datap = NULL;
677  if (lenp) *lenp = 0;
678  }
679 /*@=boundswrite@*/
680 
681  fd->ndigests = imax;
682  if (i < imax)
683  fd->ndigests++; /* convert index to count */
684 }
685 
686 /*@-shadow@*/
689 /*@unused@*/ static inline
690 int fdFileno(/*@null@*/ void * cookie)
691  /*@*/
692 {
693  FD_t fd;
694  if (cookie == NULL) return -2;
695  fd = c2f(cookie);
696 /*@-boundsread@*/
697  return fd->fps[0].fdno;
698 /*@=boundsread@*/
699 }
700 /*@=shadow@*/
701 
709 int rpmioSlurp(const char * fn,
710  /*@out@*/ const unsigned char ** bp, /*@out@*/ ssize_t * blenp)
711  /*@globals h_errno, fileSystem, internalState @*/
712  /*@modifies *bp, *blenp, fileSystem, internalState @*/;
713 
714 #ifdef __cplusplus
715 }
716 #endif
717 
718 #endif /* H_RPMIO_INTERNAL */