C Programming Help

Discuss Programming

C Programming Help

Postby X11 » Wed Oct 20, 2004 10:20 am

I am a little rusty in C, and rather ameture, I dont know a _whole_ lot about it.

I wrote a program, and it wont compile, the reason is probably obvious, I havnt even coded in C in ages, I wrote this in C (or tried to) mainly because it has to run as suid root.

Code: Select all
/*
 *   pppfix by John Tate
 *   This program checks if a ppp interface is up, if not, it kills
 *   some processes. This is because I often find I cannot connect
 *   because even after disconnection, these processes still exist
 *   and lock up the modem device, when I attempt to reconnect it
 *   hangs and never connects, or outputs an error.
 *
 *   This Program is under the GNU General Public Licence
 */

#include <stdlib.h>
#include <stdio.h>

int checkdev(char interface[16])
{
   char ifcfgpath[45]="/sbin/ifconfig";
   char chkcmd[64]="";

   //make a pretty string
   strcpy(ifcfgpath,chkcmd);
   strcat(chkcmd," ");
   strcat(chkcmd,interface);
   // the chkcmd string will contain somthing like "/sbin/ifconfig ppp0" now

        if ( system(chkcmd) )
   {
           system("/usr/bin/killall /sbin/ppp-watch");
           system("/usr/bin/killall pppd");

      return 1;
   } else {
      return 0;
   }
}

int main(int argc, char *argv[])
{
   char interface[16];

   // this should provide protection agaisnt overflowing the string and doing anything nasty
   strncpy(argv[1], interface, 16);

   // check if program is running as UID 0 (root)
   if ( getuid() != 0 )
   {
      printf("pppfix: must be run as user root\n");
      return 0;
   }

   if ( !checkdev(interface) )
   {
      printf("pppfix: interface %s active, no procs killed\n". interface);
      return 1;
   }

   return 0;
}
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby worker201 » Wed Oct 20, 2004 2:58 pm

Surely your compiler provides you with error messages! Those can be pretty useful in debugging sometimes.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby caveman » Wed Oct 20, 2004 4:03 pm

Hi.

Just copied your code and compiled it using cc.

a) You need to have the follwing #includes in your code.
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
b) in your "printf" statement..... NOTE
replace the "." dot with a "," comma before the identifier "interface".

Hint - if you use a function and the compiler gives errors eg..
`strcpy' undeclared.....
Use "man strcpy" - it should give you the #includes, call parameters etc.
that you will need.
<edit>
Make sure the man pages was installed correctly - then most "C" and "C++"
functions etc. are available using "man".
</edit>
caveman
programmer
programmer
 
Posts: 130
Joined: Sun Feb 09, 2003 1:08 pm
Location: Midrand Gauteng, South Africa

Postby X11 » Wed Oct 20, 2004 10:19 pm

I ment to post compiler errors, however I strangly forgot. (Sometimes I wish that I could change, but I cant save you from my poor brain).

Thanks for helping the terminally rusty caveman.

The original compiler errors...
Code: Select all
[x11@exeleven pppfix]$ gcc -o pppfix pppfix.c
pppfix.c: In function `main':
pppfix.c:53: error: request for member `interface' in something not a structure or union


After adding those includes still produces this error, and yea, I did use man pages. I probably should have looked a little more closely.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Wed Oct 20, 2004 10:49 pm

the problem was this mainly, now im getting somewhere

printf("pppfix: interface %s active, no procs killed\n". interface);

there was a full stop instead of a comma, someone just spotted it for me

w00t.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Wed Oct 20, 2004 11:05 pm

I changed some things, which I think are better, because system() didnt seem to return the program's return status.

Code: Select all
[x11@exeleven pppfix]$ gcc -o pppfix pppfix.c
pppfix.c: In function `checkdev':
pppfix.c:22: warning: passing arg 2 of `execv' from incompatible pointer type


Code: Select all
/*
 *      pppfix by John Tate
 *      This program checks if a ppp interface is up, if not, it kills
 *      some processes. This is because I often find I cannot connect
 *      because even after disconnection, these processes still exist
 *      and lock up the modem device, when I attempt to reconnect it
 *      hangs and never connects, or outputs an error.
 *
 *      This Program is under the GNU General Public Licence
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
 
int checkdev(char interface[16])
{
        char ifcfgpath[45]="/sbin/ifconfig";
 
        if ( execv(ifcfgpath,interface) )
        {
                system("/usr/bin/killall /sbin/ppp-watch");
                system("/usr/bin/killall pppd");
 
                return 1;
        } else {
                return 0;
        }
}
 
int main(int argc, char *argv[])
{
        char interface[16];
 
        // this should provide protection agaisnt overflowing the string and doi ng anything nasty
        strncpy(argv[1], interface, 16);
 
        // check if program is running as UID 0 (root)
        if ( getuid() != 0 )
        {
                printf("pppfix: must be run as user root\n");
                return 0;
        }
 
        if ( !checkdev(interface) )
        {
                printf("pppfix: interface %s active, no procs killed\n", interfa ce);
                return 1;
        }
 
        return 0;
}


Code: Select all
[x11@exeleven pppfix]$ sudo ./pppfix ppp0
HOSTNAME=exeleven.home: Unknown host
ifconfig: `--help' gives usage information.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Wed Oct 20, 2004 11:16 pm

Code: Select all
ifconfig: `--help' gives usage information.
[x11@exeleven pppfix]$ cat pppfix.c
/*
 *      pppfix by John Tate
 *      This program checks if a ppp interface is up, if not, it kills
 *      some processes. This is because I often find I cannot connect
 *      because even after disconnection, these processes still exist
 *      and lock up the modem device, when I attempt to reconnect it
 *      hangs and never connects, or outputs an error.
 *
 *      This Program is under the GNU General Public Licence
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
 
int checkdev(char interface[16])
{
        char ifcfgpath[45]="/sbin/ifconfig";
 
        if ( execv(ifcfgpath, &interface) )
        {
                system("/usr/bin/killall /sbin/ppp-watch");
                system("/usr/bin/killall pppd");
 
                return 1;
        } else {
                return 0;
        }
}
 
int main(int argc, char *argv[])
{
        char interface[16];
 
        // this should provide protection agaisnt overflowing the string and doing anything nasty
        strncpy(argv[1], interface, 16);
 
        // check if program is running as UID 0 (root)
        if ( getuid() != 0 )
        {
                printf("pppfix: must be run as user root\n");
                return 0;
        }
 
        if ( !checkdev(interface) )
        {
                printf("pppfix: interface %s active, no procs killed\n", interface);
                return 1;
        }
 
        return 0;
}


compiles without error.

Code: Select all
[x11@exeleven pppfix]$ sudo ./pppfix ppp0
���P7�: Unknown host
ifconfig: `--help' gives usage information.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Fri Oct 29, 2004 12:14 am

Ok looks like I had made a lot of mistakes and misconeceptions...

This code works perfectly.

However because the script is suid root (because its originally ran from a PHP script that people can use to dial the connection, and if it fails, it runs this program and attempts to kill the processes that could be locking it up. Then if that fails it assumes it is somthing else and users are put to a redirect page.

Code: Select all
/*
 *      pppfix by John Tate
 *      This program checks if a ppp interface is up, if not, it kills
 *      some processes. This is because I often find I cannot connect
 *      because even after disconnection, these processes still exist
 *      and lock up the modem device, when I attempt to reconnect it
 *      hangs and never connects, or outputs an error.
 *
 *      This Program is under the GNU General Public Licence
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
 
int checkdev(char interface[16])
{
        char ifcfgpath[45]="/sbin/ifconfig ";
        char ifchkcmd[75]="";
 
        strncpy(ifchkcmd, ifcfgpath, strlen(ifcfgpath));
        strcat(ifchkcmd, interface);
        strcat(ifchkcmd, " > /dev/null");
 
        int ifcfg_ret = system(ifchkcmd);
 
        printf("pppfix: checkdev: ifchkcmd=\"%s\" ifcfg_ret=%i\n", ifchkcmd, ifcfg_ret);
        if ( ifcfg_ret == 256 )
        {
                system("/usr/bin/killall /sbin/ppp-watch");
                system("/usr/bin/killall pppd");
                return 1;
        }
 
        if ( ifcfg_ret == 0 )
        {
                return 0;
        }
 
        return 0;
}
int main(int argc, char *argv[])
{
        char interface_buffer[16];
        char interface[16];
 
        // this should provide protection agaisnt overflowing the string and doing anything nasty
        strncpy(interface_buffer,argv[1], 16);
         
        if ( strlen(interface_buffer) <= 16 )
        {
                strncat(interface, interface_buffer, strlen(interface_buffer));
        } else {
                printf("pppfix: interface argument exceeds buffer");
                return 1;
        }
         
        // check if program is running as UID 0 (root)
        if ( getuid() != 0 )
        {
                printf("pppfix: must be run as user root\n");
                return 0;
        }
 
        if ( !checkdev( interface) )
        {
                printf("pppfix: ifconfig returned with no/unknown value, procs not killed\n", interface);
                return 1;
        }
 
        return 0;
}


X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests