{"id":43,"date":"2008-01-31T03:08:47","date_gmt":"2008-01-31T00:08:47","guid":{"rendered":"http:\/\/www.held.org.il\/blog\/?p=43"},"modified":"2008-01-31T03:08:47","modified_gmt":"2008-01-31T00:08:47","slug":"switch-to-another-uidgid-in-a-perl","status":"publish","type":"post","link":"http:\/\/www.held.org.il\/blog\/2008\/01\/switch-to-another-uidgid-in-a-perl\/","title":{"rendered":"Switch to another UID\/GID, with Perl"},"content":{"rendered":"<p>Originally I wanted to start by describing Linux' setuid()-like functions, which change the user-id of a running process (and more). <a title=\"[Almost] All about setuid-like system calls in Unix\" href=\"http:\/\/unixpapa.com\/incnote\/setuid.html\">Some reading<\/a> made me realize that this area is too big (but interesting!) for a simple post, and also that I still don't master it..<\/p>\n<p>So I'll focus only a single, simple task: <strong>switching from root to regular user<\/strong> <strong>permissions:<\/strong> when a daemon is being run by <strong>root<\/strong> (i.e. init scripts), for security reasons we want them to \"transform\" to a regular user right asap. In the kernel level we want to do something like <em>setuid<\/em> or <em>setresuid<\/em> (set all the user identifiers: Real, Effective and Saved) to a specific, different UID.<\/p>\n<p>However, we don't talk about the kernel, but about a much higher scripting language.. So let's begin with basics: according to <a title=\"man perlvar\" href=\"http:\/\/www.hmug.org\/man\/1\/perlvar.php\"><em>perlvar<\/em> manpage<\/a>, <em>$&gt;<\/em> (or <em>$EUID<\/em> if using <em>'use English'<\/em>) represents the <strong>Effective<\/strong> User ID (Effective is the User ID which matters permission-wise). \"<em>print $&gt;\"<\/em> perl command would simply print the EUID.<\/p>\n<p>Now for the surprise.. <em>\"$&gt;=44<\/em>\" perl command simply <strong>sets<\/strong> the EUID! Oh, the simplicity \ud83d\ude42<\/p>\n<p>GID can be set in a similar manner, but can't be set <strong>after<\/strong> the the uid is switched (we need the initial root permission for the GID switch).<\/p>\n<p>Enough talking, let the code begin:<br \/>\n<code><br \/>\n#!\/usr\/bin\/perl -w<br \/>\nuse strict;<br \/>\nues English;<br \/>\n$EGID=22;<br \/>\n$EUID=22;<br \/>\nsleep 50; # Sleep so we can have time to run \"ps axo pid,uid,euid,gid,egid\" :)<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Originally I wanted to start by describing Linux&#8217; setuid()-like functions, which change the user-id of a running process (and more). Some reading made me realize that this area is too big (but interesting!) for a simple post, and also that I still don&#8217;t master it.. So I&#8217;ll focus only a single, simple task: switching from [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75,11],"tags":[203,86,121],"_links":{"self":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/43"}],"collection":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}