Calling Perl Gurus: Need some help for porting a small C code chunk to Perl

Supra

Skilled
Hi as the thread title suggets....I need to implement the following function in perl.

Being a PERL uneducated...I have very sparse idea on that...I am attaching the snippet.

Can anyone help with this...will be highly appreciated.

int

check_admin_role(char *rem_usr)

{

char *tok;

char *ptrbuf;

char tmpbuf[256]="";

char buf[255]="",tmp[100]="",cmd[100]="";

snprintf(cmd,100, "/isan/bin/vsh -c 'sh user-account %s' > /var/tmp/.tmp_check_role", rem_usr);

system(cmd);

FILE* fp = fopen("/var/tmp/.tmp_check_role","r");

if( fp != NULL) {

// opened successfully and now read each line of the file

while( fgets(tmp,100,fp) != NULL) {

strncat(buf, tmp, strlen(tmp));

}

fclose(fp);

}

system("rm /var/tmp/.tmp_check_role");

strncpy(tmpbuf, buf, strlen(buf)+1);

tok = strtok_r(tmpbuf, "\n", &ptrbuf);

while(tok) {

char *t;

char *ptrt;

if(strstr(tok, "roles")) {

t = strtok_r(tok, ":", &ptrt);

t = strtok_r(ptrt, ":",&ptrt);

if(t) {

while(*t == ' ')

t++;

if(!strncmp(t, "Admin", strlen(t)-1)) {

return 1;

}

else {

// xml_debug_message(DEBUG_INFO,XML_PARSE,"User does not have Admin Role");

return 0;

}

}

}

tok = strtok_r(ptrbuf, "\n", &ptrbuf);

}

// xml_debug_message(DEBUG_INFO,XML_PARSE,"User does not have Admin Role");

return 0;

}

Finally with the help of the perl module I need to get checked whether the user currently logged in is an admin user or not.
 
post this file containing output of command ... /var/tmp/.tmp_check_role

specially the line ... containing "roles"

PERL has specialization in string manipulation :D
 
Will post the o/p soon and fire u a PM :)

Also any idea how do I use Perl Inline module to use this C function as such ?
 
^ If all you want to do is execute C code from the Perl program, the easiest option is to use a system command and give the execution string.

Code:
system ("./mycbinary")

The Inline module on the other hand is a proper, yet slightly tedious way of doing things if it is your first time. Documentation is still the best resort :)

Inline::C-Cookbook - search.cpan.org
 
^^ Executing C binary is out of question....as this snippet is a part of a huge code base and as such cannot be singled out.

I just need to execute a similar check for admin role in the perl module

--- Updated Post - Automerged ---

post this file containing output of command ... /var/tmp/.tmp_check_role

specially the line ... containing "roles"

Here is the o/p

user : admin

user : admin

this user account has no expiry date

roles: Admin

domain: default-domain

Context: Admin

Also what abt this simple statement.

Hw do we execute this in PERL

//if(strncmp(getenv("REMOTE_USER"), "admin", strlen("admin")) == 0) {
 
Here is the code

Code:
sub check_admin_role {
  my @rem_usr = @_;
  @sysargs = ("/isan/bin/vsh -c 'sh user-account @rem_usr' > /var/tmp/.tmp_check_role");
  system(@sysargs) == 0 or print "system @sysargs failed: $?";
  
  open FILE, "<", "/var/tmp/.tmp_check_role" or die $!;
  my @lines = <FILE>;
  close FILE;
  
  @sysargs = ("rm -f /var/tmp/.tmp_check_role");
  system(@sysargs) == 0 or print "system @sysargs failed: $?";
  
  for (0..scalar(@lines)-1) {
    #print($lines[$_]);
    if (index($lines[$_], "roles") != -1) {
      if (index($lines[$_], "Admin") != -1) {
        #print("Admin found\n");
        return 1;
      } else {
        #print("No Admin found\n");
        return 0;
      }
    }
  }
  #print("No Admin found");
  return 0;   
}

Here first trying to find the line containing "roles" and then if that line contains "Admin" then "return 1" or in other cases "return 0"

u can test it like this

Code:
print("check_admin_role = ", check_admin_role("kekerode"));

For this

//if(strncmp(getenv("REMOTE_USER"), "admin", strlen("admin")) == 0) {

u can do like this

Code:
if(index($ENV{'REMOTE_USER'}, "admin") != -1) {

Give some more info like where u r going to call this function because proper error handling should be done.
 
^^

Thanks a ton :)

I will first test the snippet u posted on my setup and check it out. I will post some more info on the calls and the usage so that we can have this more clear.

Thanks a lot again for all your help. YGPM as well :)
 
I'm not a familiar with perl but this looks dangerous:

Code:
if(index($ENV{'REMOTE_USER'}, "admin") != -1) {

It should probably be the following to prevent users with "admin" inside their IDs like "madmind" from getting admin access? :D

Code:
if(index($ENV{'REMOTE_USER'}, "admin") == 0) {

Still someone like "adminimum" could get thru - the length should also be checked to be the same, meaning a straight string compare function instead of "index" should be used?

But since its roles not usernames apparently, should not be that risky.
 
Back
Top