<commit_before>#include <unistd.h>
#include <sys/time.h>

#include "aconfig.h"
#include "nntpd.h"
#include "aprotos.h"

#include "semaphore.h"

#define BUF_SIZE 524288

#define CLEARBUF buffer[0]='\0';

// TODO die() is niet goed 


/* wait for client to finish sending data */
static void wait_finish_client(CLIENT *client, char *buffer, int bufsize)
{
	int i = 1;

	while (client->error == 0
			&& i == 1
			&& readclient(client, buffer, bufsize, 0) != NULL)
	{ 
		if (buffer[0] == '.' && (buffer[1] == '\r' || buffer[1] == '\n'))
			i=0;
	}
}


static int mysystem (char *command)
{
	struct sigaction sigchld;

	/* gotta ignore children because we're a child ourself */ 
	memset(&sigchld,0,sizeof (sigchld));
	sigchld.sa_handler = SIG_IGN;
	sigaction(SIGCHLD, &sigchld, NULL);

	return system(command);
}


/*
 * walk through newsgroup lists and set emailaddress
 * of first moderated newsgroups. return groupname
 * if the group is unknown, NULL if group is valid
 */
static char *check_moderated(char *groups, char **mailto, int *numgroups)
{
	char *p, *g;
	ACTIVE *grp;
	char *tokbuf;
        tokbuf = NULL;
	*numgroups = 0;
	
	p=strtok_r(groups, ",", &tokbuf);
	do {
		(*numgroups)++;

		if ((grp = getgroup(p)) == NULL)
			return p;

		if (grp != NULL && grp->mode == 'm')
		{
			for (g=p; *g!='\0'; g++)
				if (*g == '.') *g = '-';

			*mailto = malloc (strlen (p) + strlen (cfg.ModeratorDomain) + 2);
			if (*mailto == NULL) die ("No room for moderatordomain");

			strcpy (*mailto, p);
			strcat (*mailto, "@");
			strcat (*mailto, cfg.ModeratorDomain);
			return NULL;
		}
	} while ( (p=strtok_r(NULL, ",", &tokbuf)) );

	return p;
}


/*
 * Send the email to moderator address.
 */
static int send_email(CLIENT *client, HLIST *phlist, char *msgid, char *postbuf, int postsize, char *address)
{
	char tmp[19] = "/tmp/nntpsw.XXXXXX";
	int fd;
	char buf[MAX_HEADER + 512];
	char *sysbuf;

	if ((sysbuf=calloc (1, MAX_STRING)) == NULL) 
		die("no room for sysbuf in send_email");

	if ((fd=mkstemp (tmp)) == -1) 
		die("Cant write spool file for moderator");

	/* spool the headers and body first */

	while (phlist != NULL)
	{
		if (phlist->hdr == NULL && phlist->val == NULL)
		{
			syslog(LOG_ERR, "Skipping empty header during post mail command?");
			phlist = phlist->next;
			continue;
		}

		sprintf(buf, "%s %s\r\n", phlist->hdr, phlist->val);
		if ((write(fd, buf, strlen (buf))) == -1)
		{
			writeclient(client, "500 Spooling for moderator failed (headers)");
			return 1;
		}
		phlist = phlist->next;
	}

	sprintf(buf, "To: %s\r\n\r\n", address);

	if ((write(fd, buf, strlen (buf))) == -1)
	{
		writeclient(client, "500 Spooling for moderator failed (separator)");
		return 1;
	}

	if (! write (fd, postbuf, postsize))
	{
		writeclient(client, "500 Spooling for moderator failed (body)");
		disconnect_server(client);
		return 1;
	}

	close (fd);

	/* finally, mail the spooled post */

	sprintf(sysbuf, "%s %s < %s", cfg.MailerPath, address, tmp);

	if (mysystem(sysbuf))
	{
		unlink(tmp);
		free(sysbuf);
		return writeclient(client, MSG_POST_NOMAIL);
	}

	free(sysbuf);
	unlink(tmp);
	return writeclient(client, MSG_POST_MAILED);
}


/*
 * connect to the post server and send the message using ihave command
 * the message is a header list and raw data of the body.
 * return 1 on error 
 */
static int send_ihave(CLIENT *client, HLIST *phlist, char *msgid, char *postbuf, int postsize)
{
	char b[256];
	int oldsock;

	oldsock = client->serversock;<commit_msg>Fix moderated posts for updated libc versions, remove SIGCHILD ignore before system() call.<commit_after>#include <unistd.h>
#include <sys/time.h>

#include "aconfig.h"
#include "nntpd.h"
#include "aprotos.h"

#include "semaphore.h"

#define BUF_SIZE 524288

#define CLEARBUF buffer[0]='\0';

// TODO die() is niet goed 


/* wait for client to finish sending data */
static void wait_finish_client(CLIENT *client, char *buffer, int bufsize)
{
	int i = 1;

	while (client->error == 0
			&& i == 1
			&& readclient(client, buffer, bufsize, 0) != NULL)
	{ 
		if (buffer[0] == '.' && (buffer[1] == '\r' || buffer[1] == '\n'))
			i=0;
	}
}


/*
 * walk through newsgroup lists and set emailaddress
 * of first moderated newsgroups. return groupname
 * if the group is unknown, NULL if group is valid
 */
static char *check_moderated(char *groups, char **mailto, int *numgroups)
{
	char *p, *g;
	ACTIVE *grp;
	char *tokbuf;
        tokbuf = NULL;
	*numgroups = 0;
	
	p=strtok_r(groups, ",", &tokbuf);
	do {
		(*numgroups)++;

		if ((grp = getgroup(p)) == NULL)
			return p;

		if (grp != NULL && grp->mode == 'm')
		{
			for (g=p; *g!='\0'; g++)
				if (*g == '.') *g = '-';

			*mailto = malloc (strlen (p) + strlen (cfg.ModeratorDomain) + 2);
			if (*mailto == NULL) die ("No room for moderatordomain");

			strcpy (*mailto, p);
			strcat (*mailto, "@");
			strcat (*mailto, cfg.ModeratorDomain);
			return NULL;
		}
	} while ( (p=strtok_r(NULL, ",", &tokbuf)) );

	return p;
}


/*
 * Send the email to moderator address.
 */
static int send_email(CLIENT *client, HLIST *phlist, char *msgid, char *postbuf, int postsize, char *address)
{
	char tmp[19] = "/tmp/nntpsw.XXXXXX";
	int fd;
	char buf[MAX_HEADER + 512];
	char *sysbuf;

	if ((sysbuf=calloc (1, MAX_STRING)) == NULL) 
		die("no room for sysbuf in send_email");

	if ((fd=mkstemp (tmp)) == -1) 
		die("Cant write spool file for moderator");

	/* spool the headers and body first */

	while (phlist != NULL)
	{
		if (phlist->hdr == NULL && phlist->val == NULL)
		{
			syslog(LOG_ERR, "Skipping empty header during post mail command?");
			phlist = phlist->next;
			continue;
		}

		sprintf(buf, "%s %s\r\n", phlist->hdr, phlist->val);
		if ((write(fd, buf, strlen (buf))) == -1)
		{
			writeclient(client, "500 Spooling for moderator failed (headers)");
			return 1;
		}
		phlist = phlist->next;
	}

	sprintf(buf, "To: %s\r\n\r\n", address);

	if ((write(fd, buf, strlen (buf))) == -1)
	{
		writeclient(client, "500 Spooling for moderator failed (separator)");
		return 1;
	}

	if (! write (fd, postbuf, postsize))
	{
		writeclient(client, "500 Spooling for moderator failed (body)");
		disconnect_server(client);
		return 1;
	}

	close (fd);

	/* finally, mail the spooled post */

	sprintf(sysbuf, "%s %s < %s", cfg.MailerPath, address, tmp);

	int r = WEXITSTATUS(system(sysbuf));
	if (r);
	{
		syslog(LOG_WARNING, "Can't send email to moderator, sendmail exit status: %d", r);
		unlink(tmp);
		free(sysbuf);
		return writeclient(client, MSG_POST_NOMAIL);
	}

	free(sysbuf);
	unlink(tmp);
	return writeclient(client, MSG_POST_MAILED);
}


/*
 * connect to the post server and send the message using ihave command
 * the message is a header list and raw data of the body.
 * return 1 on error 
 */
static int send_ihave(CLIENT *client, HLIST *phlist, char *msgid, char *postbuf, int postsize)
{
	char b[256];
	int oldsock;

	oldsock = client->serversock;
{'fraction_non_alphanumeric': 0.16543424677395968, 'fraction_numerical': 0.01290416122952008, 'mean_word_length': 3.6671177266576453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380919', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2578, 'n_words': 763}