+ /* Parse command-line options. */
+ for (;;) {
+ const struct option opts[] = {
+ { "help", 0, 0, 'h' },
+ { "version", 0, 0, 'v' },
+ { "usage", 0, 0, 'u' },
+ { "daemon", 0, 0, 'D' },
+ { "group", OPTF_ARGREQ, 0, 'G' },
+ { "pidfile", OPTF_ARGREQ, 0, 'P' },
+ { "user", OPTF_ARGREQ, 0, 'U' },
+ { "config", OPTF_ARGREQ, 0, 'c' },
+ { "syslog", 0, 0, 'l' },
+ { "port", OPTF_ARGREQ, 0, 'p' },
+ { 0, 0, 0, 0 }
+ };
+
+ if ((i = mdwopt(argc, argv, "hvuDG:P:U:c:lp:", opts, 0, 0, 0)) < 0)
+ break;
+ switch (i) {
+ case 'h': help(stdout); exit(0);
+ case 'v': version(stdout); exit(0);
+ case 'u': usage(stdout); exit(0);
+ case 'D': f |= f_daemon; break;
+ case 'P': pidfile = optarg; break;
+ case 'c': policyfile = optarg; break;
+ case 'l': flags |= F_SYSLOG; break;
+ case 'G':
+ if (numericp(optarg))
+ g = atoi(optarg);
+ else if ((gr = getgrnam(optarg)) == 0)
+ die(1, "unknown group `%s'", optarg);
+ else
+ g = gr->gr_gid;
+ break;
+ case 'U':
+ if (numericp(optarg))
+ u = atoi(optarg);
+ else if ((pw = getpwnam(optarg)) == 0)
+ die(1, "unknown user `%s'", optarg);
+ else
+ u = pw->pw_uid;
+ break;
+ case 'p':
+ if (numericp(optarg))
+ port = atoi(optarg);
+ else if ((s = getservbyname(optarg, "tcp")) == 0)
+ die(1, "unknown service name `%s'", optarg);
+ else
+ port = ntohs(s->s_port);
+ break;
+ default: f |= f_bogus; break;
+ }
+ }
+ if (optind < argc) f |= f_bogus;
+ if (f & f_bogus) { usage(stderr); exit(1); }
+
+ /* If a user has been requested, but no group, then find the user's primary
+ * group. If the user was given by name, then we already have a password
+ * entry and should use that, in case two differently-named users have the
+ * same uid but distinct gids.
+ */
+ if (u != -1 && g == -1) {
+ if (!pw && (pw = getpwuid(u)) == 0) {
+ die(1, "failed to find password entry for user %d: "
+ "request group explicitly", u);
+ }
+ g = pw->pw_gid;
+ }
+
+ /* Initialize system-specific machinery. */
+ init_sys();
+
+ /* Load the global policy rules. */
+ fwatch_init(&polfw, policyfile);
+ if (load_policy_file(policyfile, &policy))