1
/* Copyright (c) 2003, 2004 Thorsten Kukuk
2
Author: Thorsten Kukuk <kukuk@suse.de>
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.
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.
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. */
20
#if defined(HAVE_CONFIG_H)
34
#include <sys/types.h>
35
#include <sys/socket.h>
36
#include <arpa/inet.h>
37
#include <arpa/nameser.h>
43
#include "ypserv_conf.h"
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
50
ypservSLPRegReport (SLPHandle hslp UNUSED, SLPError errcode, void* cookie)
52
/* return the error code in the cookie */
53
*(SLPError*)cookie = errcode;
57
do_refresh (int sig UNUSED)
60
log_msg ("Service registration almost expired, refreshing it");
65
/* the URL we use to register. */
66
static char *url = NULL;
68
static char hostname[1024];
70
static struct hostent *hp = NULL;
75
char *create_domain_attr (void)
81
dp = opendir (YPMAPDIR);
85
while ((dep = readdir (dp)) != NULL)
89
/* ignore files starting with . */
90
if (dep->d_name[0] == '.')
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. */
97
if (!S_ISDIR (st.st_mode))
100
/* We also don't wish to see ypbind data as domain name. */
101
if (strcmp (dep->d_name, "binding") == 0)
106
#if defined(HAVE_ASPRINTF)
107
if (asprintf (&str, "(domain=%s", dep->d_name) < 0)
109
log_msg ("Out of memory");
113
str = malloc (9 + strlen (dep->d_name));
116
log_msg ("Out of memory");
119
sprintf (str, "(domain=%s", dep->d_name);
126
#if defined(HAVE_ASPRINTF)
127
if (asprintf (&cp, "%s,%s", str, dep->d_name) < 0)
129
log_msg ("Out of memory");
133
cp = malloc (strlen (str) + strlen (dep->d_name) + 2);
136
log_msg ("Out of memory");
139
sprintf (cp, "%s,%s", str, dep->d_name);
150
#if defined(HAVE_ASPRINTF)
151
if (asprintf (&cp, "%s)", str) < 0)
153
log_msg ("Out of memory");
157
cp = malloc (strlen (str) + 2);
160
log_msg ("Out of memory");
163
sprintf (cp, "%s)", str);
175
SLPError callbackerr;
187
gethostname (hostname, sizeof (hostname));
191
if (isdigit (hostname[0]))
194
if (inet_pton (AF_INET, hostname, &addr))
195
hp = gethostbyaddr (addr, sizeof (addr), AF_INET);
198
hp = gethostbyname (hostname);
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 */
210
timeout = slp_timeout;
212
#if defined(HAVE_ASPRINTF)
213
if (asprintf (&url, "service:ypserv://%s/", hname) < 0)
215
log_msg ("Out of memory");
219
url = malloc(strlen(hname) + 19);
222
log_msg ("Out of memory");
225
sprintf (url, "service:ypserv://%s/", hname) < 0;
228
err = SLPOpen ("en", SLP_FALSE, &hslp);
231
log_msg ("Error opening slp handle %i", err);
236
attr = create_domain_attr ();
238
if (attr == NULL) /* can also be NULL if create_domain_attr fails. */
241
/* Register a service with SLP */
242
err = SLPReg (hslp, url, timeout, 0,
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))
254
log_msg ("Error registering service with slp %i", err);
258
/* callbackerr may contain an error code (that was assigned through */
259
/* the callback cookie) that occurred as slp packets were sent on */
261
if( callbackerr != SLP_OK)
263
log_msg ("Error registering service with slp %i",
268
/* Now that we're done using slp, close the slp handle */
271
/* Set up a timer to refresh the service records */
272
if (timeout != SLP_LIFETIME_MAXIMUM)
274
if (signal (SIGALRM, do_refresh) == SIG_ERR)
275
log_msg ("SLP: error establishing signal handler\n");
277
alarm (timeout - 15);
287
SLPError callbackerr;
292
log_msg ("URL not registerd!");
296
err = SLPOpen ("en", SLP_FALSE, &hslp);
299
log_msg ("Error opening slp handle %i", err);
303
/* Disable possibel alarm call. */
306
/* DeRegister a service with SLP */
307
err = SLPDereg (hslp, url, ypservSLPRegReport, &callbackerr);
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))
316
log_msg ("Error registering service with slp %i", err);
320
/* callbackerr may contain an error code (that was assigned through */
321
/* the callback cookie) that occurred as slp packets were sent on */
323
if( callbackerr != SLP_OK)
325
log_msg ("Error registering service with slp %i",
330
/* Now that we're done using slp, close the slp handle */