]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - hesiod/hescompat.c
new upstream version
[1ts-debian.git] / hesiod / hescompat.c
1 /* Copyright 1996 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 /* This file is part of the Hesiod library.  It implements
17  * backward-compatibility interfaces.
18  */
19
20 static const char rcsid[] = "$Id: hescompat.c,v 1.3 1999/10/23 19:29:15 danw Exp $";
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include "hesiod.h"
26
27 static int inited = 0;
28 static void *context;
29 static char *bindname, **list;
30 static struct passwd *pw;
31 static struct servent *serv;
32 static struct hesiod_postoffice *po;
33 static struct hes_postoffice compatpo;
34 static int errval = HES_ER_UNINIT;
35
36 static int init_context(void);
37 static void translate_errors(void);
38
39 int hes_init(void)
40 {
41   init_context();
42   return errval;
43 }
44
45 char *hes_to_bind(const char *name, const char *type)
46 {
47   if (init_context() < 0)
48     return NULL;
49   if (bindname)
50     free(bindname);
51   bindname = hesiod_to_bind(context, name, type);
52   if (!bindname)
53     translate_errors();
54   return bindname;
55 }
56
57 char **hes_resolve(const char *name, const char *type)
58 {
59   if (init_context() < 0)
60     return NULL;
61
62   /* In the old Hesiod interface, the caller was responsible for freeing
63    * the returned strings but not the vector of strings itself.
64    */
65   if (list)
66     free(list);
67
68   list = hesiod_resolve(context, name, type);
69   if (!list)
70     translate_errors();
71   return list;
72 }
73
74 int hes_error(void)
75 {
76   return errval;
77 }
78
79 struct passwd *hes_getpwnam(const char *name)
80 {
81   if (init_context() < 0)
82     return NULL;
83   if (pw)
84     hesiod_free_passwd(context, pw);
85   pw = hesiod_getpwnam(context, name);
86   if (!pw)
87     translate_errors();
88   return pw;
89 }
90
91 struct passwd *hes_getpwuid(uid_t uid)
92 {
93   if (init_context() < 0)
94     return NULL;
95   if (pw)
96     hesiod_free_passwd(context, pw);
97   pw = hesiod_getpwuid(context, uid);
98   if (!pw)
99     translate_errors();
100   return pw;
101 }
102
103 struct servent *hes_getservbyname(const char *name, const char *proto)
104 {
105   if (init_context() < 0)
106     return NULL;
107   if (serv)
108     hesiod_free_servent(context, serv);
109   serv = hesiod_getservbyname(context, name, proto);
110   if (!serv)
111     translate_errors();
112   return serv;
113 }
114
115 struct hes_postoffice *hes_getmailhost(const char *name)
116 {
117   if (init_context() < 0)
118     return NULL;
119   if (po)
120     hesiod_free_postoffice(context, po);
121   po = hesiod_getmailhost(context, name);
122   if (!po)
123     {
124       translate_errors();
125       return NULL;
126     }
127   compatpo.po_type = po->hesiod_po_type;
128   compatpo.po_host = po->hesiod_po_host;
129   compatpo.po_name = po->hesiod_po_name;
130   return &compatpo;
131 }
132
133 static int init_context(void)
134 {
135   if (!inited)
136     {
137       inited = 1;
138       if (hesiod_init(&context) < 0)
139         {
140           errval = HES_ER_CONFIG;
141           return -1;
142         }
143       errval = HES_ER_OK;
144     }
145   return 0;
146 }
147
148 static void translate_errors(void)
149 {
150   switch (errno)
151     {
152     case ENOENT:
153       errval = HES_ER_NOTFOUND;
154       break;
155     case ECONNREFUSED:
156     case EMSGSIZE:
157       errval = HES_ER_NET;
158       break;
159     case ENOMEM:
160     default:
161       /* Not a good match, but the best we can do. */
162       errval = HES_ER_CONFIG;
163       break;
164     }
165 }