File: hescompat.c

package info (click to toggle)
hesiod 3.0.2-20
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 716 kB
  • ctags: 111
  • sloc: ansic: 1,269; sh: 236; makefile: 142
file content (165 lines) | stat: -rw-r--r-- 3,669 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright 1996 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

/* This file is part of the Hesiod library.  It implements
 * backward-compatibility interfaces.
 */

static char rcsid[] = "$Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp $";

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "hesiod.h"

static int inited = 0;
static void *context;
static char *bindname, **list;
static struct passwd *pw;
static struct servent *serv;
static struct hesiod_postoffice *po;
static struct hes_postoffice compatpo;
static int errval = HES_ER_UNINIT;

static int init_context(void);
static void translate_errors(void);

int hes_init()
{
  init_context();
  return errval;
}

char *hes_to_bind(const char *name, const char *type)
{
  if (init_context() < 0)
    return NULL;
  if (bindname)
    free(bindname);
  bindname = hesiod_to_bind(context, name, type);
  if (!bindname)
    translate_errors();
  return bindname;
}

char **hes_resolve(const char *name, const char *type)
{
  if (init_context() < 0)
    return NULL;

  /* In the old Hesiod interface, the caller was responsible for freeing
   * the returned strings but not the vector of strings itself.
   */
  if (list)
    free(list);

  list = hesiod_resolve(context, name, type);
  if (!list)
    translate_errors();
  return list;
}

int hes_error(void)
{
  return errval;
}

struct passwd *hes_getpwnam(const char *name)
{
  if (init_context() < 0)
    return NULL;
  if (pw)
    hesiod_free_passwd(context, pw);
  pw = hesiod_getpwnam(context, name);
  if (!pw)
    translate_errors();
  return pw;
}

struct passwd *hes_getpwuid(uid_t uid)
{
  if (init_context() < 0)
    return NULL;
  if (pw)
    hesiod_free_passwd(context, pw);
  pw = hesiod_getpwuid(context, uid);
  if (!pw)
    translate_errors();
  return pw;
}

struct servent *hes_getservbyname(const char *name, const char *proto)
{
  if (init_context() < 0)
    return NULL;
  if (serv)
    hesiod_free_servent(context, serv);
  serv = hesiod_getservbyname(context, name, proto);
  if (!serv)
    translate_errors();
  return serv;
}

struct hes_postoffice *hes_getmailhost(const char *name)
{
  if (init_context() < 0)
    return NULL;
  if (po)
    hesiod_free_postoffice(context, po);
  po = hesiod_getmailhost(context, name);
  if (!po)
    {
      translate_errors();
      return NULL;
    }
  compatpo.po_type = po->hesiod_po_type;
  compatpo.po_host = po->hesiod_po_host;
  compatpo.po_name = po->hesiod_po_name;
  return &compatpo;
}

static int init_context()
{
  if (!inited)
    {
      inited = 1;
      if (hesiod_init(&context) < 0)
	{
	  errval = HES_ER_CONFIG;
	  return -1;
	}
      errval = HES_ER_OK;
    }
  return 0;
}

static void translate_errors()
{
  switch (errno)
    {
    case ENOENT:
      errval = HES_ER_NOTFOUND;
      break;
    case ECONNREFUSED:
    case EMSGSIZE:
      errval = HES_ER_NET;
      break;
    case ENOMEM:
    default:
      /* Not a good match, but the best we can do. */
      errval = HES_ER_CONFIG;
      break;
    }
}