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 <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <rpcsvc/nis.h>
22#include <shlib-compat.h>
23#include "nis_xdr.h"
24
25typedef bool_t (*iofct_t) (XDR *, void *);
26typedef void (*freefct_t) (void *);
27
28
29static void *
30read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
31 size_t objsize)
32{
33 FILE *in = fopen (filename: name, modes: "rce");
34 if (in == NULL)
35 return NULL;
36
37 void *obj = calloc (nmemb: 1, size: objsize);
38
39 if (obj != NULL)
40 {
41 XDR xdrs;
42 xdrstdio_create (xdrs: &xdrs, file: in, xop: XDR_DECODE);
43 bool_t status = readfct (&xdrs, obj);
44 xdr_destroy (&xdrs);
45
46 if (!status)
47 {
48 freefct (obj);
49 obj = NULL;
50 }
51 }
52
53 fclose (stream: in);
54
55 return obj;
56}
57
58static bool_t
59write_nis_obj (const char *name, const void *obj, iofct_t writefct)
60{
61 FILE *out = fopen (filename: name, modes: "wce");
62 if (out == NULL)
63 return FALSE;
64
65 XDR xdrs;
66 xdrstdio_create (xdrs: &xdrs, file: out, xop: XDR_ENCODE);
67 bool_t status = writefct (&xdrs, (void *) obj);
68 xdr_destroy (&xdrs);
69 fclose (stream: out);
70
71 return status;
72}
73
74
75static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
76
77directory_obj *
78readColdStartFile (void)
79{
80 return read_nis_obj (name: cold_start_file, readfct: (iofct_t) _xdr_directory_obj,
81 freefct: (freefct_t) nis_free_directory, objsize: sizeof (directory_obj));
82}
83libnsl_hidden_nolink_def (readColdStartFile, GLIBC_2_1)
84
85bool_t
86writeColdStartFile (const directory_obj *obj)
87{
88 return write_nis_obj (name: cold_start_file, obj, writefct: (iofct_t) _xdr_directory_obj);
89}
90libnsl_hidden_nolink_def (writeColdStartFile, GLIBC_2_1)
91
92nis_object *
93nis_read_obj (const char *name)
94{
95 return read_nis_obj (name, readfct: (iofct_t) _xdr_nis_object,
96 freefct: (freefct_t) nis_free_object, objsize: sizeof (nis_object));
97}
98libnsl_hidden_nolink_def (nis_read_obj, GLIBC_2_1)
99
100bool_t
101nis_write_obj (const char *name, const nis_object *obj)
102{
103 return write_nis_obj (name, obj, writefct: (iofct_t) _xdr_nis_object);
104}
105libnsl_hidden_nolink_def (nis_write_obj, GLIBC_2_1)
106

source code of glibc/nis/nis_file.c