1 /* Copyright (c) 1997-2022 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 *
nis_nstype2str(const nstype type)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 void
print_ttl(const uint32_t ttl)56 print_ttl (const uint32_t ttl)
57 {
58   uint32_t time, s, m, h;
59 
60   time = ttl;
61 
62   h = time / (60 * 60);
63   time %= (60 * 60);
64   m = time / 60;
65   time %= 60;
66   s = time;
67   printf ("%u:%u:%u\n", h, m, s);
68 }
69 
70 static void
print_flags(const unsigned int flags)71 print_flags (const unsigned int flags)
72 {
73   fputs ("(", stdout);
74 
75   if (flags & TA_SEARCHABLE)
76     fputs ("SEARCHABLE, ", stdout);
77 
78   if (flags & TA_BINARY)
79     {
80       fputs ("BINARY DATA", stdout);
81       if (flags & TA_XDR)
82 	fputs (", XDR ENCODED", stdout);
83       if (flags & TA_ASN1)
84 	fputs (", ASN.1 ENCODED", stdout);
85       if (flags & TA_CRYPT)
86 	fputs (", ENCRYPTED", stdout);
87     }
88   else
89     {
90       fputs ("TEXTUAL DATA", stdout);
91       if (flags & TA_SEARCHABLE)
92 	{
93 	  if (flags & TA_CASE)
94 	    fputs (", CASE INSENSITIVE", stdout);
95 	  else
96 	    fputs (", CASE SENSITIVE", stdout);
97 	}
98     }
99 
100   fputs (")\n", stdout);
101 }
102 
103 static void
nis_print_objtype(enum zotypes type)104 nis_print_objtype (enum zotypes type)
105 {
106   switch (type)
107     {
108     case NIS_BOGUS_OBJ:
109       fputs (_("BOGUS OBJECT\n"), stdout);
110       break;
111     case NIS_NO_OBJ:
112       fputs (_("NO OBJECT\n"), stdout);
113       break;
114     case NIS_DIRECTORY_OBJ:
115       fputs (_("DIRECTORY\n"), stdout);
116       break;
117     case NIS_GROUP_OBJ:
118       fputs (_("GROUP\n"), stdout);
119       break;
120     case NIS_TABLE_OBJ:
121       fputs (_("TABLE\n"), stdout);
122       break;
123     case NIS_ENTRY_OBJ:
124       fputs (_("ENTRY\n"), stdout);
125       break;
126     case NIS_LINK_OBJ:
127       fputs (_("LINK\n"), stdout);
128       break;
129     case NIS_PRIVATE_OBJ:
130       fputs (_("PRIVATE\n"), stdout);
131       break;
132     default:
133       fputs (_("(Unknown object)\n"), stdout);
134       break;
135     }
136 }
137 
138 void
nis_print_rights(const unsigned int access)139 nis_print_rights (const unsigned int access)
140 {
141   char result[17];
142   unsigned int acc;
143   int i;
144 
145   acc = access;			/* Parameter is const ! */
146   result[i = 16] = '\0';
147   while (i > 0)
148     {
149       i -= 4;
150       result[i + 0] = (acc & NIS_READ_ACC) ? 'r' : '-';
151       result[i + 1] = (acc & NIS_MODIFY_ACC) ? 'm' : '-';
152       result[i + 2] = (acc & NIS_CREATE_ACC) ? 'c' : '-';
153       result[i + 3] = (acc & NIS_DESTROY_ACC) ? 'd' : '-';
154 
155       acc >>= 8;
156     }
157   fputs (result, stdout);
158 }
libnsl_hidden_nolink_def(nis_print_rights,GLIBC_2_1)159 libnsl_hidden_nolink_def (nis_print_rights, GLIBC_2_1)
160 
161 void
162 nis_print_directory (const directory_obj *dir)
163 {
164   nis_server *sptr;
165   unsigned int i;
166 
167   printf (_("Name : `%s'\n"), dir->do_name);
168   printf (_("Type : %s\n"), nis_nstype2str (dir->do_type));
169   sptr = dir->do_servers.do_servers_val;
170   for (i = 0; i < dir->do_servers.do_servers_len; i++)
171     {
172       if (i == 0)
173 	fputs (_("Master Server :\n"), stdout);
174       else
175 	fputs (_("Replicate :\n"), stdout);
176       printf (_("\tName       : %s\n"), sptr->name);
177       fputs (_("\tPublic Key : "), stdout);
178       switch (sptr->key_type)
179 	{
180 	case NIS_PK_NONE:
181 	  fputs (_("None.\n"), stdout);
182 	  break;
183 	case NIS_PK_DH:
184 	  printf (_("Diffie-Hellmann (%d bits)\n"),
185 		  (sptr->pkey.n_len - 1) * 4);
186 	  /* sptr->pkey.n_len counts the last 0, too */
187 	  break;
188 	case NIS_PK_RSA:
189 	  printf (_("RSA (%d bits)\n"), (sptr->pkey.n_len - 1) * 4);
190 	  break;
191 	case NIS_PK_KERB:
192 	  fputs (_("Kerberos.\n"), stdout);
193 	  break;
194 	default:
195 	  printf (_("Unknown (type = %d, bits = %d)\n"), sptr->key_type,
196 		  (sptr->pkey.n_len - 1) * 4);
197 	  break;
198 	}
199 
200       if (sptr->ep.ep_len != 0)
201 	{
202 	  unsigned int j;
203 
204 	  endpoint *ptr;
205 	  ptr = sptr->ep.ep_val;
206 	  printf (_("\tUniversal addresses (%u)\n"), sptr->ep.ep_len);
207 	  for (j = 0; j < sptr->ep.ep_len; j++)
208 	    {
209 	      printf ("\t[%d] - ", j + 1);
210 	      if (ptr->proto != NULL && ptr->proto[0] != '\0')
211 		printf ("%s, ", ptr->proto);
212 	      else
213 		printf ("-, ");
214 	      if (ptr->family != NULL && ptr->family[0] != '\0')
215 		printf ("%s, ", ptr->family);
216 	      else
217 		printf ("-, ");
218 	      if (ptr->uaddr != NULL && ptr->uaddr[0] != '\0')
219 		printf ("%s\n", ptr->uaddr);
220 	      else
221 		fputs ("-\n", stdout);
222 	      ptr++;
223 	    }
224 	}
225       sptr++;
226     }
227 
228   fputs (_("Time to live : "), stdout);
229   print_ttl (dir->do_ttl);
230   fputs (_("Default Access rights :\n"), stdout);
231   if (dir->do_armask.do_armask_len != 0)
232     {
233       oar_mask *ptr;
234 
235       ptr = dir->do_armask.do_armask_val;
236       for (i = 0; i < dir->do_armask.do_armask_len; i++)
237 	{
238 	  nis_print_rights (ptr->oa_rights);
239 	  printf (_("\tType         : %s\n"), nis_nstype2str (ptr->oa_otype));
240 	  fputs (_("\tAccess rights: "), stdout);
241 	  nis_print_rights (ptr->oa_rights);
242 	  fputs ("\n", stdout);
243 	  ptr++;
244 	}
245     }
246 }
libnsl_hidden_nolink_def(nis_print_directory,GLIBC_2_1)247 libnsl_hidden_nolink_def (nis_print_directory, GLIBC_2_1)
248 
249 void
250 nis_print_group (const group_obj *obj)
251 {
252   unsigned int i;
253 
254   fputs (_("Group Flags :"), stdout);
255   if (obj->gr_flags)
256     printf ("0x%08X", obj->gr_flags);
257   fputs (_("\nGroup Members :\n"), stdout);
258 
259   for (i = 0; i < obj->gr_members.gr_members_len; i++)
260     printf ("\t%s\n", obj->gr_members.gr_members_val[i]);
261 }
libnsl_hidden_nolink_def(nis_print_group,GLIBC_2_1)262 libnsl_hidden_nolink_def (nis_print_group, GLIBC_2_1)
263 
264 void
265 nis_print_table (const table_obj *obj)
266 {
267   unsigned int i;
268 
269   printf (_("Table Type          : %s\n"), obj->ta_type);
270   printf (_("Number of Columns   : %d\n"), obj->ta_maxcol);
271   printf (_("Character Separator : %c\n"), obj->ta_sep);
272   printf (_("Search Path         : %s\n"), obj->ta_path);
273   fputs (_("Columns             :\n"), stdout);
274   for (i = 0; i < obj->ta_cols.ta_cols_len; i++)
275     {
276       printf (_("\t[%d]\tName          : %s\n"), i,
277 	      obj->ta_cols.ta_cols_val[i].tc_name);
278       fputs (_("\t\tAttributes    : "), stdout);
279       print_flags (obj->ta_cols.ta_cols_val[i].tc_flags);
280       fputs (_("\t\tAccess Rights : "), stdout);
281       nis_print_rights (obj->ta_cols.ta_cols_val[i].tc_rights);
282       fputc ('\n', stdout);
283     }
284 }
libnsl_hidden_nolink_def(nis_print_table,GLIBC_2_1)285 libnsl_hidden_nolink_def (nis_print_table, GLIBC_2_1)
286 
287 void
288 nis_print_link (const link_obj *obj)
289 {
290   fputs (_("Linked Object Type : "), stdout);
291   nis_print_objtype (obj->li_rtype);
292   printf (_("Linked to : %s\n"), obj->li_name);
293   /* XXX Print the attributes here, if they exists */
294 }
libnsl_hidden_nolink_def(nis_print_link,GLIBC_2_1)295 libnsl_hidden_nolink_def (nis_print_link, GLIBC_2_1)
296 
297 void
298 nis_print_entry (const entry_obj *obj)
299 {
300   unsigned int i;
301 
302   printf (_("\tEntry data of type %s\n"), obj->en_type);
303   for (i = 0; i < obj->en_cols.en_cols_len; i++)
304     {
305       printf (_("\t[%u] - [%u bytes] "), i,
306 	      obj->en_cols.en_cols_val[i].ec_value.ec_value_len);
307       if ((obj->en_cols.en_cols_val[i].ec_flags & EN_CRYPT) == EN_CRYPT)
308 	fputs (_("Encrypted data\n"), stdout);
309       else if ((obj->en_cols.en_cols_val[i].ec_flags & EN_BINARY) == EN_BINARY)
310 	fputs (_("Binary data\n"), stdout);
311       else if (obj->en_cols.en_cols_val[i].ec_value.ec_value_len == 0)
312 	fputs ("'(nil)'\n", stdout);
313       else
314 	printf ("'%.*s'\n",
315 		(int)obj->en_cols.en_cols_val[i].ec_value.ec_value_len,
316 		obj->en_cols.en_cols_val[i].ec_value.ec_value_val);
317     }
318 }
libnsl_hidden_nolink_def(nis_print_entry,GLIBC_2_1)319 libnsl_hidden_nolink_def (nis_print_entry, GLIBC_2_1)
320 
321 void
322 nis_print_object (const nis_object * obj)
323 {
324   time_t buf;
325 
326   printf (_("Object Name   : %s\n"), obj->zo_name);
327   printf (_("Directory     : %s\n"), obj->zo_domain);
328   printf (_("Owner         : %s\n"), obj->zo_owner);
329   printf (_("Group         : %s\n"), obj->zo_group);
330   fputs (_("Access Rights : "), stdout);
331   nis_print_rights (obj->zo_access);
332   printf (_("\nTime to Live  : "));
333   print_ttl (obj->zo_ttl);
334   buf = obj->zo_oid.ctime;
335   printf (_("Creation Time : %s"), ctime (&buf));
336   buf = obj->zo_oid.mtime;
337   printf (_("Mod. Time     : %s"), ctime (&buf));
338   fputs (_("Object Type   : "), stdout);
339   nis_print_objtype (obj->zo_data.zo_type);
340   switch (obj->zo_data.zo_type)
341     {
342     case NIS_DIRECTORY_OBJ:
343       nis_print_directory (&obj->zo_data.objdata_u.di_data);
344       break;
345     case NIS_GROUP_OBJ:
346       nis_print_group (&obj->zo_data.objdata_u.gr_data);
347       break;
348     case NIS_TABLE_OBJ:
349       nis_print_table (&obj->zo_data.objdata_u.ta_data);
350       break;
351     case NIS_ENTRY_OBJ:
352       nis_print_entry (&obj->zo_data.objdata_u.en_data);
353       break;
354     case NIS_LINK_OBJ:
355       nis_print_link (&obj->zo_data.objdata_u.li_data);
356       break;
357     case NIS_PRIVATE_OBJ:
358       printf (_("    Data Length = %u\n"),
359 	      obj->zo_data.objdata_u.po_data.po_data_len);
360       break;
361     default:
362       break;
363     }
364 }
libnsl_hidden_nolink_def(nis_print_object,GLIBC_2_1)365 libnsl_hidden_nolink_def (nis_print_object, GLIBC_2_1)
366 
367 void
368 nis_print_result (const nis_result *res)
369 {
370   unsigned int i;
371 
372   printf (_("Status            : %s\n"), nis_sperrno (NIS_RES_STATUS (res)));
373   printf (_("Number of objects : %u\n"), res->objects.objects_len);
374 
375   for (i = 0; i < res->objects.objects_len; i++)
376     {
377       printf (_("Object #%d:\n"), i);
378       nis_print_object (&res->objects.objects_val[i]);
379     }
380 }
381 libnsl_hidden_nolink_def (nis_print_result, GLIBC_2_1)
382