I have the following c setuid wrapper:
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
main( int argc, char ** argv ) {
struct passwd *pwd;
char user[] = "cvmfs-test";
pwd = getpwnam(user);
setuid(pwd->pw_uid);
system(argv[1]);
}
I can call my perl script with ./cwrapper perlscript.pl.
I would like to do ./cwrapper perlscript.pl --option1 --option2 --option3 and elaborate all arguments inside the perl script with GetOptions. How should I change my wrapper?
There is also a nice solution which does not need any allocation, is able to deal with arbitrary long commands and does not imply running useless processes because it does not use system. Moreover with the following solution you get the exit code of the spawned process for free.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#define SETUIDUSER "foobar"
int main(int argc, char **argv) {
struct passwd *pwd;
char user[] = SETUIDUSER;
pwd = getpwnam(user);
// success test needed here
setuid(pwd->pw_uid);
// success test needed here
if (argc < 2)
return 1;
execvp(argv[1], &argv[1]);
return 42;
}
Here is a version dealing with a variable number of arguments. Please note that your syscalls should be tested to ensure everything is going OK.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#define CMDMAXLGTH 4096
#define SETUIDUSER "testuser"
int main( int argc, char ** argv ) {
struct passwd *pwd;
char user[] = SETUIDUSER;
char buf[CMDMAXLGTH];
char *p = buf;
int i = 1;
pwd = getpwnam(user);
// success test needed here
setuid(pwd->pw_uid);
// success test needed here
memset (buf, 0, sizeof(buf));
while (argv[i]) {
p += sprintf(p, " %s", argv[i++]);
}
system(buf);
return 0;
}
You should use sprintf to build a character string with your options, then pass this string to system:
char command [100];
sprintf (command, "./cwrapper %s --%s --%s --%s", program_name,option1,option2,
option3);
system(command);
Update: this approach assumes a fixed number of arguments, and looking back at your question, I see that may not be the case.
Ignore the argv[0] because is the name of the c program and use all the other. You can calculate (strlen) the required memory to assemble a new string, malloc() the memory for the new string and then build your new string by concatenating all the argv (strcat). Or for a fixed length approach, follow #dan1111 answer.
Related
When I compile the following code and run strace on it, I can see that it adds two additional elements to the args[] array.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *args[2];
args[0] = "/bin/ls";
args[1] = "-lh";
execve(args[0], args, NULL);
return 1;
}
strace says that this is what is actually being called:
execve("/bin/ls", ["/bin/ls", "-lh", "\340\301\361\267", "\1"], NULL)
You need to add a null ptr to the last element of the argument array. Otherwise execve doesn't know where your array ends.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *args[3];
args[0] = "/bin/ls";
args[1] = "-lh";
args[2] = NULL;
execve(args[0], args, NULL);
return 1;
}
So basically what you are seeing is execv passing random arguments until it found a NULL in the memory you point with the array. Of course it could be crashing as well.
Program 1
In program 1 I have attempted to create the sole environment variable envar putting it in the env array which is passed to the execle function for the environments creation which program 2 will be run in.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int ret;
char envar[] = "Big ol' environment variable ;D";
char *env[2] = {envar, 0};
ret = execle("./exec_test1.1", "exec_test1.1", 0, env);
printf("my prog failed ret = %d", ret);
return 0;
}
Program 2
I intended this code in the same directory to retrieve the environment variable envar on execution and to print it. However I the output in its place is null "memes and dis (null)" I have searched but can't see my mistake. Program two is almost identical to another I found for the same purpose so I assume my mistake is in program one.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
char *envptr = getenv("envar");
printf("memes and dis %s\n", envptr);
return 0;
}
Thanks
You have wrong envar variable format - it must be NAME=VALUE. So fixing program 1 to:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int ret;
char envar[] = "envar=Big ol' environment variable ;D";
char *env[2] = {envar, 0};
ret = execle("./exec_test1.1", "exec_test1.1", 0, env);
printf("my prog failed ret = %d", ret);
return 0;
}
should make it work.
(I also took some liberty and formatted the code to make it more readable)
Here is a sample c code related to stat.h. bits/stat.h that mentioned "Never include <bits/stat.h> directly; use <sys/stat.h> instead.". However struct stat is defined in bits/stat.h, and int __xstat (...) is defined in sys/stat.h. The code won't compile with any one of headers or even both of them. How to make it copiled while only changing the #include ... without changing any one of the functions?
#include <stdio.h>
#include <bits/stat.h>
#include <sys/stat.h>
int stat_1(char *filename, struct stat *stat_buf)
{
return __xstat(1, filename, stat_buf); // extern int __xstat (...) defined in sys/stat.h
}
char * test(const char *filename) {
char *result;
stat stat_buf; // struct stat defined in bits/stat.h
printf("DO something here");
if ( stat_1(filename, &sbuf) == -1 ) {
printf("DO something here");
}
return result;
}
int main() {
const char *fileName = "file.txt";
test(fileName);
return 0;
}
You should be calling stat see https://linux.die.net/man/2/stat. Not __xstat.
Interacting with names that start with __ is almost always a sign you are doing something wrong. They are under the hood implementation things
For stat and its associated struct, you should likely be includeing:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
all this in Linux not windows
hello i want to know how i can change the color of xeyes like we can do in terminal like
xeyes -fg blue
now i want to to do this in c program using path
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <malloc.h>
//#inlcude <windows.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[0], fullPathName);
myArgv[1] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
if i simply call /usr/bin/X11/xeyes it just show eyes
now i am trying to add command like /usr/bin/X11/xeyes-fg but its not working
any suggestion?
You can add onto the argument vector, like this:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
int n = 0;
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[n++], fullPathName);
myArgv[n++] = "-fg";
myArgv[n++] = "blue";
myArgv[n] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
Here is a picture of the result:
Offhand, I would have expected strace to show the file rgb.txt being opened, but do not see this using -f option (assume it happens in the server). The "blue" does show up in a trace, but only in the exec call, e.g.,
execve("/usr/bin/X11/xeyes", ["/usr/bin/X11/xeyes", "-fg", "blue"], [/* 62 vars */]) = 0
I'm trying the write a service that pings the watchdog device using the linux watchdog driver.
In the function named 'LoadConfigurationFile' I pass a pointer to a struct that is defined above. That function then gets a string and stores it at the address of the variable in the struct with a library call(libconfig).
However when I access the variable 'printf("%s\n", options.devicepath);return 1;' instead of printing the contents of configuration file as expected: "/dev/watchdog", the program displays "�/watchdog".
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <linux/types.h>
#include <linux/watchdog.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <libconfig.h>
struct cfgoptions {
const char* devicepath;
};
struct cfgoptions options;
char *confile = "/etc/watchdogd.cfg";
int LoadConfigurationFile(struct cfgoptions *s);
int main(int argc, char *argv[])
{
struct cfgoptions *st_ptr;
st_ptr = &options;
if (LoadConfigurationFile(st_ptr) < 0)
/*exit(EXIT_FAILURE);*/
/**/
printf("%s\n", options.devicepath);return 1;
/**/
int LoadConfigurationFile(struct cfgoptions *s)
{
config_t cfg;
config_init(&cfg);
if (!config_read_file(&cfg, confile) && config_error_file(&cfg) == NULL)
{
fprintf(stderr, "daemon: cannot open configuration file: %s\n", config_error_file(&cfg));
config_destroy(&cfg);
return -1;
} else if (!config_read_file(&cfg, confile)) {
fprintf(stderr, "daemon:%s:%d: %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
config_destroy(&cfg);
return -1;
}
if (!config_lookup_string(&cfg, "watchdog-device", &s->devicepath))
s->devicepath = "/dev/watchdog";
config_destroy(&cfg);
return 0;
}
/etc/watchdogd.cfg:
watchdog-device = "/dev/watchdog"
int config_setting_lookup_string(const config_setting_t *setting,
const char *name, const char **value)
{
config_setting_t *member = config_setting_get_member(setting, name);
if(! member)
return(CONFIG_FALSE);
if(config_setting_type(member) != CONFIG_TYPE_STRING)
return(CONFIG_FALSE);
*value = config_setting_get_string(member);
return(CONFIG_TRUE);
}
config_setting_t *config_setting_get_member(const config_setting_t *setting,
const char *name)
{
if(setting->type != CONFIG_TYPE_GROUP)
return(NULL);
return(__config_list_search(setting->value.list, name, NULL));
}
const char *config_setting_get_string(const config_setting_t *setting)
{
return((setting->type == CONFIG_TYPE_STRING) ? setting->value.sval : NULL);
}
the problem is you're calling config_destroy. That loses all your data allocated during lookups..
— Function: void config_destroy (config_t * config)
These functions initialize and destroy the configuration object config.
config_init() initializes the config_t structure pointed to by config as a new, empty configuration.
config_destroy() destroys the configuration config, deallocating all memory associated with the configuration, but does not attempt to deallocate the config_t structure itself.
Pretty sure on this.
Try getting rid of that line and see what happens.
More: When libconfig allocates memory for the pointers you pass in, it has to keep the memory references around somewhere so it can clean them up later. That's what the cfg object is. It's just a big record keeping object for everything the library has allocated. When you destroy it, the library must free all allocated memory or else it will be leaked forever.