Apue Chapter 6 习题答案

Q6.1 如果系统使用阴影文件,那么如何取得加密口令?

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

我们可以用 struct spwd *getspnam(const char *name); 去/etc/shadow 里去查看用户密码部分。 


[root@clstore3 ~]# ./getspwnam  root

argc is 2

the string is root

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

the string is 16584

[root@clstore3 ~]# cat getspwnam.c

#include <apue.h>

#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);

}


你可能感兴趣的:(加密,用户,程序,如何)