helpers.c File Reference

#include <stdlib.h>
#include <rsbac/types.h>
#include <rsbac/error.h>
#include <rsbac/helpers.h>
#include <rsbac/rc_types.h>
#include <rsbac/getname.h>
#include <rsbac/cap_getname.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>

Go to the source code of this file.

Functions

int rsbac_u32_compare (__u32 *a, __u32 *b)
int rsbac_user_compare (const void *a, const void *b)
int rsbac_group_compare (const void *a, const void *b)
int rsbac_nettemp_id_compare (const void *a, const void *b)
int rsbac_dev_compare (const void *desc1, const void *desc2)
char * inttostr (char *str, int i)
char * ulongtostr (char *str, u_long i)
char * longtostr (char *str, long i)
char * u64tostrmac (char *str, __u64 i)
void error_exit (int error)
void show_error (int error)
int rsbac_get_uid_name (rsbac_uid_t *uid, char *name, char *sourcename)
int rsbac_get_fullname (char *fullname, rsbac_uid_t uid)
char * get_user_name (rsbac_uid_t user, char *name)
char * get_group_name (rsbac_gid_t group, char *name)
int rsbac_get_gid_name (rsbac_gid_t *gid, char *name, char *sourcename)
char * u64tostrlog (char *str, __u64 i)
__u64 strtou64log (char *str, __u64 *i_p)
char * u64tostrrc (char *str, __u64 i)
__u64 strtou64rc (char *str, __u64 *i_p)
char * u64tostrrcr (char *str, __u64 i)
__u64 strtou64rcr (char *str, __u64 *i_p)
__u64 strtou64mac (char *str, __u64 *i_p)
__u64 strtou64acl (char *str, __u64 *i_p)
int strtodevdesc (char *str, struct rsbac_dev_desc_t *dev_p)
char * devdesctostr (char *str, struct rsbac_dev_desc_t dev)
char * u64tostracl (char *str, __u64 i)
char * u32tostrcap (char *str, __u32 i)
__u32 strtou32cap (char *str, __u32 *i_p)


Function Documentation

char* devdesctostr char *  str,
struct rsbac_dev_desc_t  dev
 

Definition at line 598 of file helpers.c.

References D_block, D_block_major, D_char, D_char_major, rsbac_dev_desc_t::major, rsbac_dev_desc_t::minor, RSBAC_IS_ZERO_DEV_DESC, and rsbac_dev_desc_t::type.

00599   {
00600     if(RSBAC_IS_ZERO_DEV_DESC(dev))
00601       {
00602         sprintf(str, ":DEFAULT:");
00603         return str;
00604       }
00605     switch(dev.type)
00606       {
00607         case D_block:
00608         case D_char:
00609           sprintf(str, "%c%u:%u", 'b' + dev.type, dev.major, dev.minor);
00610           break;
00611         case D_block_major:
00612         case D_char_major:
00613           sprintf(str, "%c%u",
00614                   'b' + dev.type - (D_block_major - D_block),
00615                   dev.major);
00616           break;
00617         default:
00618           sprintf(str, "invalid!");
00619       }
00620     return str;
00621   }

void error_exit int  error  ) 
 

Definition at line 219 of file helpers.c.

References get_error_name().

00220   {
00221     char tmp1[80];
00222 
00223     if(error<0)
00224       {
00225         get_error_name(tmp1,error);
00226         fprintf(stderr, "Error: %s\n", tmp1);
00227         exit(1);
00228       }
00229   }

char* get_group_name rsbac_gid_t  group,
char *  name
 

Definition at line 308 of file helpers.c.

00309   {
00310     struct group * group_info_p;
00311 
00312     if((group_info_p = getgrgid(group)))
00313       {
00314         strcpy(name, group_info_p->gr_name);
00315       }
00316     else
00317       {
00318         sprintf(name, "%u", group);
00319       }
00320     return name;
00321   }

char* get_user_name rsbac_uid_t  user,
char *  name
 

Definition at line 293 of file helpers.c.

00294   {
00295     struct passwd * user_info_p;
00296 
00297     if((user_info_p = getpwuid(user)))
00298       {
00299         strcpy(name, user_info_p->pw_name);
00300       }
00301     else
00302       {
00303         sprintf(name, "%u", user);
00304       }
00305     return name;
00306   }

char* inttostr char *  str,
int  i
 

Definition at line 88 of file helpers.c.

References NULL.

Referenced by acl_detach_fd_lists(), acl_register_fd_lists(), auth_detach_fd_lists(), auth_register_fd_lists(), get_error_name(), mac_detach_fd_lists(), and mac_register_fd_lists().

00089   {
00090     int j = 0;
00091     
00092     if(!str)
00093       return(NULL);
00094       
00095     if (i<0)
00096       {
00097         str[j] = '-';
00098         j++;
00099         i = -i;
00100       }
00101     if (i>=10000)
00102       {
00103         str[j] = '0' + (i / 10000);
00104         j++;
00105       }
00106     if (i>=1000)
00107       {
00108         str[j] = '0' + ((i % 10000) / 1000);
00109         j++;
00110       }
00111     if (i>=100)
00112       {
00113         str[j] = '0' + ((i % 1000) / 100);
00114         j++;
00115       }
00116     if (i>=10)
00117       {
00118         str[j] = '0' + ((i % 100) / 10);
00119         j++;
00120       }
00121     str[j] = '0' + (i % 10);
00122     j++;
00123     str[j] = 0;
00124     return (str);
00125   };

char* longtostr char *  str,
long  i
 

Definition at line 158 of file helpers.c.

References NULL.

00159   {
00160     int    j = 0;
00161     u_long k = 1000000000;
00162     
00163     if(!str)
00164       return(NULL);
00165 
00166     if (i<0)
00167       {
00168         str[0] = '-';
00169         j = 1;
00170         i = -i;
00171       }
00172     if (i>=k)
00173       {
00174         str[j] = '0' + ((i / k) % 100);
00175         j++;
00176       }
00177     k /= 10;
00178 
00179     while (k>1)
00180       {
00181         if (i>=k)
00182           {
00183             str[j] = '0' + ((i % (k*10)) / k);
00184             j++;
00185           }
00186         k /= 10;
00187       };
00188 
00189     str[j] = '0' + (i % 10);
00190     j++;
00191     str[j] = 0;
00192     return (str);
00193   };

int rsbac_dev_compare const void *  desc1,
const void *  desc2
 

Definition at line 65 of file helpers.c.

References rsbac_dev_desc_t::major, rsbac_dev_desc_t::minor, and rsbac_dev_desc_t::type.

00067   {
00068     const struct rsbac_dev_desc_t * i_desc1 = desc1;
00069     const struct rsbac_dev_desc_t * i_desc2 = desc2;
00070     int result;
00071     
00072     result = memcmp(&i_desc1->type,
00073                     &i_desc2->type,
00074                     sizeof(i_desc1->type));
00075     if(result)
00076       return result;
00077     result = memcmp(&i_desc1->major,
00078                     &i_desc2->major,
00079                     sizeof(i_desc1->major));
00080     if(result)
00081       return result;
00082     return memcmp(&i_desc1->minor,
00083                   &i_desc2->minor,
00084                   sizeof(i_desc1->minor));
00085   }

int rsbac_get_fullname char *  fullname,
rsbac_uid_t  uid
 

Definition at line 275 of file helpers.c.

References RSBAC_EINVALIDPOINTER.

00276   {
00277     struct passwd * user_info_p;
00278     rsbac_uid_t uid_i;
00279 
00280     if(!fullname)
00281       return -RSBAC_EINVALIDPOINTER;
00282     if(!(user_info_p = getpwuid(uid)))
00283       {
00284         sprintf(fullname, "%u", uid);
00285       }
00286     else
00287       {
00288         strcpy(fullname, user_info_p->pw_gecos);
00289       }
00290     return 0;
00291   }

int rsbac_get_gid_name rsbac_gid_t gid,
char *  name,
char *  sourcename
 

Definition at line 323 of file helpers.c.

References RSBAC_EINVALIDVALUE.

Referenced by rsbac_get_gid().

00324   {
00325     struct group * group_info_p;
00326     rsbac_gid_t gid_i;
00327 
00328     if(!(group_info_p = getgrnam(sourcename)))
00329       {
00330         gid_i = strtoul(sourcename,0,10);
00331         if(   !gid_i
00332            && strcmp("0", sourcename)
00333           )
00334           {
00335             return -RSBAC_EINVALIDVALUE;
00336           }
00337         if(name)
00338           {
00339             if((group_info_p = getgrgid(gid_i)))
00340               strcpy(name, group_info_p->gr_name);
00341             else
00342               sprintf(name, "%u", gid_i);
00343           }
00344       }
00345     else
00346       {
00347         gid_i = group_info_p->gr_gid;
00348         if(name)
00349           strcpy(name, group_info_p->gr_name);
00350       }
00351     if(gid)
00352       *gid = gid_i;
00353     return 0;
00354   }

int rsbac_get_uid_name rsbac_uid_t uid,
char *  name,
char *  sourcename
 

Definition at line 242 of file helpers.c.

References RSBAC_EINVALIDVALUE.

Referenced by rsbac_get_uid().

00243   {
00244     struct passwd * user_info_p;
00245     rsbac_uid_t uid_i;
00246 
00247     if(!(user_info_p = getpwnam(sourcename)))
00248       {
00249         uid_i = strtoul(sourcename,0,10);
00250         if(   !uid_i
00251            && strcmp("0", sourcename)
00252           )
00253           {
00254             return -RSBAC_EINVALIDVALUE;
00255           }
00256         if(name)
00257           {
00258             if((user_info_p = getpwuid(uid_i)))
00259               strcpy(name, user_info_p->pw_name);
00260             else
00261               sprintf(name, "%u", uid_i);
00262           }
00263       }
00264     else
00265       {
00266         uid_i = user_info_p->pw_uid;
00267         if(name)
00268           strcpy(name, user_info_p->pw_name);
00269       }
00270     if(uid)
00271       *uid = uid_i;
00272     return 0;
00273   }

int rsbac_group_compare const void *  a,
const void *  b
 

Definition at line 55 of file helpers.c.

References rsbac_u32_compare().

00056   {
00057     return rsbac_u32_compare((__u32 *) a, (__u32 *) b);
00058   }

int rsbac_nettemp_id_compare const void *  a,
const void *  b
 

Definition at line 60 of file helpers.c.

References rsbac_u32_compare().

00061   {
00062     return rsbac_u32_compare((__u32 *) a, (__u32 *) b);
00063   }

int rsbac_u32_compare __u32 *  a,
__u32 *  b
 

Definition at line 41 of file helpers.c.

Referenced by rsbac_group_compare(), rsbac_nettemp_id_compare(), and rsbac_user_compare().

00042   {
00043     if(*a < *b)
00044      return -1;
00045     if(*a > *b)
00046       return 1;
00047     return 0;
00048   }

int rsbac_user_compare const void *  a,
const void *  b
 

Definition at line 50 of file helpers.c.

References rsbac_u32_compare().

00051   {
00052     return rsbac_u32_compare((__u32 *) a, (__u32 *) b);
00053   }

void show_error int  error  ) 
 

Definition at line 231 of file helpers.c.

References get_error_name().

00232   {
00233     char tmp1[80];
00234 
00235     if(error<0)
00236       {
00237         get_error_name(tmp1,error);
00238         fprintf(stderr, "Error: %s\n", tmp1);
00239       }
00240   }

int strtodevdesc char *  str,
struct rsbac_dev_desc_t dev_p
 

Definition at line 553 of file helpers.c.

References D_block, D_block_major, D_char, D_char_major, rsbac_dev_desc_t::major, rsbac_dev_desc_t::minor, RSBAC_EINVALIDTARGET, RSBAC_EINVALIDVALUE, RSBAC_ZERO_DEV_DESC, and rsbac_dev_desc_t::type.

00554   {
00555     char * p;
00556     char * c;
00557 
00558     if(!str)
00559       return -RSBAC_EINVALIDVALUE;
00560     if(!strcmp(str, ":DEFAULT:"))
00561       {
00562         *dev_p = RSBAC_ZERO_DEV_DESC;
00563         return 0;
00564       }
00565     p = str;
00566     c = strchr(p,':');
00567     switch(*p)
00568       {
00569         case 'b':
00570         case 'B':
00571           if(c)
00572             dev_p->type = D_block;
00573           else
00574             dev_p->type = D_block_major;
00575           break;
00576         case 'c':
00577         case 'C':
00578           if(c)
00579             dev_p->type = D_char;
00580           else
00581             dev_p->type = D_char_major;
00582           break;
00583         default:
00584           return -RSBAC_EINVALIDTARGET;
00585       }
00586     p++;
00587     dev_p->major = strtoul(p,0,0);
00588     if(c)
00589       {
00590         c++;
00591         dev_p->minor = strtoul(c,0,0);
00592       }
00593     else
00594       dev_p->minor = 0;
00595     return 0;
00596   }

__u32 strtou32cap char *  str,
__u32 *  i_p
 

Definition at line 668 of file helpers.c.

References CAP_NONE.

00669   {
00670     int    j;
00671     __u32  k = 1, res=0;
00672     
00673     if(!str)
00674       return(0);
00675 
00676     if (strlen(str) < CAP_NONE)
00677       return(-1);
00678     for(j=CAP_NONE-1;j>=0;j--)
00679       {
00680         if(str[j] != '0')
00681           {
00682             res |= k;
00683           }
00684         k <<= 1;
00685       }
00686     for(j=CAP_NONE;j<32;j++)
00687       {
00688         res |= k;
00689         k <<= 1;
00690       }
00691     *i_p = res;
00692     return(res);
00693   };

__u64 strtou64acl char *  str,
__u64 *  i_p
 

Definition at line 526 of file helpers.c.

References ACLR_NONE.

00527   {
00528     int    j;
00529     __u64  k = 1, res=0;
00530     
00531     if(!str)
00532       return(0);
00533 
00534     if (strlen(str) < (ACLR_NONE - 1))
00535       return(-1);
00536     for(j=ACLR_NONE-1;j>=0;j--)
00537       {
00538         if(str[j] != '0')
00539           {
00540             res |= k;
00541           }
00542         k <<= 1;
00543       }
00544     for(j=ACLR_NONE-1;j<64;j++)
00545       {
00546         res |= k;
00547         k <<= 1;
00548       }
00549     *i_p = res;
00550     return(res);
00551   }

__u64 strtou64log char *  str,
__u64 *  i_p
 

Definition at line 379 of file helpers.c.

References R_NONE.

00380   {
00381     int    j;
00382     __u64  k = 1, res=0;
00383     
00384     if(!str)
00385       return(0);
00386 
00387     if (strlen(str) < R_NONE)
00388       return(-1);
00389     for(j=R_NONE-1;j>=0;j--)
00390       {
00391         if(str[j] != '0')
00392           {
00393             res |= k;
00394           }
00395         k <<= 1;
00396       }
00397     for(j=R_NONE;j<64;j++)
00398       {
00399         res |= k;
00400         k <<= 1;
00401       }
00402     *i_p = res;
00403     return(res);
00404   };

__u64 strtou64mac char *  str,
__u64 *  i_p
 

Definition at line 499 of file helpers.c.

References RSBAC_MAC_MAX_CAT, and RSBAC_MAC_NR_CATS.

00500   {
00501     int    j;
00502     __u64  k = 1, res=0;
00503     
00504     if(!str)
00505       return(0);
00506 
00507     if (strlen(str) < RSBAC_MAC_NR_CATS)
00508       return(-1);
00509     for(j=RSBAC_MAC_MAX_CAT;j>=0;j--)
00510       {
00511         if(str[j] != '0')
00512           {
00513             res |= k;
00514           }
00515         k <<= 1;
00516       }
00517     for(j=RSBAC_MAC_NR_CATS;j<64;j++)
00518       {
00519         res |= k;
00520         k <<= 1;
00521       }
00522     *i_p = res;
00523     return(res);
00524   };

__u64 strtou64rc char *  str,
__u64 *  i_p
 

Definition at line 428 of file helpers.c.

00429   {
00430     int    j;
00431     __u64  k = 1, res=0;
00432     
00433     if(!str)
00434       return(0);
00435 
00436     if (strlen(str) < 64)
00437       return(-1);
00438     for(j=63;j>=0;j--)
00439       {
00440         if(str[j] != '0')
00441           {
00442             res |= k;
00443           }
00444         k <<= 1;
00445       }
00446     *i_p = res;
00447     return(res);
00448   };

__u64 strtou64rcr char *  str,
__u64 *  i_p
 

Definition at line 472 of file helpers.c.

References RCR_NONE.

00473   {
00474     int    j;
00475     __u64  k = 1, res=0;
00476     
00477     if(!str)
00478       return(0);
00479 
00480     if (strlen(str) < RCR_NONE)
00481       return(-1);
00482     for(j=RCR_NONE-1;j>=0;j--)
00483       {
00484         if(str[j] != '0')
00485           {
00486             res |= k;
00487           }
00488         k <<= 1;
00489       }
00490     for(j=RCR_NONE;j<64;j++)
00491       {
00492         res |= k;
00493         k <<= 1;
00494       }
00495     *i_p = res;
00496     return(res);
00497   };

char* u32tostrcap char *  str,
__u32  i
 

Definition at line 646 of file helpers.c.

References CAP_NONE, and NULL.

00647   {
00648     int    j = 0;
00649     __u32  k;
00650 
00651     if(!str)
00652       return(NULL);
00653 
00654     k = 1;
00655     for(j = CAP_NONE - 1;j >= 0;j--)
00656       {
00657         if (i & k)
00658           str[j] = '1';
00659         else
00660           str[j] = '0';
00661         k<<=1;
00662       };
00663 
00664     str[CAP_NONE] = 0;
00665     return (str);
00666   };

char* u64tostracl char *  str,
__u64  i
 

Definition at line 624 of file helpers.c.

References ACLR_NONE, and NULL.

Referenced by rsbac_acl_sys_add_to_acl_entry(), rsbac_acl_sys_remove_from_acl_entry(), rsbac_acl_sys_set_acl_entry(), and rsbac_acl_sys_set_mask().

00625   {
00626     int    j = 0;
00627     __u64  k;
00628 
00629     if(!str)
00630       return(NULL);
00631 
00632     k = 1;
00633     for(j = ACLR_NONE - 1;j >= 0;j--)
00634       {
00635         if (i & k)
00636           str[j] = '1';
00637         else
00638           str[j] = '0';
00639         k<<=1;
00640       };
00641 
00642     str[ACLR_NONE] = 0;
00643     return (str);
00644   };

char* u64tostrlog char *  str,
__u64  i
 

Definition at line 357 of file helpers.c.

References NULL, and R_NONE.

00358   {
00359     int    j = 0;
00360     __u64  k;
00361 
00362     if(!str)
00363       return(NULL);
00364 
00365     k = 1;
00366     for(j = R_NONE - 1;j >= 0;j--)
00367       {
00368         if (i & k)
00369           str[j] = '1';
00370         else
00371           str[j] = '0';
00372         k<<=1;
00373       };
00374 
00375     str[R_NONE] = 0;
00376     return (str);
00377   };

char* u64tostrmac char *  str,
__u64  i
 

Definition at line 195 of file helpers.c.

References NULL, RSBAC_MAC_MAX_CAT, and RSBAC_MAC_NR_CATS.

Referenced by auto_read_attr(), auto_read_write_attr(), auto_write_attr(), and rsbac_mac_set_curr_level().

00196   {
00197     int    j = 0;
00198     __u64  k;
00199 
00200     if(!str)
00201       return(NULL);
00202 
00203     k = 1;
00204     for(j = RSBAC_MAC_MAX_CAT;j >= 0;j--)
00205       {
00206         if (i & k)
00207           str[j] = '1';
00208         else
00209           str[j] = '0';
00210         k<<=1;
00211       };
00212 
00213     str[RSBAC_MAC_NR_CATS] = 0;
00214     return (str);
00215   };

char* u64tostrrc char *  str,
__u64  i
 

Definition at line 406 of file helpers.c.

References NULL.

00407   {
00408     int    j = 0;
00409     __u64  k;
00410 
00411     if(!str)
00412       return(NULL);
00413 
00414     k = 1;
00415     for(j = 63;j >= 0;j--)
00416       {
00417         if (i & k)
00418           str[j] = '1';
00419         else
00420           str[j] = '0';
00421         k<<=1;
00422       };
00423 
00424     str[64] = 0;
00425     return (str);
00426   };

char* u64tostrrcr char *  str,
__u64  i
 

Definition at line 450 of file helpers.c.

References NULL, and RCR_NONE.

00451   {
00452     int    j = 0;
00453     __u64  k;
00454 
00455     if(!str)
00456       return(NULL);
00457 
00458     k = 1;
00459     for(j = RCR_NONE - 1;j >= 0;j--)
00460       {
00461         if (i & k)
00462           str[j] = '1';
00463         else
00464           str[j] = '0';
00465         k<<=1;
00466       };
00467 
00468     str[RCR_NONE] = 0;
00469     return (str);
00470   };

char* ulongtostr char *  str,
u_long  i
 

Definition at line 127 of file helpers.c.

References NULL.

00128   {
00129     int    j = 0;
00130     u_long k = 1000000000;
00131     
00132     if(!str)
00133       return(NULL);
00134 
00135     if (i>=k)
00136       {
00137         str[j] = '0' + ((i / k) % 100);
00138         j++;
00139       }
00140     k /= 10;
00141 
00142     while (k>1)
00143       {
00144         if (i>=k)
00145           {
00146             str[j] = '0' + ((i % (k*10)) / k);
00147             j++;
00148           }
00149         k /= 10;
00150       };
00151 
00152     str[j] = '0' + (i % 10);
00153     j++;
00154     str[j] = 0;
00155     return (str);
00156   };


Generated on Sun May 21 14:31:07 2006 for RSBAC by  doxygen 1.4.2