Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sprintf 3

Status
Not open for further replies.
Nov 12, 2004
44
US
I am not an experienced C programmer by any means, but I need to allow a user to change permissions and the owner on multiple files, and the only way I know how is the code below. Would someone show me a more efficient way? Thanks.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>

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

        int uid;
        char cmd[100];
        char cmda[100];
        char cmdb[100];
        char cmdc[100];
        char cmdd[100];
        char cmde[100];
        char cmdf[100];
        char cmdg[100];
        char cmdh[100];
        uid = getuid();

        if( uid = 273 ) {
                setuid(0);
                setgid(0);

                sprintf(cmd,"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db\n");
                system(cmd);
                sprintf(cmda,"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db\n");
                system(cmda);

                sprintf(cmdb,"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7\n");
                system(cmdb);
                sprintf(cmdc,"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7\n");
                system(cmdc);

                sprintf(cmdd,"chmod 444 "chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db\n");
                system(cmdd);
                sprintf(cmde,"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db\n");
                system(cmde);

                sprintf(cmdf,"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB\n");
                system(cmdf);
                sprintf(cmdg,"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB\n");
                system(cmdg);

                sprintf(cmdh,"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/java/*\n");
                system(cmdh);

        else {
                printf("\007You are not authorized to run this program.  Your UID is %d.\n", uid);
                printf("\007Exiting.\n");
                exit(1);
        }
}
 
11011110000,

You can use _chmod to change permissions on files.

 
Perhaps you didn't read that I am not exprienced in C programming. And since I cannot find anything on google and there isn't a man page for _chmod, it doesn't even begin to help.
 
Okay, I found an example and I got it to work, however, how does _chmod help for changing the owner? and does it work with changing multiple files (i.e., using *)?

Thanks.
 
I can't see any bonus in using sprintf. Try
Code:
system ( "chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db\n" );
system("chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db\n");
system ("chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7\n");
system ( "chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7\n");
system ("chmod 444 "chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db\n");
system("chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db\n");
system ( "chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB\n");
system("chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB\n");
system("chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/java/*\n");

slightly more readable - and hence maintainable - is to define an array
Code:
char cmds[9][] = 
  {
"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db" ,
"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.db",
"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7",
"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/*.prep7",
"chmod 444 "chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db",
"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/owp634/xwascode.db",
"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB",
"chown oracle /`uname -n`/u01/oracle/product/express634/olap/oes634/service/oec634/XPDDCODE.DB",
"chmod 444 /`uname -n`/u01/oracle/product/express634/olap/oes634/owa634/java/*"
 };
for ( i = 0; i < 9; i++ )
  system ( cmds[i] );
I'm not sure if I cut and pasted everything correctly but you get the point

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
Thank you Columb, that worked. Can you explain why my uid statement isn't working? I only want it to run as that uid and any other uid it exits. Thanks.

Code:
        int uid;
        uid = getuid();

        if( uid = 273 ) {
                setuid(0);
                setgid(0);
      .
      .
      .
         }
         else {
                printf("\007You are not authorized to run this program.  Your UID is %d.\n", uid);
        }
 
After compiling use
Code:
chmod 6755 myprog
chown root:sys myprog

In order that the setuid(0) should work the user 'running' the command must be root. You can mimic this by using the suid bit which is set by the chmod 6755. if youthen do an 'ls -l' you should see the permissions set to rwsr-sr-x.

Be warned, your security team may object to programs with the suid bit set. I'm specifically prohibited and have to use sudo or something similar.

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
>if( uid = 273 )
This is assignment

if( uid == 273 )
This is comparison.

--
 
Good point Salem - I was so busy concentrating on file permissions I didn't spot that the code was incorrect.

Columb Healy
Living with a seeker after the truth is infinitely preferable to living with one who thinks they've found it.
 
>if( uid = 273 )
This is assignment

if( uid == 273 )
This is comparison.
A good way to ALWAYS catch this mistake at compile time is to put the constants and literals first.
Code:
if( [red]uid = 273[/red] ) // compiles but doesn't do what you want
[b]...[/b]
if( [red]273 = uid[/red] ) // won't compile, so you know what there 
                //error is and where
[b]...[/b]
if( [green]273 == uid[/green] ) // so to catch typos, 
                 //this works best
 
I find that technique loses more in readability than is gained in error checking. Especially since modern compilers with a decent warning level will diagnose the problem (and many many more) for you.

Besides, if you can remember the swap, how hard can it be to remember == and not =

Not forgetting that [tt] if ( var1 == var2 )[/tt] completely removes the safety net and you have to actually rely on knowing the rules.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top