void cupsdInitCerts(void) { #ifndef HAVE_ARC4RANDOM cups_file_t *fp; /* /dev/random file */ /* * Initialize the random number generator using the random device or * the current time, as available... */ if ((fp = cupsFileOpen("/dev/urandom", "rb")) == NULL) { struct timeval tod; /* Time of day */ /* * Get the time in usecs and use it as the initial seed... */ gettimeofday(&tod, NULL); CUPS_SRAND((unsigned)(tod.tv_sec + tod.tv_usec)); } else { unsigned seed; /* Seed for random number generator */ /* * Read 4 random characters from the random device and use * them as the seed... */ seed = cupsFileGetChar(fp); seed = (seed << 8) | cupsFileGetChar(fp); seed = (seed << 8) | cupsFileGetChar(fp); CUPS_SRAND((seed << 8) | cupsFileGetChar(fp)); cupsFileClose(fp); } #endif /* !HAVE_ARC4RANDOM */ /* * Create a root certificate and return... */ if (!RunUser) cupsdAddCert(0, "root", cupsdDefaultAuthType()); }
static const char * /* O - New session ID */ cgi_set_sid(void) { char buffer[512], /* SID data */ sid[33]; /* SID string */ _cups_md5_state_t md5; /* MD5 state */ unsigned char sum[16]; /* MD5 sum */ const char *remote_addr, /* REMOTE_ADDR */ *server_name, /* SERVER_NAME */ *server_port; /* SERVER_PORT */ if ((remote_addr = getenv("REMOTE_ADDR")) == NULL) remote_addr = "REMOTE_ADDR"; if ((server_name = getenv("SERVER_NAME")) == NULL) server_name = "SERVER_NAME"; if ((server_port = getenv("SERVER_PORT")) == NULL) server_port = "SERVER_PORT"; CUPS_SRAND(time(NULL)); snprintf(buffer, sizeof(buffer), "%s:%s:%s:%02X%02X%02X%02X%02X%02X%02X%02X", remote_addr, server_name, server_port, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255); _cupsMD5Init(&md5); _cupsMD5Append(&md5, (unsigned char *)buffer, (int)strlen(buffer)); _cupsMD5Finish(&md5, sum); cgiSetCookie(CUPS_SID, httpMD5String(sum, sid), "/", NULL, 0, 0); return (cupsGetOption(CUPS_SID, num_cookies, cookies)); }
static int /* O - Status */ read_write_tests(int compression) /* I - Use compression? */ { int i; /* Looping var */ cups_file_t *fp; /* File */ int status; /* Exit status */ char line[1024], /* Line from file */ *value; /* Directive value from line */ int linenum; /* Line number */ unsigned char readbuf[8192], /* Read buffer */ writebuf[8192]; /* Write buffer */ int byte; /* Byte from file */ off_t length; /* Length of file */ static const char *partial_line = "partial line"; /* Partial line */ /* * No errors so far... */ status = 0; /* * Initialize the write buffer with random data... */ CUPS_SRAND((unsigned)time(NULL)); for (i = 0; i < (int)sizeof(writebuf); i ++) writebuf[i] = CUPS_RAND(); /* * cupsFileOpen(write) */ printf("cupsFileOpen(write%s): ", compression ? " compressed" : ""); fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", compression ? "w9" : "w"); if (fp) { puts("PASS"); /* * cupsFileCompression() */ fputs("cupsFileCompression(): ", stdout); if (cupsFileCompression(fp) == compression) puts("PASS"); else { printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), compression); status ++; } /* * cupsFilePuts() */ fputs("cupsFilePuts(): ", stdout); if (cupsFilePuts(fp, "# Hello, World\n") > 0) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFilePrintf() */ fputs("cupsFilePrintf(): ", stdout); for (i = 0; i < 1000; i ++) if (cupsFilePrintf(fp, "TestLine %03d\n", i) < 0) break; if (i >= 1000) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFilePutChar() */ fputs("cupsFilePutChar(): ", stdout); for (i = 0; i < 256; i ++) if (cupsFilePutChar(fp, i) < 0) break; if (i >= 256) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileWrite() */ fputs("cupsFileWrite(): ", stdout); for (i = 0; i < 10000; i ++) if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0) break; if (i >= 10000) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFilePuts() with partial line... */ fputs("cupsFilePuts(\"partial line\"): ", stdout); if (cupsFilePuts(fp, partial_line) > 0) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileTell() */ fputs("cupsFileTell(): ", stdout); if ((length = cupsFileTell(fp)) == 81933283) puts("PASS"); else { printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length); status ++; } /* * cupsFileClose() */ fputs("cupsFileClose(): ", stdout); if (!cupsFileClose(fp)) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileOpen(read) */ fputs("\ncupsFileOpen(read): ", stdout); fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r"); if (fp) { puts("PASS"); /* * cupsFileGets() */ fputs("cupsFileGets(): ", stdout); if (cupsFileGets(fp, line, sizeof(line))) { if (line[0] == '#') puts("PASS"); else { printf("FAIL (Got line \"%s\", expected comment line)\n", line); status ++; } } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileCompression() */ fputs("cupsFileCompression(): ", stdout); if (cupsFileCompression(fp) == compression) puts("PASS"); else { printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), compression); status ++; } /* * cupsFileGetConf() */ linenum = 1; fputs("cupsFileGetConf(): ", stdout); for (i = 0; i < 1000; i ++) if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) break; else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i || linenum != (i + 2)) break; if (i >= 1000) puts("PASS"); else if (line[0]) { printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum, line, value ? value : "(null)"); status ++; } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileGetChar() */ fputs("cupsFileGetChar(): ", stdout); for (i = 0; i < 256; i ++) if ((byte = cupsFileGetChar(fp)) != i) break; if (i >= 256) puts("PASS"); else if (byte >= 0) { printf("FAIL (Got %d, expected %d)\n", byte, i); status ++; } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileRead() */ fputs("cupsFileRead(): ", stdout); for (i = 0; i < 10000; i ++) if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0) break; else if (memcmp(readbuf, writebuf, sizeof(readbuf))) break; if (i >= 10000) puts("PASS"); else if (byte > 0) { printf("FAIL (Pass %d, ", i); for (i = 0; i < (int)sizeof(readbuf); i ++) if (readbuf[i] != writebuf[i]) break; printf("match failed at offset %d - got %02X, expected %02X)\n", i, readbuf[i], writebuf[i]); } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * cupsFileGetChar() with partial line... */ fputs("cupsFileGetChar(partial line): ", stdout); for (i = 0; i < (int)strlen(partial_line); i ++) if ((byte = cupsFileGetChar(fp)) < 0) break; else if (byte != partial_line[i]) break; if (!partial_line[i]) puts("PASS"); else { printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]); status ++; } /* * cupsFileTell() */ fputs("cupsFileTell(): ", stdout); if ((length = cupsFileTell(fp)) == 81933283) puts("PASS"); else { printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length); status ++; } /* * cupsFileClose() */ fputs("cupsFileClose(): ", stdout); if (!cupsFileClose(fp)) puts("PASS"); else { printf("FAIL (%s)\n", strerror(errno)); status ++; } } else { printf("FAIL (%s)\n", strerror(errno)); status ++; } /* * Remove the test file... */ unlink(compression ? "testfile.dat.gz" : "testfile.dat"); /* * Return the test status... */ return (status); }
static void write_test(int fd, /* I - File descriptor to write to */ cups_mode_t mode) /* I - Write mode */ { int page, x, y; /* Looping vars */ int count; /* Number of bytes to set */ cups_raster_t *r; /* Raster stream */ cups_page_header2_t header; /* Page header */ unsigned char data[32][8 * TEST_WIDTH]; /* Raster data to write */ /* * Create a combination of random data and repeated data to simulate * text with some whitespace. */ CUPS_SRAND(time(NULL)); memset(data, 0, sizeof(data)); for (y = 0; y < 28; y ++) { for (x = CUPS_RAND() & 127, count = (CUPS_RAND() & 15) + 1; x < sizeof(data[0]); x ++, count --) { if (count <= 0) { x += (CUPS_RAND() & 15) + 1; count = (CUPS_RAND() & 15) + 1; if (x >= sizeof(data[0])) break; } data[y][x] = CUPS_RAND(); } } /* * Test write speed... */ if ((r = cupsRasterOpen(fd, mode)) == NULL) { perror("Unable to create raster output stream"); return; } for (page = 0; page < TEST_PAGES; page ++) { memset(&header, 0, sizeof(header)); header.cupsWidth = TEST_WIDTH; header.cupsHeight = TEST_HEIGHT; header.cupsBytesPerLine = TEST_WIDTH; if (page & 1) { header.cupsBytesPerLine *= 4; header.cupsColorSpace = CUPS_CSPACE_CMYK; header.cupsColorOrder = CUPS_ORDER_CHUNKED; } else { header.cupsColorSpace = CUPS_CSPACE_K; header.cupsColorOrder = CUPS_ORDER_BANDED; } if (page & 2) { header.cupsBytesPerLine *= 2; header.cupsBitsPerColor = 16; header.cupsBitsPerPixel = (page & 1) ? 64 : 16; } else { header.cupsBitsPerColor = 8; header.cupsBitsPerPixel = (page & 1) ? 32 : 8; } cupsRasterWriteHeader2(r, &header); for (y = 0; y < TEST_HEIGHT; y ++) cupsRasterWritePixels(r, data[y & 31], header.cupsBytesPerLine); } cupsRasterClose(r); }