static const char * /* O - Full path to filter or NULL */ add_fcache(cups_array_t *filtercache, /* I - Filter cache */ const char *name, /* I - Filter name */ const char *filterpath) /* I - Filter path */ { _mime_fcache_t key, /* Search key */ *temp; /* New filter cache */ char path[1024]; /* Full path to filter */ key.name = (char *)name; if ((temp = (_mime_fcache_t *)cupsArrayFind(filtercache, &key)) != NULL) return (temp->path); if ((temp = calloc(1, sizeof(_mime_fcache_t))) == NULL) return (NULL); temp->name = strdup(name); if (cupsFileFind(name, filterpath, 1, path, sizeof(path))) temp->path = strdup(path); cupsArrayAdd(filtercache, temp); return (temp->path); }
static const char * /* O - Full path to filter or NULL */ mime_add_fcache( cups_array_t *filtercache, /* I - Filter cache */ const char *name, /* I - Filter name */ const char *filterpath) /* I - Filter path */ { _mime_fcache_t key, /* Search key */ *temp; /* New filter cache */ char path[1024]; /* Full path to filter */ DEBUG_printf(("2mime_add_fcache(filtercache=%p, name=\"%s\", " "filterpath=\"%s\")", filtercache, name, filterpath)); key.name = (char *)name; if ((temp = (_mime_fcache_t *)cupsArrayFind(filtercache, &key)) != NULL) { DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp->path)); return (temp->path); } if ((temp = calloc(1, sizeof(_mime_fcache_t))) == NULL) { DEBUG_puts("3mime_add_fcache: Returning NULL."); return (NULL); } temp->name = strdup(name); if (cupsFileFind(name, filterpath, 1, path, sizeof(path))) temp->path = strdup(path); cupsArrayAdd(filtercache, temp); DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp->path)); return (temp->path); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int status; /* Exit status */ char filename[1024]; /* Filename buffer */ cups_file_t *fp; /* File pointer */ #ifndef WIN32 int fds[2]; /* Open file descriptors */ cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */ #endif /* !WIN32 */ int count; /* Number of lines in file */ if (argc == 1) { /* * Do uncompressed file tests... */ status = read_write_tests(0); #ifdef HAVE_LIBZ /* * Do compressed file tests... */ putchar('\n'); status += read_write_tests(1); #endif /* HAVE_LIBZ */ /* * Do uncompressed random I/O tests... */ status += random_tests(); #ifndef WIN32 /* * Test fdopen and close without reading... */ pipe(fds); close(fds[1]); fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout); fflush(stdout); if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL) { puts("FAIL"); status ++; } else { /* * Able to open file, now close without reading. If we don't return * before the alarm fires, that is a failure and we will crash on the * alarm signal... */ puts("PASS"); fputs("cupsFileClose(no read): ", stdout); fflush(stdout); alarm(5); cupsFileClose(fdfile); alarm(0); puts("PASS"); } #endif /* !WIN32 */ /* * Count lines in psglyphs, rewind, then count again. */ fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout); if ((fp = cupsFileOpen("../data/media.defs", "r")) == NULL) { puts("FAIL"); status ++; } else { puts("PASS"); fputs("cupsFileGets: ", stdout); if ((count = count_lines(fp)) != 208) { printf("FAIL (got %d lines, expected 208)\n", count); status ++; } else { puts("PASS"); fputs("cupsFileRewind: ", stdout); if (cupsFileRewind(fp) != 0) { puts("FAIL"); status ++; } else { puts("PASS"); fputs("cupsFileGets: ", stdout); if ((count = count_lines(fp)) != 208) { printf("FAIL (got %d lines, expected 208)\n", count); status ++; } else puts("PASS"); } } cupsFileClose(fp); } /* * Test path functions... */ fputs("\ncupsFileFind: ", stdout); #ifdef WIN32 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) && cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename))) #else if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) && cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename))) #endif /* WIN32 */ printf("PASS (%s)\n", filename); else { puts("FAIL"); status ++; } /* * Summarize the results and return... */ if (!status) puts("\nALL TESTS PASSED!"); else printf("\n%d TEST(S) FAILED!\n", status); } else { /* * Cat the filename on the command-line... */ char line[1024]; /* Line from file */ if ((fp = cupsFileOpen(argv[1], "r")) == NULL) { perror(argv[1]); status = 1; } else { status = 0; while (cupsFileGets(fp, line, sizeof(line))) puts(line); if (!cupsFileEOF(fp)) perror(argv[1]); cupsFileClose(fp); } } return (status); }
static int /* O - 1 on success, 0 on failure */ make_certificate(cupsd_client_t *con) /* I - Client connection */ { # ifdef HAVE_SECGENERATESELFSIGNEDCERTIFICATE int status = 0; /* Return status */ OSStatus err; /* Error code (if any) */ # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) char localname[1024];/* Local hostname */ # endif /* HAVE_DNSSD || HAVE_AVAHI */ const char *servername; /* Name of server in cert */ CFStringRef cfservername = NULL; /* CF string for server name */ SecIdentityRef ident = NULL; /* Identity */ SecKeyRef publicKey = NULL, /* Public key */ privateKey = NULL; /* Private key */ CFMutableDictionaryRef keyParams = NULL; /* Key generation parameters */ cupsdLogMessage(CUPSD_LOG_INFO, "Generating SSL server key and certificate."); # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) if (con->servername && isdigit(con->servername[0] & 255) && DNSSDHostName) { snprintf(localname, sizeof(localname), "%s.local", DNSSDHostName); servername = localname; } else # endif /* HAVE_DNSSD || HAVE_AVAHI */ servername = con->servername; cfservername = CFStringCreateWithCString(kCFAllocatorDefault, servername, kCFStringEncodingUTF8); if (!cfservername) goto cleanup; /* * Create a public/private key pair... */ keyParams = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (!keyParams) goto cleanup; CFDictionaryAddValue(keyParams, kSecAttrKeyType, kSecAttrKeyTypeRSA); CFDictionaryAddValue(keyParams, kSecAttrKeySizeInBits, CFSTR("2048")); CFDictionaryAddValue(keyParams, kSecAttrLabel, CFSTR("CUPS Self-Signed Certificate")); err = SecKeyGeneratePair(keyParams, &publicKey, &privateKey); if (err != noErr) { cupsdLogMessage(CUPSD_LOG_DEBUG, "SecKeyGeneratePair returned %ld.", (long)err); goto cleanup; } /* * Create a self-signed certificate using the public/private key pair... */ CFIndex usageInt = kSecKeyUsageAll; CFNumberRef usage = CFNumberCreate(alloc, kCFNumberCFIndexType, &usageInt); CFDictionaryRef certParams = CFDictionaryCreateMutable(kCFAllocatorDefault, kSecCSRBasicContraintsPathLen, CFINT(0), kSecSubjectAltName, cfservername, kSecCertificateKeyUsage, usage, NULL, NULL); CFRelease(usage); const void *ca_o[] = { kSecOidOrganization, CFSTR("") }; const void *ca_cn[] = { kSecOidCommonName, cfservername }; CFArrayRef ca_o_dn = CFArrayCreate(kCFAllocatorDefault, ca_o, 2, NULL); CFArrayRef ca_cn_dn = CFArrayCreate(kCFAllocatorDefault, ca_cn, 2, NULL); const void *ca_dn_array[2]; ca_dn_array[0] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_o_dn, 1, NULL); ca_dn_array[1] = CFArrayCreate(kCFAllocatorDefault, (const void **)&ca_cn_dn, 1, NULL); CFArrayRef subject = CFArrayCreate(kCFAllocatorDefault, ca_dn_array, 2, NULL); SecCertificateRef cert = SecGenerateSelfSignedCertificate(subject, certParams, publicKey, privateKey); CFRelease(subject); CFRelease(certParams); if (!cert) { cupsdLogMessage(CUPSD_LOG_DEBUG, "SecGenerateSelfSignedCertificate failed."); goto cleanup; } ident = SecIdentityCreate(kCFAllocatorDefault, cert, privateKey); if (ident) cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server certificate file \"%s\".", ServerCertificate); /* * Cleanup and return... */ cleanup: if (cfservername) CFRelease(cfservername); if (keyParams) CFRelease(keyParams); if (ident) CFRelease(ident); if (cert) CFRelease(cert); if (publicKey) CFRelease(publicKey); if (privateKey) CFRelease(publicKey); if (!status) cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create SSL server key and certificate."); return (status); # else /* !HAVE_SECGENERATESELFSIGNEDCERTIFICATE */ int pid, /* Process ID of command */ status; /* Status of command */ char command[1024], /* Command */ *argv[4], /* Command-line arguments */ *envp[MAX_ENV + 1], /* Environment variables */ keychain[1024], /* Keychain argument */ infofile[1024], /* Type-in information for cert */ # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) localname[1024], /* Local hostname */ # endif /* HAVE_DNSSD || HAVE_AVAHI */ *servername; /* Name of server in cert */ cups_file_t *fp; /* Seed/info file */ int infofd; /* Info file descriptor */ # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) if (con->servername && isdigit(con->servername[0] & 255) && DNSSDHostName) { snprintf(localname, sizeof(localname), "%s.local", DNSSDHostName); servername = localname; } else # endif /* HAVE_DNSSD || HAVE_AVAHI */ servername = con->servername; /* * Run the "certtool" command to generate a self-signed certificate... */ if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command))) { cupsdLogMessage(CUPSD_LOG_ERROR, "No SSL certificate and certtool command not found."); return (0); } /* * Create a file with the certificate information fields... * * Note: This assumes that the default questions are asked by the certtool * command... */ if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL) { cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create certificate information file %s - %s", infofile, strerror(errno)); return (0); } cupsFilePrintf(fp, "%s\n" /* Enter key and certificate label */ "r\n" /* Generate RSA key pair */ "2048\n" /* Key size in bits */ "y\n" /* OK (y = yes) */ "b\n" /* Usage (b=signing/encryption) */ "s\n" /* Sign with SHA1 */ "y\n" /* OK (y = yes) */ "%s\n" /* Common name */ "\n" /* Country (default) */ "\n" /* Organization (default) */ "\n" /* Organizational unit (default) */ "\n" /* State/Province (default) */ "%s\n" /* Email address */ "y\n", /* OK (y = yes) */ servername, servername, ServerAdmin); cupsFileClose(fp); cupsdLogMessage(CUPSD_LOG_INFO, "Generating SSL server key and certificate."); snprintf(keychain, sizeof(keychain), "k=%s", ServerCertificate); argv[0] = "certtool"; argv[1] = "c"; argv[2] = keychain; argv[3] = NULL; cupsdLoadEnv(envp, MAX_ENV); infofd = open(infofile, O_RDONLY); if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL, NULL, &pid)) { close(infofd); unlink(infofile); return (0); } close(infofd); unlink(infofile); while (waitpid(pid, &status, 0) < 0) if (errno != EINTR) { status = 1; break; } cupsdFinishProcess(pid, command, sizeof(command), NULL); if (status) { if (WIFEXITED(status)) cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create SSL server key and certificate - " "the certtool command stopped with status %d.", WEXITSTATUS(status)); else cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create SSL server key and certificate - " "the certtool command crashed on signal %d.", WTERMSIG(status)); } else { cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server certificate file \"%s\".", ServerCertificate); } return (!status); # endif /* HAVE_SECGENERATESELFSIGNEDCERTIFICATE */ }
static int /* O - 1 on success, 0 on failure */ make_certificate(cupsd_client_t *con) /* I - Client connection */ { int pid, /* Process ID of command */ status; /* Status of command */ char command[1024], /* Command */ *argv[4], /* Command-line arguments */ *envp[MAX_ENV + 1], /* Environment variables */ keychain[1024], /* Keychain argument */ infofile[1024], /* Type-in information for cert */ # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) localname[1024], /* Local hostname */ # endif /* HAVE_DNSSD || HAVE_AVAHI */ *servername; /* Name of server in cert */ cups_file_t *fp; /* Seed/info file */ int infofd; /* Info file descriptor */ # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) if (con->servername && isdigit(con->servername[0] & 255) && DNSSDHostName) { snprintf(localname, sizeof(localname), "%s.local", DNSSDHostName); servername = localname; } else # endif /* HAVE_DNSSD || HAVE_AVAHI */ servername = con->servername; /* * Run the "certtool" command to generate a self-signed certificate... */ if (!cupsFileFind("certtool", getenv("PATH"), 1, command, sizeof(command))) { cupsdLogMessage(CUPSD_LOG_ERROR, "No SSL certificate and certtool command not found!"); return (0); } /* * Create a file with the certificate information fields... * * Note: This assumes that the default questions are asked by the certtool * command... */ if ((fp = cupsTempFile2(infofile, sizeof(infofile))) == NULL) { cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create certificate information file %s - %s", infofile, strerror(errno)); return (0); } cupsFilePrintf(fp, "%s\n" /* Enter key and certificate label */ "r\n" /* Generate RSA key pair */ "2048\n" /* Key size in bits */ "y\n" /* OK (y = yes) */ "b\n" /* Usage (b=signing/encryption) */ "s\n" /* Sign with SHA1 */ "y\n" /* OK (y = yes) */ "%s\n" /* Common name */ "\n" /* Country (default) */ "\n" /* Organization (default) */ "\n" /* Organizational unit (default) */ "\n" /* State/Province (default) */ "%s\n" /* Email address */ "y\n", /* OK (y = yes) */ servername, servername, ServerAdmin); cupsFileClose(fp); cupsdLogMessage(CUPSD_LOG_INFO, "Generating SSL server key and certificate..."); snprintf(keychain, sizeof(keychain), "k=%s", ServerCertificate); argv[0] = "certtool"; argv[1] = "c"; argv[2] = keychain; argv[3] = NULL; cupsdLoadEnv(envp, MAX_ENV); infofd = open(infofile, O_RDONLY); if (!cupsdStartProcess(command, argv, envp, infofd, -1, -1, -1, -1, 1, NULL, NULL, &pid)) { close(infofd); unlink(infofile); return (0); } close(infofd); unlink(infofile); while (waitpid(pid, &status, 0) < 0) if (errno != EINTR) { status = 1; break; } cupsdFinishProcess(pid, command, sizeof(command), NULL); if (status) { if (WIFEXITED(status)) cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create SSL server key and certificate - " "the certtool command stopped with status %d!", WEXITSTATUS(status)); else cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create SSL server key and certificate - " "the certtool command crashed on signal %d!", WTERMSIG(status)); } else { cupsdLogMessage(CUPSD_LOG_INFO, "Created SSL server certificate file \"%s\"...", ServerCertificate); } return (!status); }