UNIXϵͳΪ³ÌÐòÔ±ÌṩÁËÐí¶à×Ó³ÌÐò,ÕâЩ×Ó³ÌÐò¿É´æÈ¡¸÷ÖÖ°²È«ÊôÐÔ.ÓÐ
ЩÊÇÐÅÏ¢×Ó³ÌÐò,·µ»ØÎļþÊôÐÔ,ʵ¼ÊµÄºÍÓÐЧµÄUID,GIDµÈÐÅÏ¢.ÓÐЩ×Ó³ÌÐò¿É
¸Ä±äÎļþÊôÐÔ.UID,GIDµÈÓÐЩ´¦Àí¿ÚÁîÎļþºÍС×éÎļþ,»¹ÓÐЩÍê³É¼ÓÃܺͽâÃÜ.
±¾ÎÄÖ÷ÒªÌÖÂÛÓйØϵͳ×Ó³ÌÐò,±ê×¼C¿â×Ó³ÌÐòµÄ°²È«,ÈçºÎд°²È«µÄC³ÌÐò
²¢´ÓrootµÄ½Ç¶È½éÉܳÌÐòÉè¼Æ(½öÄܱ»rootµ÷ÓõÄ×Ó³ÌÐò).
1.ϵͳ×Ó³ÌÐò
(1)I/O×Ó³ÌÐò
*creat():½¨Á¢Ò»¸öÐÂÎļþ»òÖØдһ¸öÔÝ´æÎļþ.
ÐèÒªÁ½¸ö²ÎÊý:ÎļþÃûºÍ´æÈ¡Ðí¿ÉÖµ(8½øÖÆ·½Ê½).Èç:
creat(""/usr/pat/read_write"",0666) /* ½¨Á¢´æÈ¡Ðí¿É·½Ê½Îª0666µÄÎļþ */
µ÷ÓôË×Ó³ÌÐòµÄ½ø³Ì±ØÐëÒªÓн¨Á¢µÄÎļþµÄËùÔÚĿ¼µÄдºÍÖ´ÐÐÐí¿É,ÖÃ
¸øcreat()µÄÐí¿É·½Ê½±äÁ¿½«±»umask()ÉèÖõÄÎļþ½¨Á¢ÆÁ±ÎÖµËùÐÞ¸Ä,ÐÂ
ÎļþµÄËùÓÐÕߺÍС×éÓÉÓÐЧµÄUIDºÍGID¾ö¶¨.
·µ»ØֵΪн¨ÎļþµÄÎļþÃèÊö·û.
*fstat():¼ûºóÃæµÄstat().
*open():ÔÚC³ÌÐòÄÚ²¿´ò¿ªÎļþ.
ÐèÒªÁ½¸ö²ÎÊý:Îļþ·¾¶ÃûºÍ´ò¿ª·½Ê½(I,O,I&O).
Èç¹ûµ÷ÓôË×Ó³ÌÐòµÄ½ø³ÌûÓжÔÓÚÒª´ò¿ªµÄÎļþµÄÕýÈ·´æÈ¡Ðí¿É(°üÀ¨ÎÄ
¼þ·¾¶ÉÏËùÓÐĿ¼·ÖÁ¿µÄËÑË÷Ðí¿É),½«»áÒýÆðÖ´ÐÐʧ°Ü.
Èç¹û´Ë×Ó³ÌÐò±»µ÷ÓÃÈ¥´ò¿ª²»´æÔÚµÄÎļþ,³ý·ÇÉèÖÃÁËO_CREAT±êÖ¾,µ÷ÓÃ
½«²»³É¹¦.´Ëʱ,ÐÂÎļþµÄ´æÈ¡Ðí¿É×÷ΪµÚÈý¸ö²ÎÊý(¿É±»Óû§µÄumaskÐÞ
¸Ä).
µ±Îļþ±»½ø³Ì´ò¿ªºóÔٸıä¸ÃÎļþ»ò¸ÃÎļþËùÔÚĿ¼µÄ´æÈ¡Ðí¿É,²»Ó°Ïì
¶Ô¸ÃÎļþµÄI/O²Ù×÷.
*read():´ÓÒÑÓÉopen()´ò¿ª²¢ÓÃ×÷ÊäÈëµÄÎļþÖжÁÐÅÏ¢.
Ëü²¢²»¹ØÐĸÃÎļþµÄ´æÈ¡Ðí¿É.Ò»µ©Îļþ×÷ΪÊäÈë´ò¿ª,¼´¿É´Ó¸ÃÎļþÖжÁ
È¡ÐÅÏ¢.
*write():Êä³öÐÅÏ¢µ½ÒÑÓÉopen()´ò¿ª²¢ÓÃ×÷Êä³öµÄÎļþÖÐ.ͬread()Ò»Ñù
ËüÒ²²»¹ØÐĸÃÎļþµÄ´æÈ¡Ðí¿É.
(2)½ø³Ì¿ØÖÆ
*exec()×å:°üÀ¨execl(),execv(),execle(),execve(),execlp()ºÍexecvp()
¿É½«Ò»¿ÉÖ´ÐÐÄ£¿ì¿½±´µ½µ÷Óýø³ÌÕ¼ÓеĴæÖü¿Õ¼ä.Õý±»µ÷Óýø
³ÌÖ´ÐеijÌÐò½«²»¸´´æÔÚ,гÌÐòÈ¡´úÆäλÖÃ.
ÕâÊÇUNIXϵͳÖÐÒ»¸ö³ÌÐò±»Ö´ÐеÄΨһ·½Ê½:Óý«Ö´ÐеijÌÐò¸´¸ÇÔÓеÄ
³ÌÐò.
°²È«×¢ÒâÊÂÏî:
. ʵ¼ÊµÄºÍÓÐЧµÄUIDºÍGID´«µÝ¸øÓÉexec()µ÷ÈëµÄ²»¾ßÓÐSUIDºÍSGIDÐí
¿ÉµÄ³ÌÐò.
. Èç¹ûÓÉexec()µ÷ÈëµÄ³ÌÐòÓÐSUIDºÍSGIDÐí¿É,ÔòÓÐЧµÄUIDºÍGID½«Éè
Öøø¸Ã³ÌÐòµÄËùÓÐÕß»òС×é.
. Îļþ½¨Á¢ÆÁ±ÎÖµ½«´«µÝ¸øгÌÐò.
. ³ýÉèÁ˶Ôexec()¹Ø±Õ±êÖ¾µÄÎļþÍâ,ËùÓдò¿ªµÄÎļþ¶¼´«µÝ¸øгÌÐò.
ÓÃfcntl()×Ó³ÌÐò¿ÉÉèÖöÔexec()µÄ¹Ø±Õ±êÖ¾.
*fork():ÓÃÀ´½¨Á¢Ð½ø³Ì.Æ佨Á¢µÄ×Ó½ø³ÌÊÇÓëµ÷ÓÃfork()µÄ½ø³Ì(¸¸½ø³Ì)
ÍêÈ«ÏàͬµÄ¿½±´(³ýÁ˽ø³ÌºÅÍâ)
°²È«×¢ÒâÊÂÏî:
. ×Ó½ø³Ì½«¼Ì³Ð¸¸½ø³ÌµÄʵ¼ÊºÍÓÐЧµÄUIDºÍGID.
. ×Ó½ø³Ì¼Ì³ÐÎļþ·½Ê½½¨Á¢ÆÁ±ÎÖµ.
. ËùÓдò¿ªµÄÎļþ´«¸ø×Ó½ø³Ì.
*signal():ÔÊÐí½ø³Ì´¦Àí¿ÉÄÜ·¢ÉúµÄÒâÍâʼþºÍÖжÏ.
ÐèÒªÁ½¸ö²ÎÊý:ÐźűàºÅºÍÐźŷ¢ÉúʱҪµ÷ÓõÄ×Ó³ÌÐò.
ÐźűàºÅ¶¨ÒåÔÚsignal.hÖÐ.
Ðźŷ¢ÉúʱҪµ÷ÓõÄ×Ó³ÌÐò¿ÉÓÉÓû§±àд,Ò²¿ÉÓÃϵͳ¸øµÄÖµ,Èç:SIG_IGN
ÔòÐźŽ«±»ºöÂÔ,SIG_DFLÔòÐźŽ«°´ÏµÍ³µÄȱʡ·½Ê½´¦Àí.
ÈçÐí¶àÓ밲ȫÓйصijÌÐò½ûÖ¹Öն˷¢ÖжÏÐÅÏ¢(BREAKºÍDELETE),ÒÔÃâ×Ô¼º
±»Óû§ÖÕ¶ËÖÕÖ¹ÔËÐÐ.
ÓÐЩÐźÅʹUNIXϵͳµÄ²úÉú½ø³ÌµÄºËÐÄת´¢(½ø³Ì½ÓÊÕµ½ÐźÅʱËùÕ¼ÄÚ´æ
µÄÄÚÈÝ,ÓÐʱº¬ÓÐÖØÒªÐÅÏ¢),´Ëϵͳ×Ó³ÌÐò¿ÉÓÃÓÚ½ûÖ¹ºËÐÄת´¢.
(3)ÎļþÊôÐÔ
*access():¼ì²âÖ¸¶¨ÎļþµÄ´æÈ¡ÄÜÁ¦ÊÇ·ñ·ûºÏÖ¸¶¨µÄ´æÈ¡ÀàÐÍ.
ÐèÒªÁ½¸ö²ÎÊý:ÎļþÃûºÍÒª¼ì²âµÄ´æÈ¡ÀàÐÍ(ÕûÊý).
´æÈ¡ÀàÐͶ¨ÒåÈçÏÂ:
0: ¼ì²éÎļþÊÇ·ñ´æÔÚ
1: ¼ì²éÊÇ·ñ¿ÉÖ´ÐÐ(ËÑË÷)
2: ¼ì²éÊÇ·ñ¿Éд
3: ¼ì²éÊÇ·ñ¿ÉдºÍÖ´ÐÐ
4: ¼ì²éÊÇ·ñ¿É¶Á
5: ¼ì²éÊÇ·ñ¿É¶ÁºÍÖ´ÐÐ
6: ¼ì²éÊÇ·ñ¿É¶Á¿Éд¿ÉÖ´ÐÐ
ÕâЩÊý×ÖµÄÒâÒåºÍchmodÃüÁîÖй涨Ðí¿É·½Ê½µÄÊý×ÖÒâÒåÏàͬ.
´Ë×Ó³ÌÐòʹÓÃʵ¼ÊµÄUIDºÍGID¼ì²âÎļþµÄ´æÈ¡ÄÜÁ¦(Ò»°ãÓÐЧµÄUIDºÍGID
ÓÃÓÚ¼ì²éÎļþ´æÈ¡ÄÜÁ¦).
·µ»ØÖµ: 0:Ðí¿É -1:²»Ðí¿É.
*chmod():½«Ö¸¶¨Îļþ»òĿ¼µÄ´æÈ¡Ðí¿É·½Ê½¸Ä³ÉеÄÐí¿É·½Ê½.
ÐèÒªÁ½¸ö²ÎÊý:ÎļþÃûºÍеĴæÈ¡Ðí¿É·½Ê½.
*chown():ͬʱ¸Ä±äÖ¸¶¨ÎļþµÄËùÓÐÕߺÍС×éµÄUIDºÍGID.(ÓëchownÃüÁî²»
ͬ).
ÓÉÓÚ´Ë×Ó³ÌÐòͬʱ¸Ä±äÎļþµÄËùÓÐÕߺÍС×é,¹Ê±ØÐëÈ¡ÏûËù²Ù×÷ÎļþµÄSUID
ºÍSGIDÐí¿É,ÒÔ·ÀÖ¹Óû§½¨Á¢SUIDºÍSGID³ÌÐò,È»ºóÔËÐÐchown()È¥»ñµÃ±ð
È˵ÄȨÏÞ.
*stat():·µ»ØÎļþµÄ״̬(ÊôÐÔ).
ÐèÒªÁ½¸ö²ÎÊý:Îļþ·¾¶ÃûºÍÒ»¸ö½á¹¹Ö¸Õë,Ö¸Ïò״̬ÐÅÏ¢µÄ´æ·Å
µÄλÖÃ.
½á¹¹¶¨ÒåÈçÏÂ:
st_mode: ÎļþÀàÐͺʹæÈ¡Ðí¿É·½Ê½
st_ino: I½ÚµãºÅ
st_dev: ÎļþËùÔÚÉ豸µÄID
st_rdev: ÌرðÎļþµÄID
st_nlink: ÎļþÁ´½ÓÊý
st_uid: ÎļþËùÓÐÕßµÄUID
st_gid: ÎļþС×éµÄGID
st_size: °´×Ö½Ú¼ÆÊýµÄÎļþ´óС
st_atime: ×îºó´æȡʱ¼ä(¶Á)
st_mtime: ×îºóÐÞ¸Äʱ¼ä(д)ºÍ×îºó״̬µÄ¸Ä±ä
st_ctime: ×îºóµÄ״̬ÐÞ¸Äʱ¼ä
·µ»ØÖµ: 0:³É¹¦ 1:ʧ°Ü
*umask():½«µ÷Óýø³Ì¼°Æä×Ó½ø³ÌµÄÎļþ½¨Á¢ÆÁ±ÎÖµÉèÖÃΪָ¶¨µÄ´æÈ¡Ðí¿É.
ÐèÒªÒ»¸ö²ÎÊý: еÄÎļþ½¨Á¢ÆÁÖµ.
(4)UIDºÍGIDµÄ´¦Àí
*getuid():·µ»Ø½ø³ÌµÄʵ¼ÊUID.
*getgid():·µ»Ø½ø³ÌµÄʵ¼ÊGID.
ÒÔÉÏÁ½¸ö×Ó³ÌÐò¿ÉÓÃÓÚÈ·¶¨ÊÇËÔÚÔËÐнø³Ì.
*geteuid():·µ»Ø½ø³ÌµÄÓÐЧUID.
*getegid():·µ»Ø½ø³ÌµÄÓÐЧGID.
ÒÔÉÏÁ½¸ö×Ó³ÌÐò¿ÉÔÚÒ»¸ö³ÌÐò²»µÃ²»È·¶¨ËüÊÇ·ñÔÚÔËÐÐijÓû§¶ø²»ÊÇÔËÐÐ
ËüµÄÓû§µÄSUID³ÌÐòʱºÜÓÐÓÃ,¿Éµ÷ÓÃËüÃÇÀ´¼ì²éÈ·Èϱ¾³ÌÐòµÄÈ·ÊÇÒÔ¸Ã
Óû§µÄSUIDÐí¿ÉÔÚÔËÐÐ.
*setuid():ÓÃÓڸıäÓÐЧµÄUID.
¶ÔÓÚÒ»°ãÓû§,´Ë×Ó³ÌÐò½ö¶ÔÒªÔÚÓÐЧºÍʵ¼ÊµÄUIDÖ®¼ä±ä»»µÄSUID³ÌÐò²Å
ÓÐÓÃ(´ÓÔÓÐЧUID±ä»»ÎªÊµ¼ÊUID),ÒÔ±£»¤½ø³Ì²»Êܵ½°²È«Î£º¦.ʵ¼ÊÉϸÃ
½ø³Ì²»ÔÙÊÇSUID·½Ê½ÔËÐÐ.
*setgid():ÓÃÓڸıäÓÐЧµÄGID.
2.±ê×¼C¿â
(1)±ê×¼I/O
*fopen():´ò¿ªÒ»¸öÎļþ¹©¶Á»òд,°²È«·½ÃæµÄ¿¼ÂÇͬopen()Ò»Ñù.
*fread(),getc(),fgetc(),gets(),scanf()ºÍfscanf():´ÓÒÑÓÉfopen()´ò
¿ª¹©¶ÁµÄÎļþÖжÁÈ¡ÐÅÏ¢.ËüÃDz¢²»¹ØÐÄÎļþµÄ´æÈ¡Ðí¿É.ÕâÒ»µã
ͬread().
*fwrite(),put(),fputc(),puts,fputs(),printf(),fprintf():дÐÅÏ¢µ½
ÒÑÓÉfopen()´ò¿ª¹©Ð´µÄÎļþÖÐ.ËüÃÇÒ²²»¹ØÐÄÎļþµÄ´æÈ¡Ðí¿É.
ͬwrite().
*getpass():´ÓÖÕ¶ËÉ϶ÁÖÁ¶à8¸ö×Ö·û³¤µÄ¿ÚÁî,²»»ØÏÔÓû§ÊäÈëµÄ×Ö·û.
ÐèÒªÒ»¸ö²ÎÊý: ÌáʾÐÅÏ¢.
¸Ã×Ó³ÌÐò½«ÌáʾÐÅÏ¢ÏÔʾÔÚÖÕ¶ËÉÏ,½ûÖ¹×Ö·û»ØÏÔ¹¦ÄÜ,´Ó/dev/tty¶ÁÈ¡¿Ú
Áî,È»ºóÔÙ»Ö¸´×Ö·û»ØÏÔ¹¦ÄÜ,·µ»Ø¸ÕÇÃÈëµÄ¿ÚÁîµÄÖ¸Õë.
*popen():½«ÔÚ(5)ÔËÐÐshellÖнéÉÜ.
(2)/etc/passwd´¦Àí
ÓÐÒ»×é×Ó³ÌÐò¿É¶Ô/etc/passwdÎļþ½øÐз½±ãµÄ´æÈ¡,¿É¶ÔÎļþ¶ÁÈ¡µ½Èë¿Ú
Ïî»òдеÄÈë¿ÚÏî»ò¸üеȵÈ.
*getpwuid():´Ó/etc/passwdÎļþÖлñÈ¡Ö¸¶¨µÄUIDµÄÈë¿ÚÏî.
*getpwnam():¶ÔÓÚÖ¸¶¨µÄµÇ¼Ãû,ÔÚ/etc/passwdÎļþ¼ìË÷Èë¿ÚÏî.
ÒÔÉÏÁ½¸ö×Ó³ÌÐò·µ»ØÒ»Ö¸Ïòpasswd½á¹¹µÄÖ¸Õë,¸Ã½á¹¹¶¨ÒåÔÚ
/usr/include/pwd.hÖÐ,¶¨ÒåÈçÏÂ:
struct passwd {
char * pw_name; /* µÇ¼Ãû */
char * pw_passwd; /* ¼ÓÃܺóµÄ¿ÚÁî */
uid_t pw_uid; /* UID */
gid_t pw_gid; /* GID */
char * pw_age; /* ´úÀíÐÅÏ¢ */
char * pw_comment; /* ×¢ÊÍ */
char * pw_gecos;
char * pw_dir; /* Ö÷Ŀ¼ */
char * pw_shell; /* ʹÓõÄshell */
};
*getpwent(),setpwent(),endpwent():¶Ô¿ÚÁîÎļþ×÷ºóÐø´¦Àí.
Ê״ε÷ÓÃgetpwent(),´ò¿ª/etc/passwd²¢·µ»ØÖ¸ÏòÎļþÖеÚÒ»¸öÈë¿ÚÏîµÄ
Ö¸Õë,±£³Öµ÷ÓÃÖ®¼äÎļþµÄ´ò¿ª×´Ì¬.
ÔÙµ÷ÓÃgetpwent()¿É˳ÐòµØ·µ»Ø¿ÚÁîÎļþÖеĸ÷Èë¿ÚÏî.
µ÷ÓÃsetpwent()°Ñ¿ÚÁîÎļþµÄÖ¸ÕëÖØÐÂÖÃΪÎļþµÄ¿ªÊ¼´¦.
ʹÓÃÍê¿ÚÁîÎļþºóµ÷ÓÃendpwent()¹Ø±Õ¿ÚÁîÎļþ.
*putpwent():Ð޸ĻòÔö¼Ó/etc/passwdÎļþÖеÄÈë¿ÚÏî.
´Ë×Ó³ÌÐò½«Èë¿ÚÏîдµ½Ò»¸öÖ¸¶¨µÄÎļþÖÐ,Ò»°ãÊÇÒ»¸öÁÙʱÎļþ,Ö±½Óд¿Ú
ÁîÎļþÊǺÜΣÏÕµÄ.×îºÃÔÚÖ´ÐÐÇ°×öÎļþ·âËø,ʹÁ½¸ö³ÌÐò²»ÄÜͬʱдһ¸ö
Îļþ.Ëã·¨ÈçÏÂ:
. ½¨Á¢Ò»¸ö¶ÀÁ¢µÄÁÙʱÎļþ,¼´/etc/passnnn,nnnÊÇPIDºÅ.
. ½¨Á¢Ð²úÉúµÄÁÙʱÎļþºÍ±ê×¼ÁÙʱÎļþ/etc/ptmpµÄÁ´,Èô½¨Á´Ê§°Ü,
ÔòΪÓÐÈËÕýÔÚʹÓÃ/etc/ptmp,µÈ´ýÖ±µ½/etc/ptmp¿ÉÓÃΪֹ»òÍ˳ö.
. ½«/etc/passwd¿½±´µ½/etc/ptmp,¿É¶Ô´ËÎļþ×öÈκÎÐÞ¸Ä.
. ½«/etc/passwdÒƵ½±¸·ÝÎļþ/etc/opasswd.
. ½¨Á¢/etc/ptmpºÍ/etc/passwdµÄÁ´.
. ¶Ï¿ª/etc/passnnnÓë/etc/ptmpµÄÁ´.
×¢Òâ:ÁÙʱÎļþÓ¦½¨Á¢ÔÚ/etcĿ¼,²ÅÄܱ£Ö¤Îļþ´¦ÓÚͬһÎļþϵͳÖÐ,½¨
Á´²ÅÄܳɹ¦,ÇÒÁÙʱÎļþ²»»á²»°²È«.´ËÍâ,ÈôÐÂÎļþÒÑ´æÔÚ,¼´±ã½¨
Á´µÄÊÇrootÓû§,Ò²½«Ê§°Ü,´Ó¶ø±£Ö¤ÁËÒ»µ©ÁÙʱÎļþ³É¹¦µØ½¨Á´ºó
ûÓÐÈËÄÜÔÙ²å½øÀ´¸ÉÈÅ.µ±È»,ʹÓÃÁÙʱÎļþµÄ³ÌÐòӦȷ±£Çå³ýËùÓÐ
ÁÙʱÎļþ,ÕýÈ·µØ²¶×½ÐźÅ.
(3)/etc/groupµÄ´¦Àí
ÓÐÒ»×éÀàËÆÓÚÇ°ÃæµÄ×Ó³ÌÐò´¦Àí/etc/groupµÄÐÅÏ¢,ʹÓÃʱ±ØÐëÓÃinclude
Óï¾ä½«/usr/include/grp.hÎļþ¼ÓÈëµ½×Ô¼ºµÄ³ÌÐòÖÐ.¸ÃÎļþ¶¨ÒåÁËgroup
½á¹¹,½«ÓÉgetgrnam(),getgrgid(),getgrent()·µ»Øgroup½á¹¹Ö¸Õë.
*getgrnam():ÔÚ/etc/groupÎļþÖÐËÑË÷Ö¸¶¨µÄС×éÃû,È»ºó·µ»ØÖ¸ÏòС×éÈë
¿ÚÏîµÄÖ¸Õë.
*getgrgid():ÀàËÆÓÚÇ°Ò»×Ó³ÌÐò,²»Í¬µÄÊÇËÑË÷Ö¸¶¨µÄGID.
*getgrent():·µ»ØgroupÎļþÖеÄÏÂÒ»¸öÈë¿ÚÏî.
*setgrent():½«groupÎļþµÄÎļþÖ¸Õë»Ö¸´µ½ÎļþµÄÆðµã.
*endgrent():ÓÃÓÚÍê³É¹¤×÷ºó,¹Ø±ÕgroupÎļþ.
*getuid():·µ»Øµ÷Óýø³ÌµÄʵ¼ÊUID.
*getpruid():ÒÔgetuid()·µ»ØµÄʵ¼ÊUIDΪ²ÎÊý,È·¶¨Óëʵ¼ÊUIDÏàÓ¦µÄµÇ¼
Ãû,»òÖ¸¶¨Ò»UIDΪ²ÎÊý.
*getlogin():·µ»ØÔÚÖÕ¶ËÉϵǼµÄÓû§µÄÖ¸Õë.
ϵͳÒÀ´Î¼ì²éSTDIN,STDOUT,STDERRÊÇ·ñÓëÖÕ¶ËÏàÁª,ÓëÖÕ¶ËÏàÁªµÄ±ê×¼Êä
ÈëÓÃÓÚÈ·¶¨ÖÕ¶ËÃû,ÖÕ¶ËÃûÓÃÓÚ²éÕÒÁÐÓÚ/etc/utmpÎļþÖеÄÓû§,¸ÃÎļþ
ÓÉloginά»¤,ÓÉwho³ÌÐòÓÃÀ´È·ÈÏÓû§.
*cuserid():Ê×Ïȵ÷ÓÃgetlogin(),Èôgetlogin()·µ»ØNULLÖ¸Õë,ÔÙµ÷ÓÃ
getpwuid(getuid()).
*ÒÔÏÂΪÃüÁî:
*logname:ÁгöµÇ¼½øÖն˵ÄÓû§Ãû.
*who am i:ÏÔʾ³öÔËÐÐÕâÌõÃüÁîµÄÓû§µÄµÇ¼Ãû.
*id:ÏÔʾʵ¼ÊµÄUIDºÍGID(ÈôÓÐЧµÄUIDºÍGIDºÍʵ¼ÊµÄ²»Í¬Ê±Ò²ÏÔʾÓÐЧµÄ
UIDºÍGID)ºÍÏàÓ¦µÄµÇ¼Ãû.
(4)¼ÓÃÜ×Ó³ÌÐò
1977Äê1ÔÂ,NBSÐû²¼Ò»¸öÓÃÓÚÃÀ¹úÁª°îÕþ¸®ADPϵͳµÄÍøÂçµÄ±ê×¼¼ÓÃÜ·¨:Êý
¾Ý¼ÓÃܱê×¼¼´DESÓÃÓÚ·Ç»úÃÜÓ¦Ó÷½Ãæ.DESÒ»´Î´¦Àí64BITSµÄ¿é,56λµÄ¼Ó
Ãܼü.
*setkey(),encrypt():ÌṩÓû§¶ÔDESµÄ´æÈ¡.
´ËÁ½×Ó³ÌÐò¶¼È¡64BITS³¤µÄ×Ö·ûÊý×é,Êý×éÖеÄÿ¸öÔªËØ´ú±íÒ»¸öλ,Ϊ0
»ò1.setkey()ÉèÖý«°´DES´¦ÀíµÄ¼ÓÃܼü,ºöÂÔÿµÚ8λ¹¹³ÉÒ»¸ö56λµÄ¼Ó
Ãܼü.encrypt()È»ºó¼ÓÃÜ»ò½âÃܸø¶¨µÄ64BITS³¤µÄÒ»¿é,¼ÓÃÜ»ò½âÃÜÈ¡¾ö
ÓÚ¸Ã×Ó³ÌÐòµÄµÚ¶þ¸ö±äÔª,0:¼ÓÃÜ 1:½âÃÜ.
*crypt():ÊÇUNIXϵͳÖеĿÚÁî¼ÓÃܳÌÐò,Ò²±»/usr/lib/makekeyÃüÁîµ÷ÓÃ.
crypt()×Ó³ÌÐòÓëcryptÃüÁîÎÞ¹Ø,ËüÓë/usr/lib/makekeyÒ»ÑùÈ¡8¸ö×Ö·û³¤
µÄ¹Ø¼ü´Ê,2¸ösalt×Ö·û.¹Ø¼ü´ÊË͸øsetkey(),salt×Ö·ûÓÃÓÚ»ìºÏencrypt()
ÖеÄDESËã·¨,×îÖÕµ÷ÓÃencrypt()Öظ´25´Î¼ÓÃÜÒ»¸öÏàͬµÄ×Ö·û´®.
·µ»Ø¼ÓÃܺóµÄ×Ö·û´®Ö¸Õë.
(5)ÔËÐÐshell
*system():ÔËÐÐ/bin/shÖ´ÐÐÆä²ÎÊýÖ¸¶¨µÄÃüÁî,µ±Ö¸¶¨ÃüÁîÍê³Éʱ·µ»Ø.
*popen():ÀàËÆÓÚsystem(),²»Í¬µÄÊÇÃüÁîÔËÐÐʱ,Æä±ê×¼ÊäÈë»òÊä³öÁªµ½ÓÉ
popen()·µ»ØµÄÎļþÖ¸Õë.
¶þÕ߶¼µ÷ÓÃfork(),exec(),popen()»¹µ÷ÓÃpipe(),Íê³É¸÷×ԵŤ×÷,Òò¶ø
fork()ºÍexec()µÄ°²È«·½ÃæµÄ¿¼ÂÇ¿ªÊ¼Æð×÷ÓÃ.
3.д°²È«µÄC³ÌÐò
Ò»°ãÓÐÁ½·½ÃæµÄ°²È«ÎÊÌâ,ÔÚд³ÌÐòʱ±ØÐ뿼ÂÇ:
(1)È·±£×Ô¼º½¨Á¢µÄÈκÎÁÙʱÎļþ²»º¬ÓлúÃÜÊý¾Ý,Èç¹ûÓлúÃÜÊý¾Ý,ÉèÖÃ
ÁÙʱÎļþ½ö¶Ô×Ô¼º¿É¶Á/д.È·±£½¨Á¢ÁÙʱÎļþµÄĿ¼½ö¶Ô×Ô¼º¿Éд.
(2)È·±£×Ô¼ºÒªÔËÐеÄÈκÎÃüÁî(ͨ¹ýsystem(),popen(),execlp(),
execvp()ÔËÐеÄÃüÁî)µÄÈ·ÊÇ×Ô¼ºÒªÔËÐеÄÃüÁî,¶ø²»ÊÇÆäËüʲôÃü
Áî,ÓÈÆäÊÇ×Ô¼ºµÄ³ÌÐòΪSUID»òSGIDÐí¿ÉʱҪСÐÄ.
µÚÒ»·½Ãæ±È½Ï¼òµ¥,ÔÚ³ÌÐò¿ªÊ¼Ç°µ÷ÓÃumask(077).ÈôҪʹÎļþ¶ÔÆäËûÈË¿É
¶Á,¿ÉÔÙµ÷chmod(),Ò²¿ÉÓÃÏÂÊöÓïÃû½¨Á¢Ò»¸ö""²»¿É¼û""µÄÁÙʱÎļþ.
creat(""/tmp/xxx"",0);
file=open(""/tmp/xxx"",O_RDWR);
unlink(""/tmp/xxx"");
Îļþ/tmp/xxx½¨Á¢ºó,´ò¿ª,È»ºó¶Ï¿ªÁ´,µ«ÊÇ·ÖÅä¸ø¸ÃÎļþµÄ´æ´¢Æ÷²¢Î´É¾
³ý,Ö±µ½×îÖÕÖ¸Ïò¸ÃÎļþµÄÎļþͨµÀ±»¹Ø±Õʱ²Å±»É¾³ý.´ò¿ª¸ÃÎļþµÄ½ø³Ì
ºÍËüµÄÈκÎ×Ó½ø³Ì¶¼¿É´æÈ¡Õâ¸öÁÙʱÎļþ,¶øÆäËü½ø³Ì²»ÄÜ´æÈ¡¸ÃÎļþ,Òò
ΪËüÔÚ/tmpÖеÄĿ¼ÏîÒѱ»unlink()ɾ³ý.
µÚ¶þ·½Ãæ±È½Ï¸´ÔÓ¶ø΢Ãî,ÓÉÓÚsystem(),popen(),execlp(),execvp()Ö´ÐÐ
ʱ,Èô²»¸ø³öÖ´ÐÐÃüÁîµÄȫ·¾¶,¾ÍÄÜ""Æ""Óû§µÄ³ÌÐòÈ¥Ö´Ðв»Í¬µÄÃüÁî.Òò
Ϊϵͳ×Ó³ÌÐòÊǸù¾ÝPATH±äÁ¿È·¶¨ÄÄÖÖ˳ÐòËÑË÷ÄÄЩĿ¼,ÒÔÑ°ÕÒÖ¸¶¨µÄÃü
Áî,Õâ³ÆΪSUIDÏݾ®.×ȫµÄ°ì·¨ÊÇÔÚµ÷ÓÃsystem()Ç°½«ÓÐЧUID¸Ä±ä³Éʵ
¼ÊUID,ÁíÒ»ÖֱȽϺõķ½·¨ÊÇÒÔȫ·¾¶ÃûÃüÁî×÷Ϊ²ÎÊý.execl(),execv(),
execle(),execve()¶¼ÒªÇóȫ·¾¶Ãû×÷Ϊ²ÎÊý.ÓйØSUIDÏݾ®µÄÁíÒ»·½Ê½ÊÇ
ÔÚ³ÌÐòÖÐÉèÖÃPATH,ÓÉÓÚsystem()ºÍpopen()¶¼Æô¶¯shell,¹Ê¿ÉʹÓÃshell¾ä
·¨.Èç:
system(""PATH=/bin:/usr/bin cd"");
ÕâÑùÔÊÐíÓû§ÔËÐÐϵͳÃüÁî¶ø²»±ØÖªµÀÒªÖ´ÐеÄÃüÁîÔÚÄĸöĿ¼ÖÐ,µ«ÕâÖÖ
·½·¨²»ÄÜÓÃÓÚexeclp(),execvp()ÖÐ,ÒòΪËüÃDz»ÄÜÆô¶¯shellÖ´Ðе÷ÓÃÐòÁÐ
´«µÝµÄÃüÁî×Ö·û´®.
¹ØÓÚshell½âÊÍ´«µÝ¸øsystem()ºÍpopen()µÄÃüÁîÐеķ½Ê½,ÓÐÁ½¸öÆäËüµÄÎÊ
Ìâ:
*shellʹÓÃIFS shell±äÁ¿ÖеÄ×Ö·û,½«ÃüÁîÐзֽâ³Éµ¥´Ê(ͨ³£Õâ¸ö
shell±äÁ¿ÖÐÊÇ¿Õ¸ñ,tab,»»ÐÐ),ÈçIFSÖÐÊÇ/,×Ö·û´®/bin/ed±»½âÊͳɵ¥´Ê
bin,½ÓÏÂÀ´Êǵ¥´Êed,´Ó¶øÒýÆðÃüÁîÐеÄÇú½â.
ÔÙÇ¿µ÷Ò»´Î:ÔÚͨ¹ý×Ô¼ºµÄ³ÌÐòÔËÐÐÁíÒ»¸ö³ÌÐòÇ°,Ó¦½«ÓÐЧUID¸ÄΪʵ¼ÊµÄ
UID,µÈÁíÒ»¸ö³ÌÐòÍ˳öºó,ÔÙ½«ÓÐЧUID¸Ä»ØÔÀ´µÄÓÐЧUID.
SUID/SGID³ÌÐòÖ¸µ¼×¼Ôò
(1)²»ÒªÐ´SUID/SGID³ÌÐò,´ó¶àÊýʱºòÎ޴˱ØÒª.
(2)ÉèÖÃSGIDÐí¿É,²»ÒªÉèÖÃSUIDÐí¿É.Ó¦¶À×Ô½¨Á¢Ò»¸öеÄС×é.
(3)²»ÒªÓÃexec()Ö´ÐÐÈκγÌÐò.¼Çסexec()Ò²±»system()ºÍpopen()µ÷ÓÃ.
. ÈôÒªµ÷ÓÃexec()(»òsystem(),popen()),Ó¦ÊÂÏÈÓÃsetgid(getgid())
½«ÓÐЧGIDÖüÓʵ¼ÊGID.
. Èô²»ÄÜÓÃsetgid(),Ôòµ÷ÓÃsystem()»òpopen()ʱ,Ó¦ÉèÖÃIFS:
popen(""IFS=
;export IFS;/bin/ls"",""r"");
. ʹÓÃÒªÖ´ÐеÄÃüÁîµÄȫ·¾¶Ãû.
. Èô²»ÄÜʹÓÃȫ·¾¶Ãû,ÔòÓ¦ÔÚÃüÁîÇ°ÏÈÉèÖÃPATH:
popen(""IFS=
;export IFS;PATH=/bin:/usr/bin;/bin/ls"",""r"");
. ²»Òª½«Óû§¹æ¶¨µÄ²ÎÊý´«¸øsystem()»òpopen();ÈôÎÞ·¨±ÜÃâÔòÓ¦¼ì²é
±äÔª×Ö·û´®ÖÐÊÇ·ñÓÐÌØÊâµÄshell×Ö·û.
. ÈôÓû§Óиö´ó³ÌÐò,µ÷ÓÃexec()Ö´ÐÐÐí¶àÆäËü³ÌÐò,ÕâÖÖÇé¿öϲ»Òª½«
´ó³ÌÐòÉèÖÃΪSGIDÐí¿É.¿ÉÒÔдһ¸ö(»ò¶à¸ö)¸üС,¸ü¼òµ¥µÄSGID³ÌÐò
Ö´ÐбØÐë¾ßÓÐSGIDÐí¿ÉµÄÈÎÎñ,È»ºóÓÉ´ó³ÌÐòÖ´ÐÐÕâЩСSGID³ÌÐò.
(4)ÈôÓû§±ØÐëʹÓÃSUID¶ø²»ÊÇSGID,ÒÔÏàͬµÄ˳Ðò¼Çס(2),(3)ÏîÄÚÈÝ,²¢
ÏàÓ¦µ÷Õû.²»ÒªÉèÖÃrootµÄSUIDÐí¿É.Ñ¡Ò»¸öÆäËü»§Í·.
(5)ÈôÓû§Ïë¸øÓèÆäËûÈËÖ´ÐÐ×Ô¼ºµÄshell³ÌÐòµÄÐí¿É,µ«ÓÖ²»ÏëÈÃËûÃÇÄÜ
¶Á¸Ã³ÌÐò,¿É½«³ÌÐòÉèÖÃΪ½öÖ´ÐÐÐí¿É,²¢Ö»ÄÜͨ¹ý×Ô¼ºµÄshell³ÌÐòÀ´
ÔËÐÐ.
±àÒë,°²×°SUID/SGID³ÌÐòʱӦ°´ÏÂÃæµÄ·½·¨
(1)È·±£ËùÓеÄSUID(SGID)³ÌÐòÊǶÔÓÚС×éºÍÆäËûÓû§¶¼ÊDz»¿ÉдµÄ,´æÈ¡
ȨÏÞµÄÏÞÖƵÍÓÚ4755(2755)½«´øÀ´Âé·³.Ö»ÄܸüÑϸñ.4111(2111)½«Ê¹
ÆäËûÈËÎÞ·¨Ñ°ÕÒ³ÌÐòÖеݲȫ©¶´.
(2)¾¯ÌèÍâÀ´µÄ±àÂëºÍmake/install·½·¨
. ijЩmake/install·½·¨²»¼ÓÑ¡ÔñµØ½¨Á¢SUID/SGID³ÌÐò.
. ¼ì²éÎ¥±³ÉÏÊöÖ¸µ¼ÔÔòµÄSUID/SGIDÐí¿ÉµÄ±àÂë.
. ¼ì²émakefileÎļþÖпÉÄܽ¨Á¢SUID/SGIDÎļþµÄÃüÁî.
4.root³ÌÐòµÄÉè¼Æ
ÓÐÈô¸É¸ö×Ó³ÌÐò¿ÉÒÔ´ÓÓÐЧUIDΪ0µÄ½ø³ÌÖе÷ÓÃ.Ðí¶àÇ°ÃæÌáµ½µÄ×Ó³ÌÐò,
µ±´Óroot½ø³ÌÖе÷ÓÃʱ,½«Íê³ÉºÍÔÀ´²»Í¬µÄ´¦Àí.Ö÷ÒªÊǺöÂÔÁËÐí¿ÉȨÏ޵ļì
²é.
ÓÉrootÓû§ÔËÐеijÌÐòµ±È»ÊÇroot½ø³Ì(SUID³ýÍâ),ÒòÓÐЧUIDÓÃÓÚÈ·¶¨ÎÄ
¼þµÄ´æȡȨÏÞ,ËùÒÔ´Ó¾ßÓÐrootµÄ³ÌÐòÖÐ,µ÷ÓÃfork()²úÉúµÄ½ø³Ì,Ò²ÊÇroot½ø³Ì.
(1)setuid():´Óroot½ø³Ìµ÷ÓÃsetuid()ʱ,Æä´¦ÀíÓÐËù²»Í¬,setuid()½«°ÑÓÐ
ЧµÄºÍʵ¼ÊµÄUID¶¼ÖÃΪָ¶¨µÄÖµ.Õâ¸öÖµ¿ÉÒÔÊÇÈκÎÕûÐÍÊý.¶ø¶Ô·Çroot
½ø³ÌÔò½öÄÜÒÔʵ¼ÊUID»ò±¾½ø³ÌÔÀ´ÓÐЧµÄUIDΪ±äÁ¿Öµµ÷ÓÃsetuid().
(2)setgid():ÔÚϵͳ½ø³ÌÖе÷ÓÃsetgid()ʱ,Óësetuid()ÀàËÆ,½«Êµ¼ÊºÍÓÐЧ
µÄGID¶¼¸Ä±ä³ÉÆä²ÎÊýÖ¸¶¨µÄÖµ.
* µ÷ÓÃÒÔÉÏÁ½¸ö×Ó³ÌÐòʱ,Ó¦µ±×¢ÒâÏÂÃ漸µã:
. µ÷ÓÃÒ»´Îsetuid()(setgid())½«Í¬Ê±ÉèÖÃÓÐЧºÍʵ¼ÊUID(GID),¶ÀÁ¢·Ö
±ðÉèÖÃÓÐЧ»òʵ¼ÊUID(GID)¹ÌÈ»ºÜºÃ,µ«ÎÞ·¨×öµ½Õâµã.
. setuid()(setgid())¿É½«ÓÐЧºÍʵ¼ÊUID(GID)ÉèÖóÉÈκÎÕûÐÍÊý,ÆäÊý
Öµ²»±ØÒ»¶¨Óë/etc/passwd(/etc/group)ÖÐÓû§(С×é)Ïà¹ØÁª.
. Ò»µ©³ÌÐòÒÔÒ»¸öÓû§µÄUIDÁËsetuid(),¸Ã³ÌÐò¾Í²»ÔÙ×öΪrootÔËÐÐ,Ò²
²»¿ÉÄÜÔÙ»ñrootÌØȨ.
(3)chown():µ±root½ø³ÌÔËÐÐchown()ʱ,chown()½«²»É¾³ýÎļþµÄSUIDºÍ/»ò
SGIDÐí¿É,µ«µ±·Çroot½ø³ÌÔËÐÐchown()ʱ,chown()½«È¡ÏûÎļþµÄSUIDºÍ/
»òSGIDÐí¿É.
(4)chroot():¸Ä±ä½ø³Ì¶Ô¸ùĿ¼µÄ¸ÅÄî,µ÷ÓÃchroot()ºó,½ø³Ì¾Í²»Äܰѵ±Ç°
¹¤×÷Ŀ¼¸Ä±äµ½ÐµĸùĿ¼ÒÔÉϵÄÈÎһĿ¼,ËùÓÐÒÔ/¿ªÊ¼µÄ·¾¶ËÑË÷,¶¼
´ÓеĸùĿ¼¿ªÊ¼.
(5)mknod():ÓÃÓÚ½¨Á¢Ò»¸öÎļþ,ÀàËÆÓÚcreat(),²î±ðÊÇmknod()²»·µ»ØËù´ò¿ª
ÎļþµÄÎļþÃèÊö·û,²¢ÇÒÄܽ¨Á¢ÈκÎÀàÐ͵ÄÎļþ(ÆÕͨÎļþ,ÌØÊâÎļþ,Ŀ¼
Îļþ).Èô´Ó·Çroot½ø³Ìµ÷ÓÃmknod()½«Ö´ÐÐʧ°Ü,Ö»Óн¨Á¢FIFOÌرðÎļþ
(ÓÐÃû¹ÜµÀÎļþ)ʱÀýÍâ,ÆäËüÈκÎÇé¿öÏÂ,±ØÐë´Óroot½ø³Ìµ÷ÓÃmknod().ÓÉ
ÓÚcreat()½öÄܽ¨Á¢ÆÕͨÎļþ,mknod()Êǽ¨Á¢Ä¿Â¼ÎļþµÄΨһ;¾¶,Òò¶ø½ö
ÓÐrootÄܽ¨Á¢Ä¿Â¼,Õâ¾ÍÊÇΪʲômkdirÃüÁî¾ßÓÐSUIDÐí¿É²¢ÊôrootËùÓÐ.
Ò»°ã²»´Ó³ÌÐòÖе÷ÓÃmknod().ͨ³£ÓÃ/etc/mknodÃüÁÁ¢ÌرðÉ豸Îļþ¶ø
ÕâЩÎļþÒ»°ã²»ÄÜÔÚʹÓÃ×Åʱ½¨Á¢ºÍɾ³ý,mkdirÃüÁîÓÃÓÚ½¨Á¢Ä¿Â¼.µ±ÓÃ
mknod()½¨Á¢ÌرðÎļþʱ,Ó¦µ±×¢ÒâÈ·´ÓËù½¨µÄÌرðÎļþ²»ÔÊÐí´æÈ¡ÄÚ´æ,
´ÅÅÌ,Öն˺ÍÆäËüÉ豸.
(6)unlink():ÓÃÓÚɾ³ýÎļþ.²ÎÊýÊÇҪɾ³ýÎļþµÄ·¾¶ÃûÖ¸Õë.µ±Ö¸¶¨ÁËĿ¼
ʱ,±ØÐë´Óroot½ø³Ìµ÷ÓÃunlink(),ÕâÊDZØÐë´Óroot½ø³Ìµ÷ÓÃunlink()µÄΨ
Ò»Çé¿ö,Õâ¾ÍÊÇΪʲôrmdirÃüÁî¾ßÓÐrootµÄSGIDÐí¿ÉµÄÔÒò.
(7)mount(),umount():ÓÉroot½ø³Ìµ÷ÓÃ,·Ö±ðÓÃÓÚ°²×°ºÍ²ðжÎļþϵͳ.ÕâÁ½
¸ö×Ó³ÌÐòÒ²±»mountºÍumountÃüÁîµ÷ÓÃ,Æä²ÎÊý»ù±¾ºÍÃüÁîµÄ²ÎÊýÏàͬ.µ÷
ÓÃmount(),ÐèÒª¸ø³öÒ»¸öÌرðÎļþºÍÒ»¸öĿ¼µÄÖ¸Õë,ÌرðÎļþÉϵÄÎļþ
ϵͳ¾Í½«°²×°ÔÚ¸ÃĿ¼ÏÂ,µ÷ÓÃʱ»¹Òª¸ø³öÒ»¸ö±êʶѡÏî,Ö¸¶¨±»°²×°µÄÎÄ
¼þϵͳҪ±»¶Á/д(0)»¹Êǽö¶Á(1).umount()µÄ²ÎÊýÊÇÒªÒ»¸öÒª²ðжµÄÌرð
ÎļþµÄÖ¸Õë.