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

Generated on Tue Aug 31 10:05:26 2004 for RSBAC by doxygen 1.3.8