···11#include <unistd.h>
22#include <stdio.h>
33+#include <string.h>
3444-int main(int argc, char** argv)
55-{
66- int firstarg = 1;
55+unsigned int firstarg = 1, uid = 0, gid = 0;
7688- if (argc <= 1)
99- {
1010- fprintf(stderr, "This is Darling fake sudo.\n"
1111- "Processes will think they run as UID/GID 0, but Linux kernel will still see your original UID.\n"
1212- "The purpose is to convince some tools that they can write into / or enable you to talk to certain system daemons\n.");
1313- return 1;
1414- }
77+int argparse(int, char**);
88+int usage(int, int);
1591616- setuid(0);
1717- setgid(0);
1010+int main(int argc, char **argv)
1111+{
1212+ if ( ( argparse(argc, argv) || usage(argc, 0) ) == 1 )
1313+ return 1;
18141919- if (strcmp(argv[1], "-k") == 0)
2020- firstarg++;
1515+ setuid(uid);
1616+ setgid(gid);
21172218 execvp(argv[firstarg], &argv[firstarg]);
2323- perror("Cannot execute:");
1919+ perror("Error");
2420 return 1;
2521}
26222323+int argparse(int argc, char **argv)
2424+{
2525+ while( (argv[firstarg] != NULL) && (!strncmp(argv[firstarg], "-", 1)) )
2626+ {
2727+ if(!strcmp(argv[firstarg], "-g"))
2828+ {
2929+ sscanf(argv[++firstarg], "%u", &gid);
3030+ firstarg++;
3131+ }
3232+ else if(!strcmp(argv[firstarg], "-u"))
3333+ {
3434+ sscanf(argv[++firstarg], "%u", &uid);
3535+ firstarg++;
3636+ }
3737+ else if(!strcmp(argv[firstarg], "--"))
3838+ {
3939+ firstarg++;
4040+ break;
4141+ }
4242+ else if(!strcmp(argv[firstarg], "-k"))
4343+ firstarg++;
4444+ else if(!strcmp(argv[firstarg], "--help"))
4545+ return usage(argc, 1);
4646+ else
4747+ {
4848+ fprintf(stderr, "Unknown option: %s\n", argv[firstarg]);
4949+ return usage(argc, 1);
5050+ }
5151+ }
5252+ return 0;
5353+}
5454+5555+int usage(int argc, int arg_help)
5656+{
5757+ if (argc <= (int)firstarg || arg_help == 1)
5858+ {
5959+ fprintf(stderr, "This is a fake 'sudo' implementation, intended for use in Darling.\n"
6060+ "Processes will think they are run as UID/GID 0, but they are still run as your original UID/GID.\n"
6161+ "One purpose of this program is to convince some tools that they can write into '/'.\n"
6262+ "Another is to enable you to talk to certain system daemons.\n"
6363+ "\nUsage:\n"
6464+ " sudo [-g GID] [-u UID] [-k] [--] COMMAND\n");
6565+ return 1;
6666+ }
6767+ return 0;
6868+}