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