int main(int argc, char **argv) { int opt; while ((opt = getopt(argc, argv, "b:c:a:f:s:?")) != -1) switch (opt) { case 'b': split_address(optarg, &bind_host, &bind_port); break; case 'c': split_address(optarg, &connect_host, &connect_port); break; case 'a': split_address(optarg, &application_host, &application_port); break; case 'f': split_address(optarg, &filter_host, &filter_port); break; case 's': state_file = optarg; break; default: fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]); fprintf(stderr, " -b HOST:[PORT] " "Bind to the specified address.\n"); fprintf(stderr, " -c HOST:[PORT] " "Connect to the specified address.\n"); fprintf(stderr, " -a HOST:[PORT] " "Receive updates at the specified address.\n"); fprintf(stderr, " -f HOST:[PORT] " "Send updates to the specified address.\n"); fprintf(stderr, " -s FILE " "Keep persistent state in FILE.\n"); fprintf(stderr, " -? " "Print this help message and exit.\n"); exit(EXIT_FAILURE); } if (!connect_port && connect_host) connect_port = "8888"; if (!bind_port && (bind_host || !connect_port)) bind_port = "8888"; if (filter_host || filter_port) { filtering = 1; if (!filter_port) filter_port = application_port ? application_port : "7777"; if (!application_port) application_port = filter_port; if (!application_host) application_host = bind_host; if (!state_file) state_file = "tcpr-application.dat"; setup_state(); setup_update_connection(); } if (recovering) recover_connection(); else setup_connection(); finish(); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int eflag = 0, dflag = 0, nflag = 0; int uflag = 0, vflag = 0, Eflag = 0; int status, ch, fd; char *input = NULL, *output = NULL; DBM *db; datum key, val; DBM *new_db = NULL; static char mapname[] = "ypdbXXXXXXXXXX"; char db_mapname[PATH_MAX], db_outfile[PATH_MAX]; char db_tempname[PATH_MAX]; char user[4096], host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */ char myname[HOST_NAME_MAX+1], datestr[11], *slash; while ((ch = getopt(argc, argv, "Edensuv")) != -1) switch (ch) { case 'E': eflag = 1; /* Check hostname */ Eflag = 1; /* .. even check MX records */ break; case 'd': dflag = 1; /* Don't check DNS hostname */ break; case 'e': eflag = 1; /* Check hostname */ break; case 'n': nflag = 1; /* Capitalize name parts */ break; case 's': /* Ignore */ break; case 'u': uflag = 1; /* Don't check UUCP hostname */ break; case 'v': vflag = 1; /* Verbose */ break; default: usage(); break; } if (optind == argc) usage(); input = argv[optind++]; if (optind < argc) output = argv[optind++]; if (optind < argc) usage(); db = ypdb_open(input, O_RDONLY, 0444); if (db == NULL) { err(1, "Unable to open input database %s", input); /* NOTREACHED */ } if (output != NULL) { if (strlen(output) + strlen(YPDB_SUFFIX) > PATH_MAX) { errx(1, "%s: file name too long", output); /* NOTREACHED */ } snprintf(db_outfile, sizeof(db_outfile), "%s%s", output, YPDB_SUFFIX); slash = strrchr(output, '/'); if (slash != NULL) slash[1] = 0; /* truncate to dir */ else *output = 0; /* eliminate */ /* note: output is now directory where map goes ! */ if (strlen(output) + strlen(mapname) + strlen(YPDB_SUFFIX) > PATH_MAX) { errx(1, "%s: directory name too long", output); /* NOTREACHED */ } snprintf(db_tempname, sizeof(db_tempname), "%s%s%s", output, mapname, YPDB_SUFFIX); fd = mkstemps(db_tempname, 3); if (fd == -1) goto fail; close(fd); strncpy(db_mapname, db_tempname, strlen(db_tempname) - 3); db_mapname[sizeof(db_mapname) - 1] = '\0'; new_db = ypdb_open(db_mapname, O_RDWR|O_TRUNC, 0444); if (new_db == NULL) { fail: if (fd != -1) unlink(db_tempname); err(1, "Unable to open output database %s", db_outfile); /* NOTREACHED */ } } for (key = ypdb_firstkey(db); key.dptr != NULL; key = ypdb_nextkey(db)) { val = ypdb_fetch(db, key); if (val.dptr == NULL) continue; /* No value */ if (*key.dptr == '@' && key.dsize == 1) continue; /* Sendmail token */ if (strncmp(key.dptr, "YP_", 3)==0) /* YP token */ continue; if (memchr(val.dptr, ',', val.dsize)) continue; /* List... */ if (memchr(val.dptr, '|', val.dsize)) continue; /* Pipe... */ if (!(memchr(val.dptr, '@', val.dsize) || memchr(val.dptr, '!', val.dsize))) continue; /* Skip local users */ split_address(val.dptr, val.dsize, user, host); if (eflag && check_host(val.dptr, val.dsize, host, dflag, uflag, Eflag)) { warnx("Invalid host %s in %*.*s:%*.*s", host, key.dsize, key.dsize, key.dptr, val.dsize, val.dsize, val.dptr); continue; } if (nflag) capitalize(key.dptr, key.dsize); if (new_db != NULL) { status = ypdb_store(new_db, val, key, YPDB_INSERT); if (status != 0) { warnx("problem storing %*.*s %*.*s", val.dsize, val.dsize, val.dptr, key.dsize, key.dsize, key.dptr); } } if (vflag) { printf("%*.*s --> %*.*s\n", val.dsize, val.dsize, val.dptr, key.dsize, key.dsize, key.dptr); } } if (new_db != NULL) { snprintf(datestr, sizeof datestr, "%010lld", (long long)time(NULL)); key.dptr = YP_LAST_KEY; key.dsize = strlen(YP_LAST_KEY); val.dptr = datestr; val.dsize = strlen(datestr); status = ypdb_store(new_db, key, val, YPDB_INSERT); if (status != 0) { warnx("problem storing %*.*s %*.*s", key.dsize, key.dsize, key.dptr, val.dsize, val.dsize, val.dptr); } } if (new_db != NULL) { gethostname(myname, sizeof(myname)); key.dptr = YP_MASTER_KEY; key.dsize = strlen(YP_MASTER_KEY); val.dptr = myname; val.dsize = strlen(myname); status = ypdb_store(new_db, key, val, YPDB_INSERT); if (status != 0) { warnx("problem storing %*.*s %*.*s", key.dsize, key.dsize, key.dptr, val.dsize, val.dsize, val.dptr); } } ypdb_close(db); if (new_db != NULL) { ypdb_close(new_db); if (rename(db_tempname, db_outfile) < 0) { err(1, "rename %s -> %s failed", db_tempname, db_outfile); /* NOTREACHED */ } } return(0); }
int main(int argc, char **argv) { int opt; while ((opt = getopt(argc, argv, "b:c:a:f:s:p?")) != -1) switch (opt) { case 'b': split_address(optarg, &bind_host, &bind_port); break; case 'c': split_address(optarg, &connect_host, &connect_port); break; case 'a': application_path = optarg; break; case 'f': filter_path = optarg; break; case 's': state_file = optarg; break; case 'p': filtering = 0; break; default: fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]); fprintf(stderr, " -b HOST:[PORT] " "Bind to HOST at PORT.\n"); fprintf(stderr, " -c HOST:[PORT] " "Connect to HOST at PORT.\n"); fprintf(stderr, " -a PATH " "Receive updates at the UNIX socket PATH.\n"); fprintf(stderr, " -f PATH " "Send updates to the UNIX socket PATH.\n"); fprintf(stderr, " -s FILE " "Keep persistent state in FILE.\n"); fprintf(stderr, " -p " "Act as the peer; i.e. ignore TCPR.\n"); fprintf(stderr, " -? " "Print this help message and exit.\n"); exit(EXIT_FAILURE); } if (!connect_port && connect_host) connect_port = "8888"; if (!bind_port && (bind_host || !connect_port)) bind_port = "8888"; if (filtering) { setup_state(); setup_update_connection(); } if (recovering) recover_connection(); else setup_connection(); finish(); return EXIT_SUCCESS; }