(root)/
glibc-2.38/
nis/
nis_print.c
       1  /* Copyright (c) 1997-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <time.h>
      19  #include <string.h>
      20  #include <libintl.h>
      21  #include <stdint.h>
      22  
      23  #include <rpcsvc/nis.h>
      24  #include <shlib-compat.h>
      25  
      26  static const char *
      27  nis_nstype2str (const nstype type)
      28  {
      29  
      30  /* Name service names mustn't be translated, only UNKNOWN needs it */
      31  
      32    switch (type)
      33      {
      34      case NIS:
      35        return "NIS";
      36      case SUNYP:
      37        return "SUNYP";
      38      case IVY:
      39        return "IVY";
      40      case DNS:
      41        return "DNS";
      42      case X500:
      43        return "X500";
      44      case DNANS:
      45        return "DNANS";
      46      case XCHS:
      47        return "XCHS";
      48      case CDS:
      49        return "CDS";
      50      default:
      51        return N_("UNKNOWN");
      52      }
      53  }
      54  
      55  static const char *
      56  nis_objtype (const zotypes type)
      57  {
      58    switch (type)
      59      {
      60      case NIS_BOGUS_OBJ:
      61        return _("BOGUS OBJECT");
      62      case NIS_NO_OBJ:
      63        return _("NO OBJECT");
      64      case NIS_DIRECTORY_OBJ:
      65        return _("DIRECTORY");
      66      case NIS_GROUP_OBJ:
      67        return _("GROUP");
      68      case NIS_TABLE_OBJ:
      69        return _("TABLE");
      70      case NIS_ENTRY_OBJ:
      71        return _("ENTRY");
      72      case NIS_LINK_OBJ:
      73        return _("LINK");
      74      case NIS_PRIVATE_OBJ:
      75        return _("PRIVATE\n");
      76      default:
      77        return _("(Unknown object");
      78      }
      79  }
      80  
      81  static void
      82  print_ttl (const uint32_t ttl)
      83  {
      84    uint32_t time, s, m, h;
      85  
      86    time = ttl;
      87  
      88    h = time / (60 * 60);
      89    time %= (60 * 60);
      90    m = time / 60;
      91    time %= 60;
      92    s = time;
      93    printf ("%u:%u:%u\n", h, m, s);
      94  }
      95  
      96  static void
      97  print_flags (const unsigned int flags)
      98  {
      99    fputs ("(", stdout);
     100  
     101    if (flags & TA_SEARCHABLE)
     102      fputs ("SEARCHABLE, ", stdout);
     103  
     104    if (flags & TA_BINARY)
     105      {
     106        fputs ("BINARY DATA", stdout);
     107        if (flags & TA_XDR)
     108  	fputs (", XDR ENCODED", stdout);
     109        if (flags & TA_ASN1)
     110  	fputs (", ASN.1 ENCODED", stdout);
     111        if (flags & TA_CRYPT)
     112  	fputs (", ENCRYPTED", stdout);
     113      }
     114    else
     115      {
     116        fputs ("TEXTUAL DATA", stdout);
     117        if (flags & TA_SEARCHABLE)
     118  	{
     119  	  if (flags & TA_CASE)
     120  	    fputs (", CASE INSENSITIVE", stdout);
     121  	  else
     122  	    fputs (", CASE SENSITIVE", stdout);
     123  	}
     124      }
     125  
     126    fputs (")\n", stdout);
     127  }
     128  
     129  static void
     130  nis_print_objtype (enum zotypes type)
     131  {
     132    printf ("%s\n", nis_objtype (type));
     133  }
     134  
     135  void
     136  nis_print_rights (const unsigned int access)
     137  {
     138    char result[17];
     139    unsigned int acc;
     140    int i;
     141  
     142    acc = access;			/* Parameter is const ! */
     143    result[i = 16] = '\0';
     144    while (i > 0)
     145      {
     146        i -= 4;
     147        result[i + 0] = (acc & NIS_READ_ACC) ? 'r' : '-';
     148        result[i + 1] = (acc & NIS_MODIFY_ACC) ? 'm' : '-';
     149        result[i + 2] = (acc & NIS_CREATE_ACC) ? 'c' : '-';
     150        result[i + 3] = (acc & NIS_DESTROY_ACC) ? 'd' : '-';
     151  
     152        acc >>= 8;
     153      }
     154    fputs (result, stdout);
     155  }
     156  libnsl_hidden_nolink_def (nis_print_rights, GLIBC_2_1)
     157  
     158  void
     159  nis_print_directory (const directory_obj *dir)
     160  {
     161    nis_server *sptr;
     162    unsigned int i;
     163  
     164    printf (_("Name : `%s'\n"), dir->do_name);
     165    printf (_("Type : %s\n"), nis_nstype2str (dir->do_type));
     166    sptr = dir->do_servers.do_servers_val;
     167    for (i = 0; i < dir->do_servers.do_servers_len; i++)
     168      {
     169        if (i == 0)
     170  	fputs (_("Master Server :\n"), stdout);
     171        else
     172  	fputs (_("Replicate :\n"), stdout);
     173        printf (_("\tName       : %s\n"), sptr->name);
     174        fputs (_("\tPublic Key : "), stdout);
     175        switch (sptr->key_type)
     176  	{
     177  	case NIS_PK_NONE:
     178  	  fputs (_("None.\n"), stdout);
     179  	  break;
     180  	case NIS_PK_DH:
     181  	  printf (_("Diffie-Hellmann (%d bits)\n"),
     182  		  (sptr->pkey.n_len - 1) * 4);
     183  	  /* sptr->pkey.n_len counts the last 0, too */
     184  	  break;
     185  	case NIS_PK_RSA:
     186  	  printf (_("RSA (%d bits)\n"), (sptr->pkey.n_len - 1) * 4);
     187  	  break;
     188  	case NIS_PK_KERB:
     189  	  fputs (_("Kerberos.\n"), stdout);
     190  	  break;
     191  	default:
     192  	  printf (_("Unknown (type = %d, bits = %d)\n"), sptr->key_type,
     193  		  (sptr->pkey.n_len - 1) * 4);
     194  	  break;
     195  	}
     196  
     197        if (sptr->ep.ep_len != 0)
     198  	{
     199  	  unsigned int j;
     200  
     201  	  endpoint *ptr;
     202  	  ptr = sptr->ep.ep_val;
     203  	  printf (_("\tUniversal addresses (%u)\n"), sptr->ep.ep_len);
     204  	  for (j = 0; j < sptr->ep.ep_len; j++)
     205  	    {
     206  	      printf ("\t[%d] - ", j + 1);
     207  	      if (ptr->proto != NULL && ptr->proto[0] != '\0')
     208  		printf ("%s, ", ptr->proto);
     209  	      else
     210  		printf ("-, ");
     211  	      if (ptr->family != NULL && ptr->family[0] != '\0')
     212  		printf ("%s, ", ptr->family);
     213  	      else
     214  		printf ("-, ");
     215  	      if (ptr->uaddr != NULL && ptr->uaddr[0] != '\0')
     216  		printf ("%s\n", ptr->uaddr);
     217  	      else
     218  		fputs ("-\n", stdout);
     219  	      ptr++;
     220  	    }
     221  	}
     222        sptr++;
     223      }
     224  
     225    fputs (_("Time to live : "), stdout);
     226    print_ttl (dir->do_ttl);
     227    fputs (_("Default Access rights :\n"), stdout);
     228    if (dir->do_armask.do_armask_len != 0)
     229      {
     230        oar_mask *ptr;
     231  
     232        ptr = dir->do_armask.do_armask_val;
     233        for (i = 0; i < dir->do_armask.do_armask_len; i++)
     234  	{
     235  	  nis_print_rights (ptr->oa_rights);
     236  	  printf (_("\tType         : %s\n"), nis_objtype (ptr->oa_otype));
     237  	  fputs (_("\tAccess rights: "), stdout);
     238  	  nis_print_rights (ptr->oa_rights);
     239  	  fputs ("\n", stdout);
     240  	  ptr++;
     241  	}
     242      }
     243  }
     244  libnsl_hidden_nolink_def (nis_print_directory, GLIBC_2_1)
     245  
     246  void
     247  nis_print_group (const group_obj *obj)
     248  {
     249    unsigned int i;
     250  
     251    fputs (_("Group Flags :"), stdout);
     252    if (obj->gr_flags)
     253      printf ("0x%08X", obj->gr_flags);
     254    fputs (_("\nGroup Members :\n"), stdout);
     255  
     256    for (i = 0; i < obj->gr_members.gr_members_len; i++)
     257      printf ("\t%s\n", obj->gr_members.gr_members_val[i]);
     258  }
     259  libnsl_hidden_nolink_def (nis_print_group, GLIBC_2_1)
     260  
     261  void
     262  nis_print_table (const table_obj *obj)
     263  {
     264    unsigned int i;
     265  
     266    printf (_("Table Type          : %s\n"), obj->ta_type);
     267    printf (_("Number of Columns   : %d\n"), obj->ta_maxcol);
     268    printf (_("Character Separator : %c\n"), obj->ta_sep);
     269    printf (_("Search Path         : %s\n"), obj->ta_path);
     270    fputs (_("Columns             :\n"), stdout);
     271    for (i = 0; i < obj->ta_cols.ta_cols_len; i++)
     272      {
     273        printf (_("\t[%d]\tName          : %s\n"), i,
     274  	      obj->ta_cols.ta_cols_val[i].tc_name);
     275        fputs (_("\t\tAttributes    : "), stdout);
     276        print_flags (obj->ta_cols.ta_cols_val[i].tc_flags);
     277        fputs (_("\t\tAccess Rights : "), stdout);
     278        nis_print_rights (obj->ta_cols.ta_cols_val[i].tc_rights);
     279        fputc ('\n', stdout);
     280      }
     281  }
     282  libnsl_hidden_nolink_def (nis_print_table, GLIBC_2_1)
     283  
     284  void
     285  nis_print_link (const link_obj *obj)
     286  {
     287    fputs (_("Linked Object Type : "), stdout);
     288    nis_print_objtype (obj->li_rtype);
     289    printf (_("Linked to : %s\n"), obj->li_name);
     290    /* XXX Print the attributes here, if they exists */
     291  }
     292  libnsl_hidden_nolink_def (nis_print_link, GLIBC_2_1)
     293  
     294  void
     295  nis_print_entry (const entry_obj *obj)
     296  {
     297    unsigned int i;
     298  
     299    printf (_("\tEntry data of type %s\n"), obj->en_type);
     300    for (i = 0; i < obj->en_cols.en_cols_len; i++)
     301      {
     302        printf (_("\t[%u] - [%u bytes] "), i,
     303  	      obj->en_cols.en_cols_val[i].ec_value.ec_value_len);
     304        if ((obj->en_cols.en_cols_val[i].ec_flags & EN_CRYPT) == EN_CRYPT)
     305  	fputs (_("Encrypted data\n"), stdout);
     306        else if ((obj->en_cols.en_cols_val[i].ec_flags & EN_BINARY) == EN_BINARY)
     307  	fputs (_("Binary data\n"), stdout);
     308        else if (obj->en_cols.en_cols_val[i].ec_value.ec_value_len == 0)
     309  	fputs ("'(nil)'\n", stdout);
     310        else
     311  	printf ("'%.*s'\n",
     312  		(int)obj->en_cols.en_cols_val[i].ec_value.ec_value_len,
     313  		obj->en_cols.en_cols_val[i].ec_value.ec_value_val);
     314      }
     315  }
     316  libnsl_hidden_nolink_def (nis_print_entry, GLIBC_2_1)
     317  
     318  void
     319  nis_print_object (const nis_object * obj)
     320  {
     321    time_t buf;
     322  
     323    printf (_("Object Name   : %s\n"), obj->zo_name);
     324    printf (_("Directory     : %s\n"), obj->zo_domain);
     325    printf (_("Owner         : %s\n"), obj->zo_owner);
     326    printf (_("Group         : %s\n"), obj->zo_group);
     327    fputs (_("Access Rights : "), stdout);
     328    nis_print_rights (obj->zo_access);
     329    printf (_("\nTime to Live  : "));
     330    print_ttl (obj->zo_ttl);
     331    buf = obj->zo_oid.ctime;
     332    printf (_("Creation Time : %s"), ctime (&buf));
     333    buf = obj->zo_oid.mtime;
     334    printf (_("Mod. Time     : %s"), ctime (&buf));
     335    fputs (_("Object Type   : "), stdout);
     336    nis_print_objtype (obj->zo_data.zo_type);
     337    switch (obj->zo_data.zo_type)
     338      {
     339      case NIS_DIRECTORY_OBJ:
     340        nis_print_directory (&obj->zo_data.objdata_u.di_data);
     341        break;
     342      case NIS_GROUP_OBJ:
     343        nis_print_group (&obj->zo_data.objdata_u.gr_data);
     344        break;
     345      case NIS_TABLE_OBJ:
     346        nis_print_table (&obj->zo_data.objdata_u.ta_data);
     347        break;
     348      case NIS_ENTRY_OBJ:
     349        nis_print_entry (&obj->zo_data.objdata_u.en_data);
     350        break;
     351      case NIS_LINK_OBJ:
     352        nis_print_link (&obj->zo_data.objdata_u.li_data);
     353        break;
     354      case NIS_PRIVATE_OBJ:
     355        printf (_("    Data Length = %u\n"),
     356  	      obj->zo_data.objdata_u.po_data.po_data_len);
     357        break;
     358      default:
     359        break;
     360      }
     361  }
     362  libnsl_hidden_nolink_def (nis_print_object, GLIBC_2_1)
     363  
     364  void
     365  nis_print_result (const nis_result *res)
     366  {
     367    unsigned int i;
     368  
     369    printf (_("Status            : %s\n"), nis_sperrno (NIS_RES_STATUS (res)));
     370    printf (_("Number of objects : %u\n"), res->objects.objects_len);
     371  
     372    for (i = 0; i < res->objects.objects_len; i++)
     373      {
     374        printf (_("Object #%d:\n"), i);
     375        nis_print_object (&res->objects.objects_val[i]);
     376      }
     377  }
     378  libnsl_hidden_nolink_def (nis_print_result, GLIBC_2_1)