{"id":326,"date":"2012-11-23T20:41:25","date_gmt":"2012-11-24T01:41:25","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=326"},"modified":"2012-11-24T21:34:55","modified_gmt":"2012-11-25T02:34:55","slug":"a-simple-su-implementation-in-c","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2012\/11\/23\/a-simple-su-implementation-in-c\/","title":{"rendered":"A simple su implementation in C"},"content":{"rendered":"<p>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 &#8211; 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:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/*\r\n * Simple su binary\r\n * Copyright (C) 2012 Compdigitec\r\n * http:\/\/www.compdigitec.com\/labs\/2012\/11\/23\/a-simple-su-implementation-in-c\/\r\n *\r\n * Redistribution and use in source and binary forms, with or without\r\n * modification, are permitted provided that the following conditions are\r\n * met:\r\n *\r\n * * Redistributions of source code must retain the above copyright\r\n *   notice, this list of conditions and the following disclaimer.\r\n * * Redistributions in binary form must reproduce the above\r\n *   copyright notice, this list of conditions and the following disclaimer\r\n *   in the documentation and\/or other materials provided with the\r\n *   distribution.\r\n * * Neither the name of Compdigitec nor the names of its\r\n *   contributors may be used to endorse or promote products derived from\r\n *   this software without specific prior written permission.\r\n *\r\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r\n * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n *\r\n *\/\r\n\r\n#include &lt;unistd.h&gt;\r\n#include &lt;getopt.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;pwd.h&gt;\r\n\r\n#define SHELL &quot;\/system\/bin\/sh&quot;\r\n\r\n#define about() do { \\\r\n    printf(&quot;Simple crude su binary\\n&quot;); \\\r\n    printf(&quot;See http:\/\/www.compdigitec.com\/labs\/2012\/11\/23\/a-simple-su-implementation-in-c\/\\n&quot;); \\\r\n    printf(&quot;su [-c &lt;command&gt;] [-h] [-v] [user id to su to, must be integer]\\n&quot;); \\\r\n    } while(0)\r\n\r\nint main(int argc, char* argv[]) {\r\n    char* command = NULL;\r\n    int user = 0; \/\/ root\r\n    int group = 0;\r\n\r\n    struct option long_opts[] = {\r\n        { &quot;command&quot;,\trequired_argument,\t\tNULL, 'c' },\r\n        { &quot;help&quot;, \t\tno_argument,\t\t\tNULL, 'h' },\r\n        { &quot;shell&quot;,\t\trequired_argument,\t\tNULL, 's' },\r\n        { &quot;version&quot;,\tno_argument,\t\t\tNULL, 'v' },\r\n        { NULL, 0, NULL, 0 },\r\n    };\r\n\r\n    int c;\r\n    while((c = getopt_long(argc, argv, &quot;+c:hsv&quot;, long_opts, NULL)) != -1) {\r\n        switch(c) {\r\n            case 'c':\r\n                command = strdup(optarg);\r\n                break;\r\n            case 'h':\r\n                about();\r\n                exit(EXIT_SUCCESS);\r\n                break;\r\n            case 'v':\r\n                about();\r\n                exit(EXIT_SUCCESS);\r\n            default:\r\n                fprintf(stderr, &quot;\\n&quot;);\r\n                about();\r\n                exit(EXIT_SUCCESS);\r\n        }\r\n    }\r\n\r\n    if(command == NULL)\r\n        command = strdup(SHELL);\r\n\r\n    if(optind &lt; argc) {\r\n        user = atoi( argv[optind++] );\r\n    }\r\n\r\n    \/\/ Let's get the group ID as well.\r\n    struct passwd* tempPwdPtr;\r\n#if 0\r\n    struct passwd theentry;\r\n    char* buf = malloc(sysconf(_SC_GETGR_R_SIZE_MAX));\r\n    if(getpwuid_r(user, &amp;theentry, buf, sysconf(_SC_GETGR_R_SIZE_MAX), &amp;tempPwdPtr) == 0) {\r\n        group = theentry.pw_gid;\r\n        free(buf);\r\n    }\r\n#else\r\n    if((tempPwdPtr = getpwuid(user)) != NULL) {\r\n        group = tempPwdPtr-&gt;pw_gid;\r\n    }\r\n#endif\r\n\r\n    \/\/printf(&quot;Command: %s, User to run as: %i, GID = %i\\n&quot;,command,user,group);\r\n    setgid(group);\r\n    setuid(user);\r\n    system(command);\r\n\r\n    free(command);\r\n    return 0;\r\n}\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>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 &#8211; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/326"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}