00001 #ifndef H_RPMIO_INTERNAL
00002 #define H_RPMIO_INTERNAL
00003
00009 #include <rpmio.h>
00010 #include <rpmurl.h>
00011
00012 #if HAVE_BEECRYPT_API_H
00013 #include <api.h>
00014 #else
00015 #include <beecrypt.api.h>
00016 #endif
00017
00018 #include <rpmpgp.h>
00019 #include <rpmsw.h>
00020
00021
00022 #include <beecrypt.h>
00023 #include <base64.h>
00024 #include <dsa.h>
00025 #include <endianness.h>
00026 #include <md5.h>
00027 #include <mp.h>
00028 #include <rsa.h>
00029 #include <rsapk.h>
00030 #include <sha1.h>
00031 #if HAVE_BEECRYPT_API_H
00032 #include <sha256.h>
00033 #include <sha384.h>
00034 #include <sha512.h>
00035 #endif
00036
00040 struct pgpDigParams_s {
00041
00042 const char * userid;
00043
00044 const byte * hash;
00045 const char * params[4];
00046 byte tag;
00047
00048 byte version;
00049 byte time[4];
00050 byte pubkey_algo;
00052 byte hash_algo;
00053 byte sigtype;
00054 byte hashlen;
00055 byte signhash16[2];
00056 byte signid[8];
00057 byte saved;
00058 #define PGPDIG_SAVED_TIME (1 << 0)
00059 #define PGPDIG_SAVED_ID (1 << 1)
00060
00061 };
00062
00066 struct pgpDig_s {
00067 struct pgpDigParams_s signature;
00068 struct pgpDigParams_s pubkey;
00069
00070 byte ** ppkts;
00071 int npkts;
00072 size_t nbytes;
00074
00075 DIGEST_CTX sha1ctx;
00076
00077 DIGEST_CTX hdrsha1ctx;
00078
00079 void * sha1;
00080 size_t sha1len;
00082
00083 DIGEST_CTX md5ctx;
00084
00085 DIGEST_CTX hdrmd5ctx;
00086
00087 void * md5;
00088 size_t md5len;
00090
00091 mpbarrett p;
00092 mpbarrett q;
00093 mpnumber g;
00094 mpnumber y;
00095 mpnumber hm;
00096 mpnumber r;
00097 mpnumber s;
00098
00099
00100 rsapk rsa_pk;
00101 mpnumber m;
00102 mpnumber c;
00103 mpnumber rsahm;
00104 };
00105
00108 typedef struct _FDSTACK_s {
00109
00110 FDIO_t io;
00111
00112 void * fp;
00113 int fdno;
00114 } FDSTACK_t;
00115
00119 typedef enum fdOpX_e {
00120 FDSTAT_READ = 0,
00121 FDSTAT_WRITE = 1,
00122 FDSTAT_SEEK = 2,
00123 FDSTAT_CLOSE = 3,
00124 FDSTAT_DIGEST = 4,
00125 FDSTAT_MAX = 5
00126 } fdOpX;
00127
00131 typedef struct {
00132 struct rpmop_s ops[FDSTAT_MAX];
00133 } * FDSTAT_t;
00134
00137 typedef struct _FDDIGEST_s {
00138 pgpHashAlgo hashalgo;
00139 DIGEST_CTX hashctx;
00140 } * FDDIGEST_t;
00141
00145 struct _FD_s {
00146
00147 int nrefs;
00148 int flags;
00149 #define RPMIO_DEBUG_IO 0x40000000
00150 #define RPMIO_DEBUG_REFS 0x20000000
00151 int magic;
00152 #define FDMAGIC 0x04463138
00153 int nfps;
00154 FDSTACK_t fps[8];
00155 int urlType;
00156
00157
00158 void * url;
00159
00160
00161 int rd_timeoutsecs;
00162 ssize_t bytesRemain;
00163 ssize_t contentLength;
00164 int persist;
00165 int wr_chunked;
00166
00167 int syserrno;
00168
00169 const void *errcookie;
00170
00171
00172 const char *opath;
00173 int oflags;
00174 mode_t omode;
00175
00176 FDSTAT_t stats;
00177
00178 int ndigests;
00179 #define FDDIGEST_MAX 4
00180 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00181
00182 int ftpFileDoneNeeded;
00183 unsigned long long fd_cpioPos;
00184 };
00185
00186
00187 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00188
00189
00190
00191 extern int _rpmio_debug;
00192
00193
00194
00195
00196 extern int _av_debug;
00197
00198
00199
00200
00201 extern int _ftp_debug;
00202
00203
00204
00205
00206 extern int _dav_debug;
00207
00208
00209 #define DBG(_f, _m, _x) \
00210 \
00211 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00212
00213
00214 #if defined(__LCLINT__XXX)
00215 #define DBGIO(_f, _x)
00216 #define DBGREFS(_f, _x)
00217 #else
00218 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00219 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00220 #endif
00221
00222 #ifdef __cplusplus
00223 extern "C" {
00224 #endif
00225
00228 int fdFgets(FD_t fd, char * buf, size_t len)
00229
00230 ;
00231
00234 FD_t ftpOpen(const char *url, int flags,
00235 mode_t mode, urlinfo *uret)
00236
00237 ;
00238
00241 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00242
00243 ;
00244
00247 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00248
00249 ;
00250
00253 int ufdClose( void * cookie)
00254
00255 ;
00256
00259 static inline
00260 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00261
00262 {
00263 FDSANE(fd);
00264 if (fd->opath != NULL) {
00265 free((void *)fd->opath);
00266 fd->opath = NULL;
00267 }
00268 fd->opath = xstrdup(path);
00269 fd->oflags = flags;
00270 fd->omode = mode;
00271 }
00272
00275 static inline
00276 const char * fdGetOPath(FD_t fd)
00277
00278 {
00279 FDSANE(fd);
00280 return fd->opath;
00281 }
00282
00285 static inline
00286 int fdGetOFlags(FD_t fd)
00287
00288 {
00289 FDSANE(fd);
00290 return fd->oflags;
00291 }
00292
00295 static inline
00296 mode_t fdGetOMode(FD_t fd)
00297
00298 {
00299 FDSANE(fd);
00300 return fd->omode;
00301 }
00302
00305 static inline
00306 FDIO_t fdGetIo(FD_t fd)
00307
00308 {
00309 FDSANE(fd);
00310
00311 return fd->fps[fd->nfps].io;
00312
00313 }
00314
00317
00318 static inline
00319 void fdSetIo(FD_t fd, FDIO_t io)
00320
00321 {
00322 FDSANE(fd);
00323
00324
00325 fd->fps[fd->nfps].io = io;
00326
00327
00328 }
00329
00330
00333 static inline
00334 FILE * fdGetFILE(FD_t fd)
00335
00336 {
00337 FDSANE(fd);
00338
00339
00340 return ((FILE *)fd->fps[fd->nfps].fp);
00341
00342
00343 }
00344
00347 static inline
00348 void * fdGetFp(FD_t fd)
00349
00350 {
00351 FDSANE(fd);
00352
00353 return fd->fps[fd->nfps].fp;
00354
00355 }
00356
00359
00360 static inline
00361 void fdSetFp(FD_t fd, void * fp)
00362
00363 {
00364 FDSANE(fd);
00365
00366
00367 fd->fps[fd->nfps].fp = fp;
00368
00369
00370 }
00371
00372
00375 static inline
00376 int fdGetFdno(FD_t fd)
00377
00378 {
00379 FDSANE(fd);
00380
00381 return fd->fps[fd->nfps].fdno;
00382
00383 }
00384
00387 static inline
00388 void fdSetFdno(FD_t fd, int fdno)
00389
00390 {
00391 FDSANE(fd);
00392
00393 fd->fps[fd->nfps].fdno = fdno;
00394
00395 }
00396
00399 static inline
00400 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00401
00402 {
00403 FDSANE(fd);
00404 fd->contentLength = fd->bytesRemain = contentLength;
00405 }
00406
00409 static inline
00410 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00411
00412 {
00413 FDSANE(fd);
00414 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00415 return;
00416 fd->nfps++;
00417 fdSetIo(fd, io);
00418 fdSetFp(fd, fp);
00419 fdSetFdno(fd, fdno);
00420 }
00421
00424 static inline
00425 void fdPop(FD_t fd)
00426
00427 {
00428 FDSANE(fd);
00429 if (fd->nfps < 0) return;
00430 fdSetIo(fd, NULL);
00431 fdSetFp(fd, NULL);
00432 fdSetFdno(fd, -1);
00433 fd->nfps--;
00434 }
00435
00438 static inline
00439 rpmop fdstat_op( FD_t fd, fdOpX opx)
00440
00441 {
00442 rpmop op = NULL;
00443
00444
00445 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00446 op = fd->stats->ops + opx;
00447
00448 return op;
00449 }
00450
00453 static inline
00454 void fdstat_enter( FD_t fd, int opx)
00455
00456
00457 {
00458 if (fd == NULL) return;
00459 if (fd->stats != NULL)
00460 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00461 }
00462
00465 static inline
00466 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00467
00468
00469 {
00470 if (fd == NULL) return;
00471 if (rc == -1)
00472 fd->syserrno = errno;
00473 else if (rc > 0 && fd->bytesRemain > 0)
00474 switch (opx) {
00475 case FDSTAT_READ:
00476 case FDSTAT_WRITE:
00477 fd->bytesRemain -= rc;
00478 break;
00479 default:
00480 break;
00481 }
00482 if (fd->stats != NULL)
00483 (void) rpmswExit(fdstat_op(fd, opx), rc);
00484 }
00485
00488
00489 static inline
00490 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00491
00492
00493 {
00494 static int usec_scale = (1000*1000);
00495 int opx;
00496
00497 if (fd == NULL || fd->stats == NULL) return;
00498 for (opx = 0; opx < 4; opx++) {
00499 rpmop op = &fd->stats->ops[opx];
00500 if (op->count <= 0) continue;
00501 switch (opx) {
00502 case FDSTAT_READ:
00503 if (msg) fprintf(fp, "%s:", msg);
00504 fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00505 op->count, (unsigned long)op->bytes,
00506 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00507 break;
00508 case FDSTAT_WRITE:
00509 if (msg) fprintf(fp, "%s:", msg);
00510 fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00511 op->count, (unsigned long)op->bytes,
00512 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00513 break;
00514 case FDSTAT_SEEK:
00515 break;
00516 case FDSTAT_CLOSE:
00517 break;
00518 }
00519 }
00520 }
00521
00522
00525 static inline
00526 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00527
00528 {
00529 FDSANE(fd);
00530 fd->syserrno = syserrno;
00531
00532 fd->errcookie = errcookie;
00533
00534 }
00535
00538 static inline
00539 int fdGetRdTimeoutSecs(FD_t fd)
00540
00541 {
00542 FDSANE(fd);
00543 return fd->rd_timeoutsecs;
00544 }
00545
00548 static inline
00549 unsigned long long fdGetCpioPos(FD_t fd)
00550
00551 {
00552 FDSANE(fd);
00553 return fd->fd_cpioPos;
00554 }
00555
00558 static inline
00559 void fdSetCpioPos(FD_t fd, long int cpioPos)
00560
00561 {
00562 FDSANE(fd);
00563 fd->fd_cpioPos = cpioPos;
00564 }
00565
00568 static inline
00569 FD_t c2f( void * cookie)
00570
00571 {
00572
00573 FD_t fd = (FD_t) cookie;
00574
00575 FDSANE(fd);
00576 return fd;
00577 }
00578
00582 static inline
00583 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00584
00585
00586 {
00587 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00588 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00589 fd->ndigests++;
00590 fddig->hashalgo = hashalgo;
00591 fdstat_enter(fd, FDSTAT_DIGEST);
00592 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00593 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00594 }
00595 }
00596
00600 static inline
00601 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00602
00603
00604 {
00605 int i;
00606
00607 if (buf != NULL && buflen > 0)
00608 for (i = fd->ndigests - 1; i >= 0; i--) {
00609 FDDIGEST_t fddig = fd->digests + i;
00610 if (fddig->hashctx == NULL)
00611 continue;
00612 fdstat_enter(fd, FDSTAT_DIGEST);
00613 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00614 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00615 }
00616 }
00617
00620 static inline
00621 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00622 void ** datap,
00623 size_t * lenp,
00624 int asAscii)
00625
00626
00627 {
00628 int imax = -1;
00629 int i;
00630
00631 for (i = fd->ndigests - 1; i >= 0; i--) {
00632 FDDIGEST_t fddig = fd->digests + i;
00633 if (fddig->hashctx == NULL)
00634 continue;
00635 if (i > imax) imax = i;
00636 if (fddig->hashalgo != hashalgo)
00637 continue;
00638 fdstat_enter(fd, FDSTAT_DIGEST);
00639 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00640 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00641 fddig->hashctx = NULL;
00642 break;
00643 }
00644
00645 if (i < 0) {
00646 if (datap) *datap = NULL;
00647 if (lenp) *lenp = 0;
00648 }
00649
00650
00651 fd->ndigests = imax;
00652 if (i < imax)
00653 fd->ndigests++;
00654 }
00655
00656
00659 static inline
00660 int fdFileno( void * cookie)
00661
00662 {
00663 FD_t fd;
00664 if (cookie == NULL) return -2;
00665 fd = c2f(cookie);
00666
00667 return fd->fps[0].fdno;
00668
00669 }
00670
00671
00679 int rpmioSlurp(const char * fn,
00680 const unsigned char ** bp, ssize_t * blenp)
00681
00682 ;
00683
00684 #ifdef __cplusplus
00685 }
00686 #endif
00687
00688 #endif