Compdigitec Labs

« | Home | »

A simple su implementation in C

By admin | November 23, 2012

Sometimes, you might need to keep things simple. Here is a very simple su implementation written in portable C that can be used on almost any architecture – e.g. Embedded Linux, Android, Windows, etc as long as it supports C. It is licensed under the 3-clause BSD licence so any use is OK as long as credit is given:

/*
 * Simple su binary
 * Copyright (C) 2012 Compdigitec
 * http://www.compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following disclaimer
 *   in the documentation and/or other materials provided with the
 *   distribution.
 * * Neither the name of Compdigitec nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>

#define SHELL "/system/bin/sh"

#define about() do { \
    printf("Simple crude su binary\n"); \
    printf("See http://www.compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/\n"); \
    printf("su [-c <command>] [-h] [-v] [user id to su to, must be integer]\n"); \
    } while(0)

int main(int argc, char* argv[]) {
    char* command = NULL;
    int user = 0; // root
    int group = 0;

    struct option long_opts[] = {
        { "command",	required_argument,		NULL, 'c' },
        { "help", 		no_argument,			NULL, 'h' },
        { "shell",		required_argument,		NULL, 's' },
        { "version",	no_argument,			NULL, 'v' },
        { NULL, 0, NULL, 0 },
    };

    int c;
    while((c = getopt_long(argc, argv, "+c:hsv", long_opts, NULL)) != -1) {
        switch(c) {
            case 'c':
                command = strdup(optarg);
                break;
            case 'h':
                about();
                exit(EXIT_SUCCESS);
                break;
            case 'v':
                about();
                exit(EXIT_SUCCESS);
            default:
                fprintf(stderr, "\n");
                about();
                exit(EXIT_SUCCESS);
        }
    }

    if(command == NULL)
        command = strdup(SHELL);

    if(optind < argc) {
        user = atoi( argv[optind++] );
    }

    // Let's get the group ID as well.
    struct passwd* tempPwdPtr;
#if 0
    struct passwd theentry;
    char* buf = malloc(sysconf(_SC_GETGR_R_SIZE_MAX));
    if(getpwuid_r(user, &theentry, buf, sysconf(_SC_GETGR_R_SIZE_MAX), &tempPwdPtr) == 0) {
        group = theentry.pw_gid;
        free(buf);
    }
#else
    if((tempPwdPtr = getpwuid(user)) != NULL) {
        group = tempPwdPtr->pw_gid;
    }
#endif

    //printf("Command: %s, User to run as: %i, GID = %i\n",command,user,group);
    setgid(group);
    setuid(user);
    system(command);

    free(command);
    return 0;
}

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux | 24 Comments »

24 Responses to “A simple su implementation in C”

  1. Exploring and rooting the iPPea TV at Compdigitec Labs Says:
    November 24th, 2012 at 22:22

    […] A simple su implementation in C | […]

  2. sweetwatercornhole Says:
    March 4th, 2022 at 08:16

    … [Trackback]

    […] Read More here on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  3. Latest devops trends Says:
    March 30th, 2022 at 03:09

    … [Trackback]

    […] Find More Information here to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  4. สล็อตวอเลท ไม่มีขั้นต่ำ Says:
    April 9th, 2022 at 04:44

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  5. colt firearms Says:
    April 14th, 2022 at 10:28

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  6. 카지노사이트 Says:
    April 27th, 2022 at 08:24

    … [Trackback]

    […] There you will find 19276 additional Info on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  7. go to my blog Says:
    May 4th, 2022 at 19:10

    … [Trackback]

    […] Read More here on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  8. เช่าเครื่องเสียง Says:
    May 30th, 2022 at 06:43

    … [Trackback]

    […] There you will find 17553 more Information on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  9. Alyssa SALT - best love song Says:
    June 22nd, 2022 at 10:45

    … [Trackback]

    […] Find More on on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  10. real estate san jose Says:
    June 28th, 2022 at 12:03

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  11. buy positive youtube comments Says:
    July 4th, 2022 at 20:32

    … [Trackback]

    […] Find More on to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  12. Vortex optics for sale Says:
    July 23rd, 2022 at 18:27

    … [Trackback]

    […] Read More on on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  13. Best selling guns online Says:
    August 3rd, 2022 at 11:25

    … [Trackback]

    […] There you can find 30985 more Information to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  14. cvv fullz dumps Says:
    September 28th, 2022 at 03:15

    … [Trackback]

    […] Here you will find 70144 additional Information to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  15. Alliant Reloder 19 Smokeless Gun Powder Store Says:
    October 18th, 2022 at 21:23

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  16. sbobet Says:
    October 28th, 2022 at 04:57

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  17. เงินด่วน ออนไลน์ โอนเข้าบัญชี Says:
    November 10th, 2022 at 06:11

    … [Trackback]

    […] Find More here to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  18. 토토굿게임 Says:
    December 22nd, 2022 at 09:42

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  19. check this site out Says:
    February 24th, 2023 at 13:37

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  20. Plantation Shutters Says:
    February 28th, 2023 at 06:01

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  21. visit our website Says:
    March 10th, 2023 at 22:01

    … [Trackback]

    […] Find More Information here to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  22. สล็อตวอเลท Says:
    March 15th, 2023 at 20:02

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  23. http://framed.bz Says:
    March 16th, 2023 at 23:22

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

  24. dfurbabies Says:
    March 23rd, 2023 at 10:18

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2012/11/23/a-simple-su-implementation-in-c/ […]

Comments