Linux PPP



Disconnection program

This program may be run by any user to make PPP drop the connection and terminate.

It is stored in /etc/ppp/scripts/ppp-off, but I also have a link to it in /usr/local/bin, which is part of my standard path.

Because this program has to kill a process which may not have been started by the same user that is running the disconnection program, it is written in C and made SUID to root. This ensures that the security of the system is not affected, as it would have been if kill had been made SUID to root.

/* ppp-off.c

   Program to kill off a running pppd connection

   There may be more than one connection running.  If an interface name is given on
   the command line, the connection using that device is terminated; otherwise
   ppp0 is terminated.

   This is a program rather than a script so as to enable any user to run it.  This is
   preferable to making kill SUID to root.

*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>

#define MAX_IF_LENGTH	16
#define	PIDFILEMASK	"%s/%s.pid"
#define	PIDDIR		"/var/run"

void kill_pppd(pid_t);

char *progname;

int main(int argc, char **argv) {
	FILE *pidfile;
	char interface[MAX_IF_LENGTH];
	char pidfilename[255];
	int pid = 0;

	/* Check the command line */
	progname = argv[0];
	if (argc > 1)
		strcpy(interface, argv[1]);
	else
		strcpy(interface,"ppp0");
	
	/* Open the file that records the process numbers */
	sprintf(pidfilename,PIDFILEMASK,PIDDIR,interface);
	if ((pidfile = fopen(pidfilename,"r")) == NULL)
	{
		fprintf(stderr,"%s: No pppd process recorded (%s is missing)\n", progname,
			pidfilename);
		exit(1);
	}

	fscanf(pidfile, "%d", &pid);
	kill_pppd(pid);
	return(0);
}

void kill_pppd(pid_t pid) {
	if (kill(pid, SIGINT) == -1) {
		perror(progname);
		exit(2);
	}
}
 

Copy this code into a file called ppp-off.c. To compile it, and prepare it for use, use these commands, while logged in as root:
   cc -o ppp-off -Wall ppp-off.c
   mv ppp-off /etc/ppp/scripts
   chown root /etc/ppp/scripts/ppp-off
   chmod u+s,a+x /etc/ppp/scripts/ppp-off

When pppd starts, it records its PID in a file called /var/run/pppn.pid. This program reads that file to find the PID and sends the process an interrupt signal.

When pppd receives the signal, it drops the connection, cleans up and terminates.



Up to Linux ppp page

Last updated on 13th March 2005 (include files changed) by Oliver Elphick