helpers.c

Go to the documentation of this file.
00001 /************************************* */
00002 /* Rule Set Based Access Control       */
00003 /* Author and (c) 1999-2004:           */
00004 /*   Amon Ott <ao@rsbac.org>           */
00005 /* Helper functions for all parts      */
00006 /* Last modified: 11/Mar/2004          */
00007 /************************************* */
00008 
00009 #include <rsbac/types.h>
00010 #include <rsbac/error.h>
00011 #include <rsbac/helpers.h>
00012 #include <rsbac/rc_types.h>
00013 #include <rsbac/getname.h>
00014 #include <rsbac/cap_getname.h>
00015 
00016 #ifdef __KERNEL__
00017 #include <linux/kernel.h>
00018 #include <linux/module.h>
00019 #include <asm/uaccess.h>
00020 #include <linux/fs.h>
00021 #include <linux/mm.h>
00022 #include <rsbac/aci.h>
00023 #include <rsbac/rkmem.h>
00024 #include <rsbac/debug.h>
00025 #ifdef CONFIG_RSBAC_RC
00026 #include <rsbac/rc_getname.h>
00027 #endif
00028 #endif
00029 #ifndef __KERNEL__
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <pwd.h>
00033 #include <grp.h>
00034 #endif
00035 
00036 char * inttostr(char * str, int i)
00037   {
00038     int j = 0;
00039     
00040     if(!str)
00041       return(NULL);
00042       
00043     if (i<0)
00044       {
00045         str[j] = '-';
00046         j++;
00047         i = -i;
00048       }
00049     if (i>=10000)
00050       {
00051         str[j] = '0' + (i / 10000);
00052         j++;
00053       }
00054     if (i>=1000)
00055       {
00056         str[j] = '0' + ((i % 10000) / 1000);
00057         j++;
00058       }
00059     if (i>=100)
00060       {
00061         str[j] = '0' + ((i % 1000) / 100);
00062         j++;
00063       }
00064     if (i>=10)
00065       {
00066         str[j] = '0' + ((i % 100) / 10);
00067         j++;
00068       }
00069     str[j] = '0' + (i % 10);
00070     j++;
00071     str[j] = 0;
00072     return (str);
00073   };
00074 
00075 char * ulongtostr(char * str, u_long i)
00076   {
00077     int    j = 0;
00078     u_long k = 1000000000;
00079     
00080     if(!str)
00081       return(NULL);
00082 
00083     if (i>=k)
00084       {
00085         str[j] = '0' + ((i / k) % 100);
00086         j++;
00087       }
00088     k /= 10;
00089 
00090     while (k>1)
00091       {
00092         if (i>=k)
00093           {
00094             str[j] = '0' + ((i % (k*10)) / k);
00095             j++;
00096           }
00097         k /= 10;
00098       };
00099 
00100     str[j] = '0' + (i % 10);
00101     j++;
00102     str[j] = 0;
00103     return (str);
00104   };
00105 
00106 char * longtostr(char * str, long i)
00107   {
00108     int    j = 0;
00109     u_long k = 1000000000;
00110     
00111     if(!str)
00112       return(NULL);
00113 
00114     if (i<0)
00115       {
00116         str[0] = '-';
00117         j = 1;
00118         i = -i;
00119       }
00120     if (i>=k)
00121       {
00122         str[j] = '0' + ((i / k) % 100);
00123         j++;
00124       }
00125     k /= 10;
00126 
00127     while (k>1)
00128       {
00129         if (i>=k)
00130           {
00131             str[j] = '0' + ((i % (k*10)) / k);
00132             j++;
00133           }
00134         k /= 10;
00135       };
00136 
00137     str[j] = '0' + (i % 10);
00138     j++;
00139     str[j] = 0;
00140     return (str);
00141   };
00142 
00143 char * u64tostrmac(char * str, __u64 i)
00144   {
00145     int    j = 0;
00146     __u64  k;
00147 
00148     if(!str)
00149       return(NULL);
00150 
00151     k = 1;
00152     for(j = RSBAC_MAC_MAX_CAT;j >= 0;j--)
00153       {
00154         if (i & k)
00155           str[j] = '1';
00156         else
00157           str[j] = '0';
00158         k<<=1;
00159       };
00160 
00161     str[RSBAC_MAC_NR_CATS] = 0;
00162     return (str);
00163   };
00164 
00165 #ifndef __KERNEL__
00166 
00167 void error_exit(int error)
00168   {
00169     char tmp1[80];
00170 
00171     if(error<0)
00172       {
00173         get_error_name(tmp1,error);
00174         fprintf(stderr, "Error: %s\n", tmp1);
00175         exit(1);
00176       }
00177   }
00178 
00179 void show_error(int error)
00180   {
00181     char tmp1[80];
00182 
00183     if(error<0)
00184       {
00185         get_error_name(tmp1,error);
00186         fprintf(stderr, "Error: %s\n", tmp1);
00187       }
00188   }
00189 
00190 int rsbac_get_uid_name(rsbac_uid_t * uid, char * name, char * sourcename)
00191   {
00192     struct passwd * user_info_p;
00193     rsbac_uid_t uid_i;
00194 
00195     if(!(user_info_p = getpwnam(sourcename)))
00196       {
00197         uid_i = strtoul(sourcename,0,10);
00198         if(   !uid_i
00199            && strcmp("0", sourcename)
00200           )
00201           {
00202             return -RSBAC_EINVALIDVALUE;
00203           }
00204         if(name)
00205           {
00206             if((user_info_p = getpwuid(uid_i)))
00207               strcpy(name, user_info_p->pw_name);
00208             else
00209               sprintf(name, "%u", uid_i);
00210           }
00211       }
00212     else
00213       {
00214         uid_i = user_info_p->pw_uid;
00215         if(name)
00216           strcpy(name, user_info_p->pw_name);
00217       }
00218     if(uid)
00219       *uid = uid_i;
00220     return 0;
00221   }
00222 
00223 int rsbac_get_fullname(char * fullname, rsbac_uid_t uid)
00224   {
00225     struct passwd * user_info_p;
00226     rsbac_uid_t uid_i;
00227 
00228     if(!fullname)
00229       return -RSBAC_EINVALIDPOINTER;
00230     if(!(user_info_p = getpwuid(uid)))
00231       {
00232         sprintf(fullname, "%u", uid);
00233       }
00234     else
00235       {
00236         strcpy(fullname, user_info_p->pw_gecos);
00237       }
00238     return 0;
00239   }
00240 
00241 char * get_user_name(rsbac_uid_t user, char * name)
00242   {
00243     struct passwd * user_info_p;
00244 
00245     if((user_info_p = getpwuid(user)))
00246       {
00247         strcpy(name, user_info_p->pw_name);
00248       }
00249     else
00250       {
00251         sprintf(name, "%u", user);
00252       }
00253     return name;
00254   }
00255 
00256 int rsbac_get_gid_name(rsbac_gid_t * gid, char * name, char * sourcename)
00257   {
00258     struct group * group_info_p;
00259     rsbac_gid_t gid_i;
00260 
00261     if(!(group_info_p = getgrnam(sourcename)))
00262       {
00263         gid_i = strtoul(sourcename,0,10);
00264         if(   !gid_i
00265            && strcmp("0", sourcename)
00266           )
00267           {
00268             return -RSBAC_EINVALIDVALUE;
00269           }
00270         if(name)
00271           {
00272             if((group_info_p = getgrgid(gid_i)))
00273               strcpy(name, group_info_p->gr_name);
00274             else
00275               sprintf(name, "%u", gid_i);
00276           }
00277       }
00278     else
00279       {
00280         gid_i = group_info_p->gr_gid;
00281         if(name)
00282           strcpy(name, group_info_p->gr_name);
00283       }
00284     if(gid)
00285       *gid = gid_i;
00286     return 0;
00287   }
00288 
00289 
00290 char * u64tostrlog(char * str, __u64 i)
00291   {
00292     int    j = 0;
00293     __u64  k;
00294 
00295     if(!str)
00296       return(NULL);
00297 
00298     k = 1;
00299     for(j = R_NONE - 1;j >= 0;j--)
00300       {
00301         if (i & k)
00302           str[j] = '1';
00303         else
00304           str[j] = '0';
00305         k<<=1;
00306       };
00307 
00308     str[R_NONE] = 0;
00309     return (str);
00310   };
00311 
00312 __u64 strtou64log(char * str, __u64 * i_p)
00313   {
00314     int    j;
00315     __u64  k = 1, res=0;
00316     
00317     if(!str)
00318       return(0);
00319 
00320     if (strlen(str) < R_NONE)
00321       return(-1);
00322     for(j=R_NONE-1;j>=0;j--)
00323       {
00324         if(str[j] != '0')
00325           {
00326             res |= k;
00327           }
00328         k <<= 1;
00329       }
00330     for(j=R_NONE;j<64;j++)
00331       {
00332         res |= k;
00333         k <<= 1;
00334       }
00335     *i_p = res;
00336     return(res);
00337   };
00338 
00339 char * u64tostrrc(char * str, __u64 i)
00340   {
00341     int    j = 0;
00342     __u64  k;
00343 
00344     if(!str)
00345       return(NULL);
00346 
00347     k = 1;
00348     for(j = 63;j >= 0;j--)
00349       {
00350         if (i & k)
00351           str[j] = '1';
00352         else
00353           str[j] = '0';
00354         k<<=1;
00355       };
00356 
00357     str[64] = 0;
00358     return (str);
00359   };
00360 
00361 __u64 strtou64rc(char * str, __u64 * i_p)
00362   {
00363     int    j;
00364     __u64  k = 1, res=0;
00365     
00366     if(!str)
00367       return(0);
00368 
00369     if (strlen(str) < 64)
00370       return(-1);
00371     for(j=63;j>=0;j--)
00372       {
00373         if(str[j] != '0')
00374           {
00375             res |= k;
00376           }
00377         k <<= 1;
00378       }
00379     *i_p = res;
00380     return(res);
00381   };
00382 
00383 char * u64tostrrcr(char * str, __u64 i)
00384   {
00385     int    j = 0;
00386     __u64  k;
00387 
00388     if(!str)
00389       return(NULL);
00390 
00391     k = 1;
00392     for(j = RCR_NONE - 1;j >= 0;j--)
00393       {
00394         if (i & k)
00395           str[j] = '1';
00396         else
00397           str[j] = '0';
00398         k<<=1;
00399       };
00400 
00401     str[RCR_NONE] = 0;
00402     return (str);
00403   };
00404 
00405 __u64 strtou64rcr(char * str, __u64 * i_p)
00406   {
00407     int    j;
00408     __u64  k = 1, res=0;
00409     
00410     if(!str)
00411       return(0);
00412 
00413     if (strlen(str) < RCR_NONE)
00414       return(-1);
00415     for(j=RCR_NONE-1;j>=0;j--)
00416       {
00417         if(str[j] != '0')
00418           {
00419             res |= k;
00420           }
00421         k <<= 1;
00422       }
00423     for(j=RCR_NONE;j<64;j++)
00424       {
00425         res |= k;
00426         k <<= 1;
00427       }
00428     *i_p = res;
00429     return(res);
00430   };
00431 
00432 __u64 strtou64mac(char * str, __u64 * i_p)
00433   {
00434     int    j;
00435     __u64  k = 1, res=0;
00436     
00437     if(!str)
00438       return(0);
00439 
00440     if (strlen(str) < RSBAC_MAC_NR_CATS)
00441       return(-1);
00442     for(j=RSBAC_MAC_MAX_CAT;j>=0;j--)
00443       {
00444         if(str[j] != '0')
00445           {
00446             res |= k;
00447           }
00448         k <<= 1;
00449       }
00450     for(j=RSBAC_MAC_NR_CATS;j<64;j++)
00451       {
00452         res |= k;
00453         k <<= 1;
00454       }
00455     *i_p = res;
00456     return(res);
00457   };
00458 
00459 __u64 strtou64acl(char * str, __u64 * i_p)
00460   {
00461     int    j;
00462     __u64  k = 1, res=0;
00463     
00464     if(!str)
00465       return(0);
00466 
00467     if (strlen(str) < (ACLR_NONE - 1))
00468       return(-1);
00469     for(j=ACLR_NONE-1;j>=0;j--)
00470       {
00471         if(str[j] != '0')
00472           {
00473             res |= k;
00474           }
00475         k <<= 1;
00476       }
00477     for(j=ACLR_NONE-1;j<64;j++)
00478       {
00479         res |= k;
00480         k <<= 1;
00481       }
00482     *i_p = res;
00483     return(res);
00484   };
00485 #endif
00486 
00487 char * u64tostracl(char * str, __u64 i)
00488   {
00489     int    j = 0;
00490     __u64  k;
00491 
00492     if(!str)
00493       return(NULL);
00494 
00495     k = 1;
00496     for(j = ACLR_NONE - 1;j >= 0;j--)
00497       {
00498         if (i & k)
00499           str[j] = '1';
00500         else
00501           str[j] = '0';
00502         k<<=1;
00503       };
00504 
00505     str[ACLR_NONE] = 0;
00506     return (str);
00507   };
00508 
00509 char * u32tostrcap(char * str, __u32 i)
00510   {
00511     int    j = 0;
00512     __u32  k;
00513 
00514     if(!str)
00515       return(NULL);
00516 
00517     k = 1;
00518     for(j = CAP_NONE - 1;j >= 0;j--)
00519       {
00520         if (i & k)
00521           str[j] = '1';
00522         else
00523           str[j] = '0';
00524         k<<=1;
00525       };
00526 
00527     str[CAP_NONE] = 0;
00528     return (str);
00529   };
00530 
00531 __u32 strtou32cap(char * str, __u32 * i_p)
00532   {
00533     int    j;
00534     __u32  k = 1, res=0;
00535     
00536     if(!str)
00537       return(0);
00538 
00539     if (strlen(str) < CAP_NONE)
00540       return(-1);
00541     for(j=CAP_NONE-1;j>=0;j--)
00542       {
00543         if(str[j] != '0')
00544           {
00545             res |= k;
00546           }
00547         k <<= 1;
00548       }
00549     for(j=CAP_NONE;j<32;j++)
00550       {
00551         res |= k;
00552         k <<= 1;
00553       }
00554     *i_p = res;
00555     return(res);
00556   };
00557 
00558 
00559 #ifdef __KERNEL__
00560 
00561 /* find the current owner of this process */
00562 int rsbac_get_owner(rsbac_uid_t * user_p)
00563   {
00564     *user_p = current->uid;
00565     return(0);
00566   }
00567 
00568 void rsbac_ds_get_error(char * function, enum rsbac_attribute_t attr)
00569   {
00570     if(!function)
00571       return;
00572     if(attr != A_none)
00573       {
00574         char tmp[80];
00575 
00576         get_attribute_name(tmp, attr);
00577 #ifdef CONFIG_RSBAC_RMSG
00578         rsbac_printk(KERN_WARNING
00579                      "%s: rsbac_get_attr() for %s returned error!\n",
00580                      function, tmp);
00581 #endif
00582 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00583         if (!rsbac_nosyslog)
00584 #endif
00585           printk(KERN_WARNING
00586                  "%s: rsbac_get_attr() for %s returned error!\n",
00587                  function, tmp);
00588       }
00589     else
00590       {
00591 #ifdef CONFIG_RSBAC_RMSG
00592         rsbac_printk(KERN_WARNING
00593                      "%s: rsbac_get_attr() returned error!\n",
00594                      function);
00595 #endif
00596 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00597         if (!rsbac_nosyslog)
00598 #endif
00599           printk(KERN_WARNING
00600                  "%s: rsbac_get_attr() returned error!\n",
00601                  function);
00602       }
00603   }
00604 
00605 void rsbac_ds_set_error(char * function, enum rsbac_attribute_t attr)
00606   {
00607     if(!function)
00608       return;
00609     if(attr != A_none)
00610       {
00611         char tmp[80];
00612 
00613         get_attribute_name(tmp, attr);
00614 #ifdef CONFIG_RSBAC_RMSG
00615         rsbac_printk(KERN_WARNING
00616                      "%s: rsbac_set_attr() for %s returned error!\n",
00617                      function, tmp);
00618 #endif
00619 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00620         if (!rsbac_nosyslog)
00621 #endif
00622           printk(KERN_WARNING
00623                  "%s: rsbac_set_attr() for %s returned error!\n",
00624                  function, tmp);
00625       }
00626     else
00627       {
00628 #ifdef CONFIG_RSBAC_RMSG
00629         rsbac_printk(KERN_WARNING
00630                      "%s: rsbac_set_attr() returned error!\n",
00631                      function);
00632 #endif
00633 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00634         if (!rsbac_nosyslog)
00635 #endif
00636           printk(KERN_WARNING
00637                  "%s: rsbac_set_attr() returned error!\n",
00638                  function);
00639       }
00640   }
00641 
00642 #ifdef CONFIG_RSBAC_RC
00643 void rsbac_rc_ds_get_error(char * function, enum rsbac_rc_item_t item)
00644   {
00645     if(!function)
00646       return;
00647     if(item != RI_none)
00648       {
00649         char tmp[80];
00650 
00651         get_rc_item_name(tmp, item);
00652 #ifdef CONFIG_RSBAC_RMSG
00653         rsbac_printk(KERN_WARNING
00654                      "%s: rsbac_rc_get_item() for %s returned error!\n",
00655                      function, tmp);
00656 #endif
00657 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00658         if (!rsbac_nosyslog)
00659 #endif
00660           printk(KERN_WARNING
00661                  "%s: rsbac_rc_get_item() for %s returned error!\n",
00662                  function, tmp);
00663       }
00664     else
00665       {
00666 #ifdef CONFIG_RSBAC_RMSG
00667         rsbac_printk(KERN_WARNING
00668                      "%s: rsbac_rc_get_item() returned error!\n",
00669                      function);
00670 #endif
00671 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00672         if (!rsbac_nosyslog)
00673 #endif
00674           printk(KERN_WARNING
00675                  "%s: rsbac_rc_get_item() returned error!\n",
00676                  function);
00677       }
00678   }
00679 
00680 void rsbac_rc_ds_set_error(char * function, enum rsbac_rc_item_t item)
00681   {
00682     if(!function)
00683       return;
00684     if(item != RI_none)
00685       {
00686         char tmp[80];
00687 
00688         get_rc_item_name(tmp, item);
00689 #ifdef CONFIG_RSBAC_RMSG
00690         rsbac_printk(KERN_WARNING
00691                      "%s: rsbac_rc_set_item() for %s returned error!\n",
00692                      function, tmp);
00693 #endif
00694 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00695         if (!rsbac_nosyslog)
00696 #endif
00697           printk(KERN_WARNING
00698                  "%s: rsbac_rc_set_item() for %s returned error!\n",
00699                  function, tmp);
00700       }
00701     else
00702       {
00703 #ifdef CONFIG_RSBAC_RMSG
00704         rsbac_printk(KERN_WARNING
00705                      "%s: rsbac_rc_set_item() returned error!\n",
00706                      function);
00707 #endif
00708 #ifdef CONFIG_RSBAC_RMSG_NOSYSLOG
00709         if (!rsbac_nosyslog)
00710 #endif
00711           printk(KERN_WARNING
00712                  "%s: rsbac_rc_set_item() returned error!\n",
00713                  function);
00714       }
00715   }
00716 #endif
00717 
00718 
00719 /****************************************************************/
00720 /* Access to user data space                                    */
00721 
00722 
00723 #if defined(CONFIG_RSBAC_REG) || defined(CONFIG_RSBAC_REG_MAINT)
00724 EXPORT_SYMBOL(rsbac_get_user);
00725 #endif
00726 int rsbac_get_user(unsigned char * kern_p, unsigned char * user_p, int size)
00727   {
00728     if(kern_p && user_p && (size > 0))
00729       {
00730         return copy_from_user(kern_p, user_p, size);
00731       }
00732     return(0);
00733   }
00734 
00735 
00736 #if defined(CONFIG_RSBAC_REG) || defined(CONFIG_RSBAC_REG_MAINT)
00737 EXPORT_SYMBOL(rsbac_put_user);
00738 #endif
00739 int rsbac_put_user(unsigned char * kern_p, unsigned char * user_p, int size)
00740   {
00741     if(kern_p && user_p && (size > 0))
00742       {
00743         return copy_to_user(user_p,kern_p,size);
00744       }
00745     return(0);
00746   };
00747 
00748 #if defined(CONFIG_RSBAC_REG) || defined(CONFIG_RSBAC_REG_MAINT)
00749 EXPORT_SYMBOL(rsbac_getname);
00750 #endif
00751 char * rsbac_getname(const char * name)
00752   {
00753     return getname(name);
00754   };
00755 
00756 #if defined(CONFIG_RSBAC_REG) || defined(CONFIG_RSBAC_REG_MAINT)
00757 EXPORT_SYMBOL(rsbac_putname);
00758 #endif
00759 void rsbac_putname(const char * name)
00760   {
00761     putname(name);
00762   }
00763 
00764 inline int clear_user_buf(char * ubuf, int len)
00765   {
00766     return clear_user(ubuf,len);
00767   }
00768 
00769 #endif /* __KERNEL__ */

Generated on Fri Jun 17 09:45:26 2005 for RSBAC by  doxygen 1.4.2