- FILE *in, *out;
- int pairme = 0;
- int error_on_write = NO;
- char buf[BUFSIZE];
- char matchbuf[BUFSIZE];
- char temppath[BUFSIZE];
- const char *filename;
- const char *host, *user;
- mode_t oldumask;
- int matchlen;
-
- host = aconf->host;
- user = aconf->user;
-
- ircsnprintf(temppath, sizeof(temppath),
- "%s.tmp", ConfigFileEntry.klinefile);
-
- filename = get_conf_name(KLINE_TYPE);
-
- if((in = fopen(filename, "r")) == 0)
- {
- sendto_one_notice(source_p, ":Cannot open %s", filename);
- return;
- }
-
- oldumask = umask(0);
- if((out = fopen(temppath, "w")) == 0)
- {
- sendto_one_notice(source_p, ":Cannot open %s", temppath);
- fclose(in);
- umask(oldumask);
- return;
- }
-
- umask(oldumask);
-
- snprintf(matchbuf, sizeof(matchbuf), "\"%s\",\"%s\"", user, host);
- matchlen = strlen(matchbuf);
-
- while (fgets(buf, sizeof(buf), in))
- {
- if(error_on_write)
- break;
-
- if(!strncasecmp(buf, matchbuf, matchlen))
- {
- pairme++;
- break;
- }
- else
- error_on_write = flush_write(source_p, out, buf, temppath);
- }
-
- /* we dropped out of the loop early because we found a match,
- * to drop into this somewhat faster loop as we presume we'll never
- * have two matching klines --anfl
- */
- if(pairme && !error_on_write)
- {
- while(fgets(buf, sizeof(buf), in))
- {
- if(error_on_write)
- break;
-
- error_on_write = flush_write(source_p, out, buf, temppath);
- }
- }
-
- fclose(in);
- if (fclose(out))
- error_on_write = YES;
-
- /* The result of the rename should be checked too... oh well */
- /* If there was an error on a write above, then its been reported
- * and I am not going to trash the original kline /conf file
- */
- if(error_on_write)
- {
- sendto_one_notice(source_p, ":Couldn't write temp kline file, aborted");
- return;
- }
- else if(!pairme)
- {
- sendto_one_notice(source_p, ":Cannot find K-Line for %s@%s in file",
- user, host);
-
- if(temppath != NULL)
- (void) unlink(temppath);
-
- return;
- }
-
- if (rename(temppath, filename))
- {
- sendto_one_notice(source_p, ":Couldn't rename temp file, aborted");
- return;
- }
-
- sendto_one_notice(source_p, ":K-Line for [%s@%s] is removed",
- user, host);