]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_file.c
Add a few more alloca size checks
[thirdparty/glibc.git] / nis / nis_file.c
CommitLineData
eca086a6 1/* Copyright (c) 1997, 1998, 1999, 2004, 2005 Free Software Foundation, Inc.
e61abf83 2 This file is part of the GNU C Library.
32abdb71 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
e61abf83
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
e61abf83
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
e61abf83 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
e61abf83
UD
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <rpcsvc/nis.h>
91eee4dd 24#include "nis_xdr.h"
5ae9d168 25
eca086a6
UD
26typedef bool_t (*iofct_t) (XDR *, void *);
27typedef void (*freefct_t) (void *);
0ecb606c 28
eca086a6
UD
29
30static void *
31read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
32 size_t objsize)
e61abf83 33{
eca086a6 34 FILE *in = fopen (name, "rc");
e61abf83 35 if (in == NULL)
3e5f5557 36 return NULL;
e61abf83 37
eca086a6 38 void *obj = calloc (1, objsize);
3d8fa13a
UD
39
40 if (obj != NULL)
e852e889 41 {
7440c23e 42 XDR xdrs;
3d8fa13a 43 xdrstdio_create (&xdrs, in, XDR_DECODE);
eca086a6 44 bool_t status = readfct (&xdrs, obj);
3d8fa13a
UD
45 xdr_destroy (&xdrs);
46
47 if (!status)
48 {
eca086a6 49 freefct (obj);
3d8fa13a
UD
50 obj = NULL;
51 }
e852e889 52 }
3d8fa13a
UD
53
54 fclose (in);
55
56 return obj;
e61abf83
UD
57}
58
eca086a6
UD
59static bool_t
60write_nis_obj (const char *name, const void *obj, iofct_t writefct)
e61abf83 61{
eca086a6 62 FILE *out = fopen (name, "w");
e61abf83 63 if (out == NULL)
3e5f5557 64 return FALSE;
e61abf83 65
eca086a6 66 XDR xdrs;
e61abf83 67 xdrstdio_create (&xdrs, out, XDR_ENCODE);
eca086a6 68 bool_t status = writefct (&xdrs, (void *) obj);
a53bad16
UD
69 xdr_destroy (&xdrs);
70 fclose (out);
e61abf83 71
a53bad16 72 return status;
e61abf83
UD
73}
74
e61abf83 75
eca086a6 76static const char cold_start_file[] = "/var/nis/NIS_COLD_START";
e61abf83 77
eca086a6
UD
78directory_obj *
79readColdStartFile (void)
80{
81 return read_nis_obj (cold_start_file, (iofct_t) _xdr_directory_obj,
82 (freefct_t) nis_free_directory, sizeof (directory_obj));
83}
84libnsl_hidden_def (readColdStartFile)
32abdb71 85
eca086a6
UD
86bool_t
87writeColdStartFile (const directory_obj *obj)
88{
89 return write_nis_obj (cold_start_file, obj, (iofct_t) _xdr_directory_obj);
90}
e61abf83 91
eca086a6
UD
92nis_object *
93nis_read_obj (const char *name)
94{
95 return read_nis_obj (name, (iofct_t) _xdr_nis_object,
96 (freefct_t) nis_free_object, sizeof (nis_object));
e61abf83
UD
97}
98
99bool_t
100nis_write_obj (const char *name, const nis_object *obj)
101{
eca086a6 102 return write_nis_obj (name, obj, (iofct_t) _xdr_nis_object);
e61abf83 103}