if (errno == ENOENT)
if (errno == ENOENT) { ret = 0;
changefilter(int add, char *luser, char *ipsrc){	char rulesfile[MAXPATHLEN], natfile[MAXPATHLEN], buf[1024];	char template[] = "/tmp/authpfrules.XXXXXXX";	char template2[] = "/tmp/authpfnat.XXXXXXX";	int tmpfile = -1, from_fd = -1, ret = -1;	struct pfioc_nat	pn;	struct pfioc_binat	pb;	struct pfioc_rdr	pd;	struct pfioc_rule	pr;	struct pfctl		pf;	int rcount, wcount;	FILE *fin = NULL;	memset(&pf, 0, sizeof(pf));	memset(&pr, 0, sizeof(pr));	syslog(LOG_DEBUG, "%s filter for ip=%s, user %s",	    add ? "Adding" : "Removing", ipsrc, luser);	/* add filter rules */	if (add)		Delete_Rules = 0;	else		Delete_Rules = 1;	tmpfile = mkstemp(template);	if (tmpfile == -1) {		syslog(LOG_ERR, "Can't open temp file %s (%m)",		    template);		goto error;	}	fin = fdopen(tmpfile, "r+");	if (fin == NULL) {		syslog(LOG_ERR, "can't open %s (%m)", template);		goto error;	}	/* write the variable to the start of the file */	fprintf(fin, "user_ip = \"%s\"\n", ipsrc);	fflush(fin);	if (snprintf(rulesfile, sizeof rulesfile, "%s/%s/authpf.rules",	    PATH_USER_DIR, luser) >= sizeof rulesfile) {		syslog(LOG_ERR, "homedir path too long, exiting");		goto error;	}	if ((from_fd = open(rulesfile, O_RDONLY, 0)) == -1) {		/* if home dir rules do not exist, we try PATH_PFRULES */		if (errno != ENOENT) {			syslog(LOG_ERR, "can't open %s (%m)", rulesfile);			if (unlink(template) == -1)				syslog(LOG_ERR, "can't unlink %s", template);			goto error;		}	}	if (from_fd == -1) {		snprintf(rulesfile, sizeof rulesfile, PATH_PFRULES);		if  ((from_fd = open(rulesfile, O_RDONLY, 0)) == -1) {			syslog(LOG_ERR, "can't open %s (%m)", rulesfile);			if (unlink(template) == -1)				syslog(LOG_ERR, "can't unlink %s", template);			goto error;		}	}	while ((rcount = read(from_fd, buf, sizeof(buf))) > 0) {		wcount = write(tmpfile, buf, rcount);		if (rcount != wcount || wcount == -1) {			syslog(LOG_ERR, "rules template copy failed");			if (unlink(template) == -1)				syslog(LOG_ERR, "can't unlink %s", template);			goto error;		}	}	if (rcount == -1) {		syslog(LOG_ERR, "read of rules template failed");		if (unlink(template) == -1)			syslog(LOG_ERR, "can't unlink %s", template);		goto error;	}	fclose(fin);	fin = NULL;	close(tmpfile);	tmpfile = -1;	close(from_fd);	from_fd = -1;	fin = fopen(template, "r");	if (fin == NULL) {		syslog(LOG_ERR, "can't open %s (%m)", template);		if (unlink(template) == -1)			syslog(LOG_ERR, "can't unlink %s", template);		goto error;	}	infile = template;	if (unlink(template) == -1) {		syslog(LOG_ERR, "can't unlink %s", template);		goto error;	}	/* add/delete rules, using parse_rule */	memset(&pf, 0, sizeof(pf));	pf.dev = dev;	pf.prule = &pr;	if (parse_rules(fin, &pf) < 0) {		syslog(LOG_ERR,		    "syntax error in rule file: authpf rules not loaded");		goto error;	}	if (snprintf(natfile, sizeof natfile, "%s/%s/authpf.nat",	    PATH_USER_DIR, luser) >= sizeof natfile) {		syslog(LOG_ERR, "homedir path too long, exiting");		goto error;	}	if ((from_fd = open(natfile, O_RDONLY, 0)) == -1) {		/* if it doesn't exist, we try /etc */		if (errno != ENOENT) {			syslog(LOG_ERR, "can't open %s (%m)", natfile);			if (unlink(template) == -1)				syslog(LOG_ERR, "can't unlink %s", template);			goto error;		}	}	if (from_fd == -1) {		snprintf(natfile, sizeof natfile, PATH_NATRULES);		if ((from_fd = open(natfile, O_RDONLY, 0)) == -1) {			if (errno == ENOENT)				goto out; /* NAT is optional */			else {				syslog(LOG_ERR, "can't open %s (%m)", natfile);				if (unlink(template) == -1)					syslog(LOG_ERR, "can't unlink %s",					    template);				goto error;			}		}	}	tmpfile = mkstemp(template2);	if (tmpfile == -1) {		syslog(LOG_ERR, "Can't open temp file %s (%m)",		    template2);		goto error;	}	fin = fdopen(tmpfile, "r+");	if (fin == NULL) {		syslog(LOG_ERR, "Can't open %s (%m)", template2);		goto error;	}	/* write the variable to the start of the file */	fprintf(fin, "user_ip = \"%s\"\n", ipsrc);	fflush(fin);	while ((rcount = read(from_fd, buf, sizeof(buf))) > 0) {		wcount = write(tmpfile, buf, rcount);		if (rcount != wcount || wcount == -1) {			syslog(LOG_INFO, "nat copy failed");			goto error;		}	}	if (rcount == -1) {		syslog(LOG_INFO, "read for nat copy failed");		goto error;	}	fclose(fin);	fin = NULL;	close(tmpfile);	tmpfile = -1;	close(from_fd);	from_fd = -1;	fin = fopen(template2, "r");	if (fin == NULL) {		syslog(LOG_INFO, "can't open %s (%m)", template2);		goto error;	}	infile = template;	if (unlink(template2) == -1) {		syslog(LOG_INFO, "can't unlink %s (%m)", template2);		goto error;	}	/* add/delete rules, using parse_nat */	memset(&pf, 0, sizeof(pf));	pf.dev = dev;	pf.pnat = &pn;	pf.pbinat = &pb;	pf.prdr = &pd;	if (parse_nat(fin, &pf) < 0) {		syslog(LOG_INFO,		    "syntax error in nat file: nat rules not loaded");		goto error;	}	ret = 0;	goto out; error:	ret = -1; out:	if (fin != NULL)		fclose(fin);	if (tmpfile != -1)		close(tmpfile);	if (from_fd != -1)		close(from_fd);	if (add) {		(void)gettimeofday(&Tstart, NULL);		syslog(LOG_INFO, "Allowing %s, user %s", ipsrc, luser);	} else {		(void)gettimeofday(&Tend, NULL);		syslog(LOG_INFO, "Removed %s, user %s - duration %ld seconds",		    ipsrc, luser, Tend.tv_sec - Tstart.tv_sec);	}	return(ret);}