void gtget_ssl_init(connection_t * conn) { char *clientcert = NULL; char *clientkey = NULL; const char *pers = "gtget"; sslparam_t *ssl = calloc(1, sizeof(sslparam_t)); if (!(conn->flags & GTGET_FLAG_INSECURE)) { char *cacertfile = alloca(strlen(conn->remote->host) + 5); char *servercert = NULL; strcpy(cacertfile, conn->remote->host); strcat(cacertfile, ".pem"); if (!(servercert = tryopen_alt(conn, conn->caFile, cacertfile))) servercert = tryopen("cacerts.pem"); if (!(servercert)) die(conn, "can't open cacert", NULL); if (x509_crt_parse_file(&ssl->cacert, servercert)) die(conn, "error reading cacert", servercert); } /* read and parse the client certificate if provided */ if ((clientcert = tryopen_alt(conn, conn->ccFile, "clientcert.pem"))) { if (!(clientkey = tryopen_alt(conn, conn->ckFile, "clientkey.pem"))) clientkey = clientcert; if (x509_crt_parse_file(&ssl->clicert, clientcert)) { die(conn, "error reading client certificate", clientcert); if (clientkey && pk_parse_public_keyfile(&ssl->pk, clientkey)) die(conn, "error reading client key", clientkey); } write2f("using client cert: %s\n", clientcert); write2f("using client key: %s\n", clientkey); } entropy_init(&ssl->entropy); if (0 != (ctr_drbg_init(&ssl->ctr_drbg, entropy_func, &ssl->entropy, (const unsigned char *)pers, strlen(pers)))) die(conn, "Seeding the random number generator failed", NULL); if (ssl_init(&ssl->ssl)) die(conn, "error initializing SSL", NULL); ssl_set_endpoint(&ssl->ssl, SSL_IS_CLIENT); if ((conn->flags & GTGET_FLAG_INSECURE)) { ssl_set_authmode(&ssl->ssl, SSL_VERIFY_NONE); } ssl_set_ca_chain(&ssl->ssl, &ssl->cacert, NULL, conn->remote->host); ssl_set_authmode(&ssl->ssl, SSL_VERIFY_OPTIONAL); ssl_set_verify(&ssl->ssl, verify_cb, conn); ssl_set_ciphersuites(&ssl->ssl, ssl_list_ciphersuites()); ssl_set_session(&ssl->ssl, &ssl->ssn); ssl_set_rng(&ssl->ssl, ctr_drbg_random, &ssl->ctr_drbg); conn->ssl = ssl; }
char *searchfile(char *file, const char *name, const char *ext, const char *path) { int p = 0; /* position in path */ int len, i, j; if (file == NULL) return NULL; if (name == NULL) return NULL; strcpy(file, name); do { /* 1. attempt to open the file as given */ if(tryopen(file)) return file; if(ext != NULL) { /* 2. strip the extention if present */ len = strlen(file); for(i = len; i > 0; i--) { if (file[i] == '\\' || file[i] == '/') break; if (file[i] == '.') { file[i] = 0; break; } } /* 3. add the default extention */ if(ext[0] != '.') strcat(file, "."); strcat(file, ext); /* 4. try again */ if(tryopen(file)) return file; } if(path != NULL && path[p] != 0) { /* 5. prepend the first path */ for(j = 0; path[p] != ';' && path[p] != 0; p++, j++) { file[j] = path[p]; } if(path[p] == ';') p++; if(file[j-1] != '\\' && file[j-1] != '/') file[j++] = '\\'; file[j] = 0; strcat(file, name); } else break; } while(TRUE); return NULL; }
static int input_fopen(const char *fname) { FILE *fp; u_int p; if (fname[0] == '/' || fname[0] == '.' || fname[0] == '~') { if ((fp = tryopen(NULL, fname)) != NULL) { input_new(fp, NULL, fname); return (0); } } else { for (p = 0; paths[p] != NULL; p++) if ((fp = tryopen(paths[p], fname)) != NULL) { input_new(fp, paths[p], fname); return (0); } } warnx("cannot open '%s'", fname); return (-1); }
void open_extra_files (void) { FILE *ftmp; int c; char *filename; fclose(fparser); filename = (char *) getenv ("BISON_HAIRY"); #ifdef MSDOS char *cp; /* File doesn't exist in current directory; try in INIT directory. */ cp = getenv("INIT"); if (filename == 0 && cp != 0) { filename = malloc(strlen(cp) + strlen(PFILE1) + 2); strcpy(filename, cp); cp = filename + strlen(filename); *cp++ = '/'; strcpy(cp, PFILE1); } #endif fparser= tryopen(filename ? filename : PFILE1, "r"); /* JF change from inline attrs file to separate one */ ftmp = tryopen(attrsfile, "w"); rewind(fattrs); while((c=getc(fattrs))!=EOF) /* Thank god for buffering */ putc(c,ftmp); fclose(fattrs); fattrs=ftmp; fguard = tryopen(guardfile, "w"); }
int main(int argc, char *argv[]) { FILE *input, *output; char fn_in[FILENAME_MAX], fn_out[FILENAME_MAX]; int k, firstarg=1; char *s; /* -n on command line means use /fontname (<FontName>) def output form */ /* -f gives FamilyName */ if (traceflag) { for (k = 0; k < argc; k++) printf("%d\t%s\n", k, argv[k]); } while (firstarg < argc && argv[firstarg][0] == '-') { if (strcmp(argv[firstarg], "-n") == 0) { defineflag = 1; } else if (strncmp(argv[firstarg], "-f", 2) == 0) { if ((s = strchr(argv[firstarg], '=')) != NULL) familyname = s+1; else { firstarg++; familyname = argv[firstarg]; } printf("FamilyName %s\n", familyname); } firstarg++; } if (argc <= firstarg) { /* file name specified ? */ exit(1); } strcpy(fn_in, argv[firstarg]); input = fopen(fn_in, "rb"); if (input == NULL) { strcpy(fn_in, argv[firstarg]); input = tryopen(fn_in, "pfb", "rb"); } if (input == NULL) { strcpy(fn_in, argv[firstarg]); input = tryopen(fn_in, "pfa", "r"); } if (input == NULL) { strcpy(fn_in, argv[firstarg]); input = tryopen(fn_in, "afm", "r"); } if (input == NULL) { strcpy(fn_in, argv[firstarg]); input = tryopen(fn_in, "pfm", "rb"); } if (input == NULL) { /* perror(fn_in); exit(2); */ perror(argv[firstarg]); exit(2); } if (traceflag) printf("Opened %s for input\n", fn_in); if (verboseflag != 0) printf("FILE: %s\n", fn_in); firstarg++; if (argc > firstarg) { outfilename = argv[firstarg]; strcpy(fn_out, outfilename); if ((output = fopen(fn_out, "w")) == NULL) { perror(fn_out); exit(2); } if (traceflag) printf("Opened %s for output\n", fn_out); wrapflag++; } else { output = stdout; if (traceflag) printf("Using stdout for output\n"); } if (scanfile(output, input) != 0) { fclose(output); fclose(input); exit(5); } if (ferror(output) != 0) { perror(fn_out); exit(3); } else fclose(output); fclose(input); return 0; }
/* JF this has been hacked to death. Nowaday it sets up the file names for the output files, and opens the tmp files and the parser */ void openfiles (void) { char *name_base; /*** char *cp; ***/ const char *filename; int base_length; int short_base_length; #ifdef VMS char *tmp_base = "sys$scratch:b_"; #else char *tmp_base = "/tmp/b."; #endif int tmp_len; #ifdef MSDOS tmp_base = getenv ("TMP"); if (tmp_base == 0) tmp_base = ""; strlwr (infile); #endif /* MSDOS */ tmp_len = strlen (tmp_base); if (spec_outfile) { /* -o was specified. The precise -o name will be used for ftable. For other output files, remove the ".c" or ".tab.c" suffix. */ name_base = (char *)spec_outfile; #ifdef MSDOS strlwr (name_base); #endif /* MSDOS */ /* BASE_LENGTH includes ".tab" but not ".c". */ base_length = strlen (name_base); if (!strcmp (name_base + base_length - 2, ".c")) base_length -= 2; /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ short_base_length = base_length; if (!strncmp (name_base + short_base_length - 4, ".tab", 4)) short_base_length -= 4; else if (!strncmp (name_base + short_base_length - 4, "_tab", 4)) short_base_length -= 4; } else if (spec_file_prefix) { /* -b was specified. Construct names from it. */ /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c". */ short_base_length = strlen (spec_file_prefix); /* Count room for `.tab'. */ base_length = short_base_length + 4; name_base = (char *) mallocate (base_length + 1); /* Append `.tab'. */ strcpy (name_base, spec_file_prefix); #ifdef VMS strcat (name_base, "_tab"); #else strcat (name_base, ".tab"); #endif #ifdef MSDOS strlwr (name_base); #endif /* MSDOS */ } else { /* -o was not specified; compute output file name from input or use y.tab.c, etc., if -y was specified. */ name_base = fixed_outfiles ? "y.y" : infile; /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any. */ base_length = strlen (name_base); if (!strcmp (name_base + base_length - 2, ".y")) base_length -= 2; short_base_length = base_length; #ifdef VMS name_base = stringappend(name_base, short_base_length, "_tab"); #else #ifdef MSDOS name_base = stringappend(name_base, short_base_length, "_tab"); #else name_base = stringappend(name_base, short_base_length, ".tab"); #endif /* not MSDOS */ #endif base_length = short_base_length + 4; } finput = tryopen(infile, "r"); filename = getenv ("BISON_SIMPLE"); #ifdef MSDOS /* File doesn't exist in current directory; try in INIT directory. */ cp = getenv("INIT"); if (filename == 0 && cp != 0) { filename = malloc(strlen(cp) + strlen(PFILE) + 2); strcpy(filename, cp); cp = filename + strlen(filename); *cp++ = '/'; strcpy(cp, PFILE); } #endif /* MSDOS */ fparser = tryopen (filename ? filename : PFILE, "r"); if (verboseflag) { #ifdef MSDOS outfile = stringappend(name_base, short_base_length, ".out"); #else /* We used to use just .out if spec_name_prefix (-p) was used, but that conflicts with Posix. */ outfile = stringappend(name_base, short_base_length, ".output"); #endif foutput = tryopen(outfile, "w"); } #ifdef MSDOS actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX")); tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX")); tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX")); tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "deXXXXXX")); #else actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX")); tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX")); tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX")); tmpdefsfile = mktemp(stringappend(tmp_base, tmp_len, "defs.XXXXXX")); #endif /* not MSDOS */ faction = tryopen(actfile, "w+"); fattrs = tryopen(tmpattrsfile,"w+"); ftable = tryopen(tmptabfile, "w+"); if (definesflag) { defsfile = stringappend(name_base, base_length, ".h"); fdefines = tryopen(tmpdefsfile, "w+"); } #ifndef MSDOS unlink(actfile); unlink(tmpattrsfile); unlink(tmptabfile); unlink(tmpdefsfile); #endif /* These are opened by `done' or `open_extra_files', if at all */ if (spec_outfile) tabfile = (char *)spec_outfile; else tabfile = stringappend(name_base, base_length, ".c"); #ifdef VMS attrsfile = stringappend(name_base, short_base_length, "_stype.h"); guardfile = stringappend(name_base, short_base_length, "_guard.c"); #else #ifdef MSDOS attrsfile = stringappend(name_base, short_base_length, ".sth"); guardfile = stringappend(name_base, short_base_length, ".guc"); #else attrsfile = stringappend(name_base, short_base_length, ".stype.h"); guardfile = stringappend(name_base, short_base_length, ".guard.c"); #endif /* not MSDOS */ #endif /* not VMS */ }
void done (int k) { if (faction) fclose(faction); if (fattrs) fclose(fattrs); if (fguard) fclose(fguard); if (finput) fclose(finput); if (fparser) fclose(fparser); if (foutput) fclose(foutput); /* JF write out the output file */ if (k == 0 && ftable) { FILE *ftmp; int c; ftmp=tryopen(tabfile, "w"); rewind(ftable); while((c=getc(ftable)) != EOF) putc(c,ftmp); fclose(ftmp); fclose(ftable); if (definesflag) { ftmp = tryopen(defsfile, "w"); fflush(fdefines); rewind(fdefines); while((c=getc(fdefines)) != EOF) putc(c,ftmp); fclose(ftmp); fclose(fdefines); } } #ifdef VMS if (faction) delete(actfile); if (fattrs) delete(tmpattrsfile); if (ftable) delete(tmptabfile); if (k==0) sys$exit(SS$_NORMAL); sys$exit(SS$_ABORT); #else #ifdef MSDOS if (actfile) unlink(actfile); if (tmpattrsfile) unlink(tmpattrsfile); if (tmptabfile) unlink(tmptabfile); if (tmpdefsfile) unlink(tmpdefsfile); #endif /* MSDOS */ exit(k); #endif /* not VMS */ }
static FILE * opencal(FILE **in) { int fd = -1; int pdes[2]; /* open up calendar file as stdin */ if (fname == NULL) { for (const char **name = defaultnames; *name != NULL; name++) { if ((fd = tryopen(*name, O_RDONLY)) == -1) continue; else break; } if (fd == -1) { if (doall) return NULL; err(EXIT_FAILURE, "Cannot open calendar file"); } } else if ((fd = tryopen(fname, O_RDONLY)) == -1) { if (doall) return NULL; err(EXIT_FAILURE, "Cannot open `%s'", fname); } if (pipe(pdes) == -1) { warn("Cannot open pipe"); return NULL; } switch (fork()) { case -1: /* error */ (void)close(pdes[0]); (void)close(pdes[1]); return NULL; case 0: /* child */ /* set stdin to calendar file */ if (fd != STDIN_FILENO) { (void)dup2(fd, STDIN_FILENO); (void)close(fd); } /* set stdout to pipe input */ if (pdes[1] != STDOUT_FILENO) { (void)dup2(pdes[1], STDOUT_FILENO); (void)close(pdes[1]); } (void)close(pdes[0]); if (doall) { /* become the user properly */ changeuser(); } /* tell CPP to only open regular files */ if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1) == -1) err(EXIT_FAILURE, "Cannot restrict cpp"); cpp_restricted = true; (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.", "-I" _PATH_CALENDARS, NULL); err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_CPP); /*NOTREACHED*/ default: /* parent -- fdopen *in to pipe output */ *in = fdopen(pdes[0], "r"); (void)close(pdes[1]); /* close calendar file */ close(fd); /* not reading all calendar files, just set output to stdout */ if (!doall) return stdout; /* * Set output to a temporary file, so if no output * don't send mail. */ (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); if ((fd = mkstemp(path)) == -1) { warn("Cannot create temporary file"); return NULL; } return fdopen(fd, "w+"); } /*NOTREACHED*/ }