~ubuntu-branches/ubuntu/oneiric/nis/oneiric-proposed

« back to all changes in this revision

Viewing changes to ypserv-2.17/ypserv/reg_slp.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2005-11-16 23:42:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051116234206-p00omaw5ji5q0qhr
Tags: 3.15-3ubuntu1
Resynchronise with Debian.  (me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2003, 2004 Thorsten Kukuk
2
 
   Author: Thorsten Kukuk <kukuk@suse.de>
3
 
 
4
 
   The YP Server is free software; you can redistribute it and/or
5
 
   modify it under the terms of the GNU General Public License
6
 
   version 2 as published by the Free Software Foundation.
7
 
 
8
 
   The YP Server is distributed in the hope that it will be useful,
9
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
   General Public License for more details.
12
 
 
13
 
   You should have received a copy of the GNU General Public
14
 
   License along with the YP Server; see the file COPYING. If
15
 
   not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16
 
   Cambridge, MA 02139, USA. */
17
 
 
18
 
#define _GNU_SOURCE
19
 
 
20
 
#if defined(HAVE_CONFIG_H)
21
 
#include "config.h"
22
 
#endif
23
 
 
24
 
#if USE_SLP
25
 
 
26
 
#include <netdb.h>
27
 
#include <ctype.h>
28
 
#include <stdio.h>
29
 
#include <stdlib.h>
30
 
#include <unistd.h>
31
 
#include <signal.h>
32
 
#include <dirent.h>
33
 
#include <sys/stat.h>
34
 
#include <sys/types.h>
35
 
#include <sys/socket.h>
36
 
#include <arpa/inet.h>
37
 
#include <arpa/nameser.h>
38
 
#include <slp.h>
39
 
 
40
 
#include "reg_slp.h"
41
 
#include "log_msg.h"
42
 
 
43
 
#include "ypserv_conf.h"
44
 
 
45
 
/*  This is the minimum we'll use, irrespective of config setting.
46
 
    definately don't set to less than about 30 seconds.  */
47
 
#define SLP_MIN_TIMEOUT 120
48
 
 
49
 
static void
50
 
ypservSLPRegReport (SLPHandle hslp UNUSED, SLPError errcode, void* cookie)
51
 
{
52
 
  /* return the error code in the cookie */
53
 
  *(SLPError*)cookie = errcode;
54
 
}
55
 
 
56
 
static void
57
 
do_refresh (int sig UNUSED)
58
 
{
59
 
  if (debug_flag)
60
 
    log_msg ("Service registration almost expired, refreshing it");
61
 
  register_slp ();
62
 
}
63
 
 
64
 
 
65
 
/* the URL we use to register.  */
66
 
static char *url = NULL;
67
 
 
68
 
static char hostname[1024];
69
 
#if USE_FQDN
70
 
static struct hostent *hp = NULL;
71
 
#endif
72
 
static char *hname;
73
 
 
74
 
static
75
 
char *create_domain_attr (void)
76
 
{
77
 
  DIR *dp;
78
 
  struct dirent *dep;
79
 
  char *str = NULL;
80
 
 
81
 
  dp = opendir (YPMAPDIR);
82
 
  if (dp == NULL)
83
 
    return NULL;
84
 
 
85
 
  while ((dep = readdir (dp)) != NULL)
86
 
    {
87
 
      struct stat st;
88
 
 
89
 
      /* ignore files starting with . */
90
 
      if (dep->d_name[0] == '.')
91
 
        continue;
92
 
 
93
 
      /* Ignore all files which are not a directory.  */
94
 
      if (stat (dep->d_name, &st) < 0)
95
 
        continue; /* Don't add something we cannot stat. */
96
 
 
97
 
      if (!S_ISDIR (st.st_mode))
98
 
        continue;
99
 
 
100
 
      /* We also don't wish to see ypbind data as domain name.  */
101
 
      if (strcmp (dep->d_name, "binding") == 0)
102
 
        continue;
103
 
 
104
 
      if (str == NULL)
105
 
        {
106
 
#if defined(HAVE_ASPRINTF)
107
 
          if (asprintf (&str, "(domain=%s", dep->d_name) < 0)
108
 
            {
109
 
              log_msg ("Out of memory");
110
 
              return NULL;
111
 
            }
112
 
#else
113
 
          str = malloc (9 + strlen (dep->d_name));
114
 
          if (str == NULL)
115
 
            {
116
 
              log_msg ("Out of memory");
117
 
              return NULL;
118
 
            }
119
 
          sprintf (str, "(domain=%s", dep->d_name);
120
 
#endif
121
 
        }
122
 
      else
123
 
        {
124
 
          char *cp;
125
 
 
126
 
#if defined(HAVE_ASPRINTF)
127
 
          if (asprintf (&cp, "%s,%s", str, dep->d_name) < 0)
128
 
            {
129
 
              log_msg ("Out of memory");
130
 
              return NULL;
131
 
            }
132
 
#else
133
 
          cp = malloc (strlen (str) + strlen (dep->d_name) + 2);
134
 
          if (cp == NULL)
135
 
            {
136
 
              log_msg ("Out of memory");
137
 
              return NULL;
138
 
            }
139
 
          sprintf (cp, "%s,%s", str, dep->d_name);
140
 
#endif
141
 
          free (str);
142
 
          str = cp;
143
 
        }
144
 
    }
145
 
  closedir (dp);
146
 
  if (str)
147
 
    {
148
 
      char *cp;
149
 
 
150
 
#if defined(HAVE_ASPRINTF)
151
 
      if (asprintf (&cp, "%s)", str) < 0)
152
 
        {
153
 
          log_msg ("Out of memory");
154
 
          return NULL;
155
 
        }
156
 
#else
157
 
      cp = malloc (strlen (str) + 2);
158
 
      if (cp == NULL)
159
 
        {
160
 
          log_msg ("Out of memory");
161
 
          return NULL;
162
 
        }
163
 
      sprintf (cp, "%s)", str);
164
 
#endif
165
 
      free (str);
166
 
      return cp;
167
 
    }
168
 
  return NULL;
169
 
}
170
 
 
171
 
int
172
 
register_slp ()
173
 
{
174
 
  SLPError err;
175
 
  SLPError callbackerr;
176
 
  SLPHandle hslp;
177
 
  int timeout;
178
 
  char *attr = NULL;
179
 
 
180
 
  if (url != NULL)
181
 
    {
182
 
      free (url);
183
 
      url = NULL;
184
 
    }
185
 
  else
186
 
    {
187
 
      gethostname (hostname, sizeof (hostname));
188
 
#if !USE_FQDN
189
 
      hname = hostname;
190
 
#else
191
 
      if (isdigit (hostname[0]))
192
 
        {
193
 
          char addr[INADDRSZ];
194
 
          if (inet_pton (AF_INET, hostname, &addr))
195
 
            hp = gethostbyaddr (addr, sizeof (addr), AF_INET);
196
 
        }
197
 
      else
198
 
        hp = gethostbyname (hostname);
199
 
      hname = hp->h_name;
200
 
#endif
201
 
    }
202
 
 
203
 
  if (slp_timeout == 0)
204
 
    timeout = SLP_LIFETIME_MAXIMUM; /* don't expire, ever */
205
 
  else if (SLP_MIN_TIMEOUT > slp_timeout)
206
 
    timeout = SLP_MIN_TIMEOUT; /* use a reasonable minimum */
207
 
  else if (SLP_LIFETIME_MAXIMUM <= slp_timeout)
208
 
    timeout = (SLP_LIFETIME_MAXIMUM - 1); /* as long as possible */
209
 
  else
210
 
    timeout = slp_timeout;
211
 
 
212
 
#if defined(HAVE_ASPRINTF)
213
 
  if (asprintf (&url, "service:ypserv://%s/", hname) < 0)
214
 
    {
215
 
      log_msg ("Out of memory");
216
 
      return -1;
217
 
    }
218
 
#else
219
 
  url = malloc(strlen(hname) + 19);
220
 
  if (!url)
221
 
    {
222
 
      log_msg ("Out of memory");
223
 
      return -1;
224
 
    }
225
 
  sprintf (url, "service:ypserv://%s/", hname) < 0;
226
 
#endif
227
 
 
228
 
  err = SLPOpen ("en", SLP_FALSE, &hslp);
229
 
  if(err != SLP_OK)
230
 
    {
231
 
      log_msg ("Error opening slp handle %i", err);
232
 
      return -1;
233
 
    }
234
 
 
235
 
  if (slp_flag == 2)
236
 
    attr = create_domain_attr ();
237
 
 
238
 
  if (attr == NULL) /* can also be NULL if create_domain_attr fails.  */
239
 
    attr = strdup ("");
240
 
 
241
 
  /* Register a service with SLP */
242
 
  err = SLPReg (hslp, url, timeout, 0,
243
 
                attr,
244
 
                SLP_TRUE,
245
 
                ypservSLPRegReport,
246
 
                &callbackerr);
247
 
 
248
 
  free (attr);
249
 
 
250
 
  /* err may contain an error code that occurred as the slp library    */
251
 
  /* _prepared_ to make the call.                                     */
252
 
  if ((err != SLP_OK) || (callbackerr != SLP_OK))
253
 
    {
254
 
      log_msg ("Error registering service with slp %i", err);
255
 
      return -1;
256
 
    }
257
 
 
258
 
  /* callbackerr may contain an error code (that was assigned through */
259
 
  /* the callback cookie) that occurred as slp packets were sent on    */
260
 
  /* the wire */
261
 
  if( callbackerr != SLP_OK)
262
 
    {
263
 
      log_msg ("Error registering service with slp %i",
264
 
               callbackerr);
265
 
      return callbackerr;
266
 
    }
267
 
 
268
 
  /* Now that we're done using slp, close the slp handle */
269
 
  SLPClose (hslp);
270
 
 
271
 
  /* Set up a timer to refresh the service records */
272
 
  if (timeout != SLP_LIFETIME_MAXIMUM)
273
 
    {
274
 
      if (signal (SIGALRM, do_refresh) == SIG_ERR)
275
 
        log_msg ("SLP: error establishing signal handler\n");
276
 
 
277
 
      alarm (timeout - 15);
278
 
    }
279
 
 
280
 
  return 0;
281
 
}
282
 
 
283
 
int
284
 
deregister_slp ()
285
 
{
286
 
  SLPError err;
287
 
  SLPError callbackerr;
288
 
  SLPHandle hslp;
289
 
 
290
 
  if (url == NULL)
291
 
    {
292
 
      log_msg ("URL not registerd!");
293
 
      return -1;
294
 
    }
295
 
 
296
 
  err = SLPOpen ("en", SLP_FALSE, &hslp);
297
 
  if(err != SLP_OK)
298
 
    {
299
 
      log_msg ("Error opening slp handle %i", err);
300
 
      return -1;
301
 
    }
302
 
 
303
 
  /* Disable possibel alarm call.  */
304
 
  alarm (0);
305
 
 
306
 
    /* DeRegister a service with SLP */
307
 
  err = SLPDereg (hslp, url, ypservSLPRegReport, &callbackerr);
308
 
 
309
 
  free (url);
310
 
  url = NULL;
311
 
 
312
 
  /* err may contain an error code that occurred as the slp library    */
313
 
  /* _prepared_ to make the call.                                     */
314
 
  if ((err != SLP_OK) || (callbackerr != SLP_OK))
315
 
    {
316
 
      log_msg ("Error registering service with slp %i", err);
317
 
      return -1;
318
 
    }
319
 
 
320
 
  /* callbackerr may contain an error code (that was assigned through */
321
 
  /* the callback cookie) that occurred as slp packets were sent on    */
322
 
  /* the wire */
323
 
  if( callbackerr != SLP_OK)
324
 
    {
325
 
      log_msg ("Error registering service with slp %i",
326
 
               callbackerr);
327
 
      return callbackerr;
328
 
    }
329
 
 
330
 
  /* Now that we're done using slp, close the slp handle */
331
 
  SLPClose (hslp);
332
 
 
333
 
  return 0;
334
 
}
335
 
 
336
 
#endif