Apue Chapter 6 習題答案

Q6.1 若是系統使用陰影文件,那麼如何取得加密口令?node

If the system uses a shadow file and we need to obtain the encrypted password, how do we do so ?
dom

咱們能夠用 struct spwd *getspnam(const char *name); 去/etc/shadow 裏去查看用戶密碼部分。 ide


[root@clstore3 ~]# ./getspwnam  rootthis

argc is 2加密

the string is rootspa

the string is $6$z5AyhdWr$9V0IBMBGLDE82.ueFZVv5WQRAwiTOYhXk..RtmHR7xUqlAGPTneEuILxqxxzbJIhRkkXoVmbs4yUyCq839KZR0ci

the string is 16584get

[root@clstore3 ~]# cat getspwnam.cstring

#include <apue.h>it

#include <shadow.h>

int

main (int argc, char *argv[])

{

struct spwd *

getspnam(const char *name)

{

  struct spwd *ptr;

  setpwent();

  while (( ptr = getspent()) != NULL )

    if ( strcmp(name, ptr->sp_namp) == 0)

     break;

printf("the string is %s\n", ptr->sp_namp);

printf("the string is %s\n", ptr->sp_pwdp);

printf("the string is %ld\n", ptr->sp_lstchg);

printf("the string is %ld\n", ptr->sp_expire);

printf("the string is %ld\n", ptr->sp_inact);

printf("the string is %ld\n", ptr->sp_expire);

    endspent();

   return(ptr);

}

   printf("argc is %ld\n", argc);

   if (argc != 2 )

{


    printf("usage:  getspnam  <user account>\n");

   exit(1);

 }


if ( getspnam(argv[1]) < 0  )

   printf("get error");


}


Q6.2 假設你有超級用戶權限,而且系統使用的陰影口令,從新考慮上一道題?

If you have superuser access and your system uses shadow passowords, implement the previous exercise. 

只有你有超級用戶權限才能去取,看標準答案多簡潔,其實個人也還行,:) :) 

522868 $ ./getspnam1 root

getspnam error: Permission denied

[root@clstore3 ~]# ./getspnam1 root

sp_pwdp = $6$z5AyhdWr$9V0IBMBGLDE82.ueFZVv5WQRAwiTOYhXk..RtmHR7xUqlAGPTneEuILxqxxzbJIhRkkXoVmbs4yUyCq839KZR0

代碼部分,

[root@clstore3 ~]# cat getspnam1.c

#include "apue.h"

#include <shadow.h>

int

main (int argc, char *argv[])

{

struct spwd *ptr;

   if(( ptr = getspnam(argv[1])) == NULL)

     err_sys("getspnam error");

printf("sp_pwdp = %s\n", ptr->sp_pwdp == NULL ||  ptr->sp_pwdp[0] == 0 ? "(null)" : ptr->sp_pwdp);

  exit(0);

}

Q6.3 編寫一個程序,它調用uname 並輸出utsname 結構中的因此字段,將該輸出與uname命令的輸出結果進行比較。 

[root@clstore3 ~]# ./uname

system name = Linux

nodename = clstore3.sha.chin.seagate.com

release  = 2.6.32-431.el6.x86_64

version = #1 SMP Fri Nov 22 03:15:09 UTC 2013

machine = x86_64

 uname 是這樣的。 

[root@clstore3 ~]# uname -a

Linux clstore3.sha.chin.seagate.com 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux


[root@clstore3 ~]# cat uname.c

#include "apue.h"

#include <shadow.h>

#include <sys/utsname.h>


int

main (int argc, char *argv[])

{

     int uname ( struct utsname *name);

     struct utsname ptr;

    int fb;

     fb = uname(&ptr);

    if ( fb < 0 )

     printf("error ");

   else

    {

    printf("system name = %s\n", ptr.sysname);

    printf("nodename = %s\n", ptr.nodename);

    printf("release  = %s\n", ptr.release);

    printf("version = %s\n", ptr.version);

    printf("machine = %s\n", ptr.machine);


#if _UTSNAME_DOMAIN_LENGTH - 0

    /* Name of the domain of this node on the network.  */

# ifdef __USE_GNU

 printf("domain name = %s\n", ptr.domainname);

#endif

#endif

    }

  exit(0);

}

Q6.4 計算可有time_t 數據類型表示的最近時間,若是超出了這一時間將會若是?


Q6.5 編寫一程序,獲取當前時間,並使用 strftime將輸出結果轉換爲相似的data 命令的默認輸出,將環境變量TZ 設置爲不一樣值,觀察輸出結果/

能夠看到,TZ 不一樣,它的輸出也不一樣, 這個和6.11的腳本是如出一轍的,重標準答案抄過來的。 

[root@clstore3 ~]# date

Thu Nov 26 10:50:12 CST 2015

[root@clstore3 ~]# TZ=Japan ./date

Thu Nov 26 11:50:25 JST 2015

[root@clstore3 ~]# cat date.c

#include "apue.h"

#include <time.h>


int

main (void)

{


   time_t caltime;

   struct tm *tm;

   char line[MAXLINE];

  if (( caltime = time(NULL)) == -1 )

  err_sys("time error");


  if ((tm = localtime(&caltime)) == NULL)

    err_sys("localtime error");

 if(strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", tm) == 0 )

   err_sys("strftime error");


  fputs(line, stdout);

  exit(0);

}

相關文章
相關標籤/搜索