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 void * req;
00161
00162 int rd_timeoutsecs;
00163 ssize_t bytesRemain;
00164 ssize_t contentLength;
00165 int persist;
00166 int wr_chunked;
00167
00168 int syserrno;
00169
00170 const void *errcookie;
00171
00172
00173 const char *opath;
00174 int oflags;
00175 mode_t omode;
00176
00177 FDSTAT_t stats;
00178
00179 int ndigests;
00180 #define FDDIGEST_MAX 4
00181 struct _FDDIGEST_s digests[FDDIGEST_MAX];
00182
00183 int ftpFileDoneNeeded;
00184 unsigned long long fd_cpioPos;
00185 };
00186
00187
00188 #define FDSANE(fd) assert(fd && fd->magic == FDMAGIC)
00189
00190
00191
00192 extern int _rpmio_debug;
00193
00194
00195
00196
00197 extern int _av_debug;
00198
00199
00200
00201
00202 extern int _ftp_debug;
00203
00204
00205
00206
00207 extern int _dav_debug;
00208
00209
00210 #define DBG(_f, _m, _x) \
00211 \
00212 if ((_rpmio_debug | ((_f) ? ((FD_t)(_f))->flags : 0)) & (_m)) fprintf _x \
00213
00214
00215 #if defined(__LCLINT__XXX)
00216 #define DBGIO(_f, _x)
00217 #define DBGREFS(_f, _x)
00218 #else
00219 #define DBGIO(_f, _x) DBG((_f), RPMIO_DEBUG_IO, _x)
00220 #define DBGREFS(_f, _x) DBG((_f), RPMIO_DEBUG_REFS, _x)
00221 #endif
00222
00223 #ifdef __cplusplus
00224 extern "C" {
00225 #endif
00226
00229 int fdFgets(FD_t fd, char * buf, size_t len)
00230
00231 ;
00232
00235 FD_t ftpOpen(const char *url, int flags,
00236 mode_t mode, urlinfo *uret)
00237
00238 ;
00239
00242 int ftpReq(FD_t data, const char * ftpCmd, const char * ftpArg)
00243
00244 ;
00245
00248 int ftpCmd(const char * cmd, const char * url, const char * arg2)
00249
00250 ;
00251
00254 int ufdClose( void * cookie)
00255
00256 ;
00257
00260 static inline
00261 void fdSetOpen(FD_t fd, const char * path, int flags, mode_t mode)
00262
00263 {
00264 FDSANE(fd);
00265 if (fd->opath != NULL) {
00266 free((void *)fd->opath);
00267 fd->opath = NULL;
00268 }
00269 fd->opath = xstrdup(path);
00270 fd->oflags = flags;
00271 fd->omode = mode;
00272 }
00273
00276 static inline
00277 const char * fdGetOPath(FD_t fd)
00278
00279 {
00280 FDSANE(fd);
00281 return fd->opath;
00282 }
00283
00286 static inline
00287 int fdGetOFlags(FD_t fd)
00288
00289 {
00290 FDSANE(fd);
00291 return fd->oflags;
00292 }
00293
00296 static inline
00297 mode_t fdGetOMode(FD_t fd)
00298
00299 {
00300 FDSANE(fd);
00301 return fd->omode;
00302 }
00303
00306 static inline
00307 FDIO_t fdGetIo(FD_t fd)
00308
00309 {
00310 FDSANE(fd);
00311
00312 return fd->fps[fd->nfps].io;
00313
00314 }
00315
00318
00319 static inline
00320 void fdSetIo(FD_t fd, FDIO_t io)
00321
00322 {
00323 FDSANE(fd);
00324
00325
00326 fd->fps[fd->nfps].io = io;
00327
00328
00329 }
00330
00331
00334 static inline
00335 FILE * fdGetFILE(FD_t fd)
00336
00337 {
00338 FDSANE(fd);
00339
00340
00341 return ((FILE *)fd->fps[fd->nfps].fp);
00342
00343
00344 }
00345
00348 static inline
00349 void * fdGetFp(FD_t fd)
00350
00351 {
00352 FDSANE(fd);
00353
00354 return fd->fps[fd->nfps].fp;
00355
00356 }
00357
00360
00361 static inline
00362 void fdSetFp(FD_t fd, void * fp)
00363
00364 {
00365 FDSANE(fd);
00366
00367
00368 fd->fps[fd->nfps].fp = fp;
00369
00370
00371 }
00372
00373
00376 static inline
00377 int fdGetFdno(FD_t fd)
00378
00379 {
00380 FDSANE(fd);
00381
00382 return fd->fps[fd->nfps].fdno;
00383
00384 }
00385
00388 static inline
00389 void fdSetFdno(FD_t fd, int fdno)
00390
00391 {
00392 FDSANE(fd);
00393
00394 fd->fps[fd->nfps].fdno = fdno;
00395
00396 }
00397
00400 static inline
00401 void fdSetContentLength(FD_t fd, ssize_t contentLength)
00402
00403 {
00404 FDSANE(fd);
00405 fd->contentLength = fd->bytesRemain = contentLength;
00406 }
00407
00410 static inline
00411 void fdPush(FD_t fd, FDIO_t io, void * fp, int fdno)
00412
00413 {
00414 FDSANE(fd);
00415 if (fd->nfps >= (sizeof(fd->fps)/sizeof(fd->fps[0]) - 1))
00416 return;
00417 fd->nfps++;
00418 fdSetIo(fd, io);
00419 fdSetFp(fd, fp);
00420 fdSetFdno(fd, fdno);
00421 }
00422
00425 static inline
00426 void fdPop(FD_t fd)
00427
00428 {
00429 FDSANE(fd);
00430 if (fd->nfps < 0) return;
00431 fdSetIo(fd, NULL);
00432 fdSetFp(fd, NULL);
00433 fdSetFdno(fd, -1);
00434 fd->nfps--;
00435 }
00436
00439 static inline
00440 rpmop fdstat_op( FD_t fd, fdOpX opx)
00441
00442 {
00443 rpmop op = NULL;
00444
00445
00446 if (fd != NULL && fd->stats != NULL && opx >= 0 && opx < FDSTAT_MAX)
00447 op = fd->stats->ops + opx;
00448
00449 return op;
00450 }
00451
00454 static inline
00455 void fdstat_enter( FD_t fd, int opx)
00456
00457
00458 {
00459 if (fd == NULL) return;
00460 if (fd->stats != NULL)
00461 (void) rpmswEnter(fdstat_op(fd, opx), 0);
00462 }
00463
00466 static inline
00467 void fdstat_exit( FD_t fd, int opx, ssize_t rc)
00468
00469
00470 {
00471 if (fd == NULL) return;
00472 if (rc == -1)
00473 fd->syserrno = errno;
00474 else if (rc > 0 && fd->bytesRemain > 0)
00475 switch (opx) {
00476 case FDSTAT_READ:
00477 case FDSTAT_WRITE:
00478 fd->bytesRemain -= rc;
00479 break;
00480 default:
00481 break;
00482 }
00483 if (fd->stats != NULL)
00484 (void) rpmswExit(fdstat_op(fd, opx), rc);
00485 }
00486
00489
00490 static inline
00491 void fdstat_print( FD_t fd, const char * msg, FILE * fp)
00492
00493
00494 {
00495 static int usec_scale = (1000*1000);
00496 int opx;
00497
00498 if (fd == NULL || fd->stats == NULL) return;
00499 for (opx = 0; opx < 4; opx++) {
00500 rpmop op = &fd->stats->ops[opx];
00501 if (op->count <= 0) continue;
00502 switch (opx) {
00503 case FDSTAT_READ:
00504 if (msg) fprintf(fp, "%s:", msg);
00505 fprintf(fp, "%8d reads, %8lu total bytes in %d.%06d secs\n",
00506 op->count, (unsigned long)op->bytes,
00507 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00508 break;
00509 case FDSTAT_WRITE:
00510 if (msg) fprintf(fp, "%s:", msg);
00511 fprintf(fp, "%8d writes, %8lu total bytes in %d.%06d secs\n",
00512 op->count, (unsigned long)op->bytes,
00513 (int)(op->usecs/usec_scale), (int)(op->usecs%usec_scale));
00514 break;
00515 case FDSTAT_SEEK:
00516 break;
00517 case FDSTAT_CLOSE:
00518 break;
00519 }
00520 }
00521 }
00522
00523
00526 static inline
00527 void fdSetSyserrno(FD_t fd, int syserrno, const void * errcookie)
00528
00529 {
00530 FDSANE(fd);
00531 fd->syserrno = syserrno;
00532
00533 fd->errcookie = errcookie;
00534
00535 }
00536
00539 static inline
00540 int fdGetRdTimeoutSecs(FD_t fd)
00541
00542 {
00543 FDSANE(fd);
00544 return fd->rd_timeoutsecs;
00545 }
00546
00549 static inline
00550 unsigned long long fdGetCpioPos(FD_t fd)
00551
00552 {
00553 FDSANE(fd);
00554 return fd->fd_cpioPos;
00555 }
00556
00559 static inline
00560 void fdSetCpioPos(FD_t fd, long int cpioPos)
00561
00562 {
00563 FDSANE(fd);
00564 fd->fd_cpioPos = cpioPos;
00565 }
00566
00569 static inline
00570 FD_t c2f( void * cookie)
00571
00572 {
00573
00574 FD_t fd = (FD_t) cookie;
00575
00576 FDSANE(fd);
00577 return fd;
00578 }
00579
00583 static inline
00584 void fdInitDigest(FD_t fd, pgpHashAlgo hashalgo, int flags)
00585
00586
00587 {
00588 FDDIGEST_t fddig = fd->digests + fd->ndigests;
00589 if (fddig != (fd->digests + FDDIGEST_MAX)) {
00590 fd->ndigests++;
00591 fddig->hashalgo = hashalgo;
00592 fdstat_enter(fd, FDSTAT_DIGEST);
00593 fddig->hashctx = rpmDigestInit(hashalgo, flags);
00594 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00595 }
00596 }
00597
00601 static inline
00602 void fdUpdateDigests(FD_t fd, const unsigned char * buf, ssize_t buflen)
00603
00604
00605 {
00606 int i;
00607
00608 if (buf != NULL && buflen > 0)
00609 for (i = fd->ndigests - 1; i >= 0; i--) {
00610 FDDIGEST_t fddig = fd->digests + i;
00611 if (fddig->hashctx == NULL)
00612 continue;
00613 fdstat_enter(fd, FDSTAT_DIGEST);
00614 (void) rpmDigestUpdate(fddig->hashctx, buf, buflen);
00615 fdstat_exit(fd, FDSTAT_DIGEST, buflen);
00616 }
00617 }
00618
00621 static inline
00622 void fdFiniDigest(FD_t fd, pgpHashAlgo hashalgo,
00623 void * datap,
00624 size_t * lenp,
00625 int asAscii)
00626
00627
00628 {
00629 int imax = -1;
00630 int i;
00631
00632 for (i = fd->ndigests - 1; i >= 0; i--) {
00633 FDDIGEST_t fddig = fd->digests + i;
00634 if (fddig->hashctx == NULL)
00635 continue;
00636 if (i > imax) imax = i;
00637 if (fddig->hashalgo != hashalgo)
00638 continue;
00639 fdstat_enter(fd, FDSTAT_DIGEST);
00640 (void) rpmDigestFinal(fddig->hashctx, datap, lenp, asAscii);
00641 fdstat_exit(fd, FDSTAT_DIGEST, 0);
00642 fddig->hashctx = NULL;
00643 break;
00644 }
00645
00646 if (i < 0) {
00647 if (datap) *(void **)datap = NULL;
00648 if (lenp) *lenp = 0;
00649 }
00650
00651
00652 fd->ndigests = imax;
00653 if (i < imax)
00654 fd->ndigests++;
00655 }
00656
00657
00660 static inline
00661 int fdFileno( void * cookie)
00662
00663 {
00664 FD_t fd;
00665 if (cookie == NULL) return -2;
00666 fd = c2f(cookie);
00667
00668 return fd->fps[0].fdno;
00669
00670 }
00671
00672
00680 int rpmioSlurp(const char * fn,
00681 const unsigned char ** bp, ssize_t * blenp)
00682
00683 ;
00684
00685 #ifdef __cplusplus
00686 }
00687 #endif
00688
00689 #endif