xref: /illumos-gate/usr/src/lib/libslp/clib/SLPReg.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This file contains all functions pertaining to registrations:
31*7c478bd9Sstevel@tonic-gate  *	SLPReg
32*7c478bd9Sstevel@tonic-gate  *	SLPDereg
33*7c478bd9Sstevel@tonic-gate  *	SLPDelAttrs
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * Each function talks only to the local slpd, and receives a SrvAck
36*7c478bd9Sstevel@tonic-gate  * reply.
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * These calls can operate in sync or async mode. Sync mode operates
39*7c478bd9Sstevel@tonic-gate  * as follows:
40*7c478bd9Sstevel@tonic-gate  *	format params into a char *msg
41*7c478bd9Sstevel@tonic-gate  *	send this msg to slpd
42*7c478bd9Sstevel@tonic-gate  *	invoke the SLPRegReport callback with the error code found in the
43*7c478bd9Sstevel@tonic-gate  *		reply from slpd
44*7c478bd9Sstevel@tonic-gate  *	return
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * Async mode operates as follows:
47*7c478bd9Sstevel@tonic-gate  *	format the params into a char *msg
48*7c478bd9Sstevel@tonic-gate  *	there is one thread per process which handles async regs
49*7c478bd9Sstevel@tonic-gate  *	make sure this thread is running
50*7c478bd9Sstevel@tonic-gate  *	the reg_thread monitors the global static reg_q for messages
51*7c478bd9Sstevel@tonic-gate  *	a queue message is represented as a struct reg_q_msg
52*7c478bd9Sstevel@tonic-gate  *	caller thread places the reg msg on the reg_q, and returns
53*7c478bd9Sstevel@tonic-gate  *	the reg_thread reads the message from the reg_q, and sends the
54*7c478bd9Sstevel@tonic-gate  *		msg to slpd
55*7c478bd9Sstevel@tonic-gate  *	the reg_thread then invokes the SLPRegReport callback with the error
56*7c478bd9Sstevel@tonic-gate  *		code found in the reply from slpd
57*7c478bd9Sstevel@tonic-gate  *	once started, the reg_thread manages registration refreshing.
58*7c478bd9Sstevel@tonic-gate  *		If there are no registrations to refresh, the thread exits.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #include <stdio.h>
62*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
63*7c478bd9Sstevel@tonic-gate #include <thread.h>
64*7c478bd9Sstevel@tonic-gate #include <synch.h>
65*7c478bd9Sstevel@tonic-gate #include <syslog.h>
66*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
68*7c478bd9Sstevel@tonic-gate #include <time.h>
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /* Indices into a reg_msg iovec for auth blocks */
71*7c478bd9Sstevel@tonic-gate #define	SLP_URL_AUTH	1
72*7c478bd9Sstevel@tonic-gate #define	SLP_ATTR_AUTH	3
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /* A registration / de-registration message */
75*7c478bd9Sstevel@tonic-gate struct reg_msg {
76*7c478bd9Sstevel@tonic-gate 	struct iovec *msgiov;	/* msg contents */
77*7c478bd9Sstevel@tonic-gate 	int msgiov_len;		/* number of iovec components in msgiov */
78*7c478bd9Sstevel@tonic-gate 	struct iovec urlbytes;
79*7c478bd9Sstevel@tonic-gate 	struct iovec attrbytes;
80*7c478bd9Sstevel@tonic-gate 	int urlauth;		/* index into authiov for URL auth blocks */
81*7c478bd9Sstevel@tonic-gate 	int attrauth;		/* index into authiov for attr auth blocks */
82*7c478bd9Sstevel@tonic-gate };
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * This is the message bundle passed to the reg thread via a queue.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate struct reg_q_msg {
88*7c478bd9Sstevel@tonic-gate 	struct reg_msg *msg;
89*7c478bd9Sstevel@tonic-gate 	slp_handle_impl_t *hp;
90*7c478bd9Sstevel@tonic-gate 	SLPRegReport *cb;
91*7c478bd9Sstevel@tonic-gate 	void *cookie;
92*7c478bd9Sstevel@tonic-gate };
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * These structures and vars are used for automatic re-registering.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate static struct rereg_entry {
98*7c478bd9Sstevel@tonic-gate 	char *url;
99*7c478bd9Sstevel@tonic-gate 	struct reg_msg *msg;
100*7c478bd9Sstevel@tonic-gate 	time_t wake_time;
101*7c478bd9Sstevel@tonic-gate 	unsigned short lifetime;
102*7c478bd9Sstevel@tonic-gate 	struct rereg_entry *next;
103*7c478bd9Sstevel@tonic-gate } *reregs;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static time_t next_wake_time;
106*7c478bd9Sstevel@tonic-gate static unsigned short granularity = 3600;
107*7c478bd9Sstevel@tonic-gate static mutex_t rereg_lock = DEFAULTMUTEX;	/* protects the rereg struct */
108*7c478bd9Sstevel@tonic-gate static mutex_t start_lock = DEFAULTMUTEX;	/* protects reg_thr creation */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static slp_queue_t *reg_q;	/* the global registration queue */
111*7c478bd9Sstevel@tonic-gate static int slp_reg_thr_running;	/* positive if reg_thread is running */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /* Private Utility Routines */
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate static SLPBoolean check_reregs();
116*7c478bd9Sstevel@tonic-gate static SLPError add_rereg(const char *, struct reg_msg *, unsigned short);
117*7c478bd9Sstevel@tonic-gate static unsigned short dereg_rereg(const char *);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate static SLPError enqueue_reg(slp_handle_impl_t *, struct reg_msg *,
120*7c478bd9Sstevel@tonic-gate 			    void *, SLPRegReport *);
121*7c478bd9Sstevel@tonic-gate static SLPError reg_impl(slp_handle_impl_t *, struct reg_msg *,
122*7c478bd9Sstevel@tonic-gate 				void *, SLPRegReport *);
123*7c478bd9Sstevel@tonic-gate static void reg_thread();
124*7c478bd9Sstevel@tonic-gate static SLPError start_reg_thr();
125*7c478bd9Sstevel@tonic-gate static SLPError reg_common(slp_handle_impl_t *, struct reg_msg *,
126*7c478bd9Sstevel@tonic-gate 				void *, SLPRegReport *);
127*7c478bd9Sstevel@tonic-gate static SLPError UnpackSrvAck(char *, SLPError *);
128*7c478bd9Sstevel@tonic-gate static SLPError packSrvReg(slp_handle_impl_t *, const char *,
129*7c478bd9Sstevel@tonic-gate 				unsigned short, const char *, const char *,
130*7c478bd9Sstevel@tonic-gate 				const char *, SLPBoolean, struct reg_msg **);
131*7c478bd9Sstevel@tonic-gate static SLPError packSrvDereg(slp_handle_impl_t *, const char *,
132*7c478bd9Sstevel@tonic-gate 				const char *, const char *, struct reg_msg **);
133*7c478bd9Sstevel@tonic-gate static SLPError find_SAscopes(char **scopes);
134*7c478bd9Sstevel@tonic-gate static void free_msgiov(struct iovec *, int);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate /* Public API SA functionality */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate SLPError SLPReg(SLPHandle   hSLP, const char  *pcSrvURL,
139*7c478bd9Sstevel@tonic-gate 		const unsigned short usLifetime,
140*7c478bd9Sstevel@tonic-gate 		const char  *pcSrvType,
141*7c478bd9Sstevel@tonic-gate 		const char  *pcAttrs, SLPBoolean  fresh,
142*7c478bd9Sstevel@tonic-gate 		SLPRegReport callback, void *pvUser) {
143*7c478bd9Sstevel@tonic-gate 	SLPError err;
144*7c478bd9Sstevel@tonic-gate 	char *pcScopeList;
145*7c478bd9Sstevel@tonic-gate 	struct reg_msg *msg;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (!hSLP || !pcSrvURL || !*pcSrvURL || !pcSrvType ||
148*7c478bd9Sstevel@tonic-gate 	    !pcAttrs || !callback) {
149*7c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if ((strlen(pcSrvURL) > SLP_MAX_STRINGLEN) ||
153*7c478bd9Sstevel@tonic-gate 	    (strlen(pcSrvType) > SLP_MAX_STRINGLEN) ||
154*7c478bd9Sstevel@tonic-gate 	    (strlen(pcAttrs) > SLP_MAX_STRINGLEN)) {
155*7c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if ((err = find_SAscopes(&pcScopeList)) != SLP_OK) {
159*7c478bd9Sstevel@tonic-gate 		return (err);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
163*7c478bd9Sstevel@tonic-gate 		return (err);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* format params into msg */
166*7c478bd9Sstevel@tonic-gate 	if ((err = packSrvReg(
167*7c478bd9Sstevel@tonic-gate 		hSLP, pcSrvURL, usLifetime, pcSrvType,
168*7c478bd9Sstevel@tonic-gate 		pcScopeList, pcAttrs, fresh, &msg)) != SLP_OK) {
169*7c478bd9Sstevel@tonic-gate 		free(pcScopeList);
170*7c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
171*7c478bd9Sstevel@tonic-gate 		return (err);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if ((err = reg_common(hSLP, msg, pvUser, callback)) == SLP_OK &&
175*7c478bd9Sstevel@tonic-gate 	    usLifetime == SLP_LIFETIME_MAXIMUM) {
176*7c478bd9Sstevel@tonic-gate 		struct reg_msg *rereg_msg;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 		/* create a rereg message, with no attrs */
179*7c478bd9Sstevel@tonic-gate 		err = packSrvReg(
180*7c478bd9Sstevel@tonic-gate 			hSLP, pcSrvURL, usLifetime,
181*7c478bd9Sstevel@tonic-gate 			pcSrvType, pcScopeList, "", SLP_TRUE, &rereg_msg);
182*7c478bd9Sstevel@tonic-gate 		if (err == SLP_OK) {
183*7c478bd9Sstevel@tonic-gate 			err = add_rereg(pcSrvURL, rereg_msg, usLifetime);
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	free(pcScopeList);
188*7c478bd9Sstevel@tonic-gate 	return (err);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate static SLPError packSrvReg(slp_handle_impl_t *hp, const char *url,
192*7c478bd9Sstevel@tonic-gate 				unsigned short lifetime, const char *type,
193*7c478bd9Sstevel@tonic-gate 				const char *scope, const char *attrs,
194*7c478bd9Sstevel@tonic-gate 				SLPBoolean fresh, struct reg_msg **msg) {
195*7c478bd9Sstevel@tonic-gate 	char *m = NULL;
196*7c478bd9Sstevel@tonic-gate 	SLPError err;
197*7c478bd9Sstevel@tonic-gate 	size_t msgLen, tmplen, len = 0;
198*7c478bd9Sstevel@tonic-gate 	time_t ts;
199*7c478bd9Sstevel@tonic-gate 	struct timeval tp[1];
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	/* calculate the timestamp */
202*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(tp, NULL);
203*7c478bd9Sstevel@tonic-gate 	ts = tp->tv_sec + lifetime;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/* create the reg_msg */
206*7c478bd9Sstevel@tonic-gate 	*msg = NULL;
207*7c478bd9Sstevel@tonic-gate 	if (!(*msg = calloc(1, sizeof (**msg)))) {
208*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
209*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	/* compute the total messge length */
213*7c478bd9Sstevel@tonic-gate 	msgLen =
214*7c478bd9Sstevel@tonic-gate 		slp_hdrlang_length(hp) +
215*7c478bd9Sstevel@tonic-gate 		/* URL entry */
216*7c478bd9Sstevel@tonic-gate 		5 + strlen(url) +
217*7c478bd9Sstevel@tonic-gate 		/* srv reg msg */
218*7c478bd9Sstevel@tonic-gate 		2 + strlen(type) +
219*7c478bd9Sstevel@tonic-gate 		2 + strlen(scope) +
220*7c478bd9Sstevel@tonic-gate 		2 + strlen(attrs);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	/*
223*7c478bd9Sstevel@tonic-gate 	 * Allocate memory for all the message except the auth blocks.
224*7c478bd9Sstevel@tonic-gate 	 * The iovec msgiov actually contains only pointers into this
225*7c478bd9Sstevel@tonic-gate 	 * memory.
226*7c478bd9Sstevel@tonic-gate 	 */
227*7c478bd9Sstevel@tonic-gate 	if (!(m = calloc(msgLen, 1))) {
228*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
229*7c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
230*7c478bd9Sstevel@tonic-gate 		goto error;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	/*
234*7c478bd9Sstevel@tonic-gate 	 * Create iovec for the msg. The iovec components are layed out thus:
235*7c478bd9Sstevel@tonic-gate 	 *   0: header + URL
236*7c478bd9Sstevel@tonic-gate 	 *   1: URL auth block count, URL auth block
237*7c478bd9Sstevel@tonic-gate 	 *   2: attrs
238*7c478bd9Sstevel@tonic-gate 	 *   3: attrs auth block count, attr auth block
239*7c478bd9Sstevel@tonic-gate 	 */
240*7c478bd9Sstevel@tonic-gate 	if (!((*msg)->msgiov = calloc(4, sizeof (*((*msg)->msgiov))))) {
241*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
242*7c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
243*7c478bd9Sstevel@tonic-gate 		goto error;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov_len = 4;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_header(hp->locale, m, msgLen, SRVREG, 0, &len))
248*7c478bd9Sstevel@tonic-gate 	    != SLP_OK)
249*7c478bd9Sstevel@tonic-gate 		goto error;
250*7c478bd9Sstevel@tonic-gate 	/* set fresh flag */
251*7c478bd9Sstevel@tonic-gate 	if (fresh)
252*7c478bd9Sstevel@tonic-gate 		slp_set_fresh(m);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	/* URL entry */
255*7c478bd9Sstevel@tonic-gate 	len++;	/* skip reserved byte in URL entry */
256*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_sht(m, msgLen, lifetime, &len)) != SLP_OK)
257*7c478bd9Sstevel@tonic-gate 		goto error;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/* save pointer to URL for signing */
260*7c478bd9Sstevel@tonic-gate 	tmplen = len;
261*7c478bd9Sstevel@tonic-gate 	(*msg)->urlbytes.iov_base = m + len;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, url, &len)) != SLP_OK)
264*7c478bd9Sstevel@tonic-gate 		goto error;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	(*msg)->urlbytes.iov_len = len - tmplen;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[0].iov_base = m;
269*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[0].iov_len = len;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	/* add auth blocks for URL */
272*7c478bd9Sstevel@tonic-gate 	err = slp_sign(&((*msg)->urlbytes), 1, ts,
273*7c478bd9Sstevel@tonic-gate 			(*msg)->msgiov, SLP_URL_AUTH);
274*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
275*7c478bd9Sstevel@tonic-gate 		goto error;
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[2].iov_base = m + len;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/* type, scopes, and attrs */
281*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, type, &len)) != SLP_OK)
282*7c478bd9Sstevel@tonic-gate 		goto error;
283*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, scope, &len)) != SLP_OK)
284*7c478bd9Sstevel@tonic-gate 		goto error;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	/* save pointer to attr for signing */
287*7c478bd9Sstevel@tonic-gate 	tmplen = len;
288*7c478bd9Sstevel@tonic-gate 	(*msg)->attrbytes.iov_base = m + len;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, attrs, &len)) != SLP_OK)
291*7c478bd9Sstevel@tonic-gate 		goto error;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	(*msg)->attrbytes.iov_len = len - tmplen;
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	/* length of 2nd portion is len - length of 1st portion */
296*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[2].iov_len = len - (*msg)->msgiov[0].iov_len;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/* add auth blocks for attrs */
299*7c478bd9Sstevel@tonic-gate 	err = slp_sign(&((*msg)->attrbytes), 1, ts,
300*7c478bd9Sstevel@tonic-gate 			(*msg)->msgiov, SLP_ATTR_AUTH);
301*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
302*7c478bd9Sstevel@tonic-gate 		goto error;
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	/* adjust msgLen with authblocks, and set header length */
306*7c478bd9Sstevel@tonic-gate 	msgLen += (*msg)->msgiov[SLP_URL_AUTH].iov_len;
307*7c478bd9Sstevel@tonic-gate 	msgLen += (*msg)->msgiov[SLP_ATTR_AUTH].iov_len;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	/* make sure msgLen is valid */
310*7c478bd9Sstevel@tonic-gate 	if (msgLen > SLP_MAX_MSGLEN) {
311*7c478bd9Sstevel@tonic-gate 		err = SLP_PARAMETER_BAD;
312*7c478bd9Sstevel@tonic-gate 		goto error;
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 	slp_set_length(m, msgLen);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
317*7c478bd9Sstevel@tonic-gate error:
318*7c478bd9Sstevel@tonic-gate 	if (m) free(m);
319*7c478bd9Sstevel@tonic-gate 	if (*msg) {
320*7c478bd9Sstevel@tonic-gate 		if ((*msg)->msgiov) free_msgiov((*msg)->msgiov, 4);
321*7c478bd9Sstevel@tonic-gate 		free(*msg);
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 	*msg = NULL;
324*7c478bd9Sstevel@tonic-gate 	return (err);
325*7c478bd9Sstevel@tonic-gate }
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate SLPError SLPDereg(SLPHandle hSLP, const char *pURL,
328*7c478bd9Sstevel@tonic-gate 			SLPRegReport callback, void *pvUser) {
329*7c478bd9Sstevel@tonic-gate 	char *pcScopeList;
330*7c478bd9Sstevel@tonic-gate 	struct reg_msg *msg;
331*7c478bd9Sstevel@tonic-gate 	SLPError err;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	if (!hSLP || !pURL || !*pURL || !callback) {
334*7c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	if (strlen(pURL) > SLP_MAX_STRINGLEN) {
338*7c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
339*7c478bd9Sstevel@tonic-gate 	}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if ((err = find_SAscopes(&pcScopeList))
342*7c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
343*7c478bd9Sstevel@tonic-gate 		return (err);
344*7c478bd9Sstevel@tonic-gate 	}
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
347*7c478bd9Sstevel@tonic-gate 		return (err);
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	/* format params into msg */
350*7c478bd9Sstevel@tonic-gate 	if ((err = packSrvDereg(hSLP, pURL, pcScopeList, NULL, &msg))
351*7c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
352*7c478bd9Sstevel@tonic-gate 		free(pcScopeList);
353*7c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
354*7c478bd9Sstevel@tonic-gate 		return (err);
355*7c478bd9Sstevel@tonic-gate 	}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	if ((err = reg_common(hSLP, msg, pvUser, callback)) == SLP_OK) {
358*7c478bd9Sstevel@tonic-gate 		(void) dereg_rereg(pURL);
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	free(pcScopeList);
362*7c478bd9Sstevel@tonic-gate 	return (err);
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate SLPError SLPDelAttrs(SLPHandle hSLP, const char *pURL,
366*7c478bd9Sstevel@tonic-gate 			const char *pcAttrs,
367*7c478bd9Sstevel@tonic-gate 			SLPRegReport callback, void *pvUser) {
368*7c478bd9Sstevel@tonic-gate 	SLPError err;
369*7c478bd9Sstevel@tonic-gate 	char *pcScopeList;
370*7c478bd9Sstevel@tonic-gate 	struct reg_msg *msg;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	if (!hSLP || !pURL || !*pURL || !pcAttrs || !callback) {
373*7c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
374*7c478bd9Sstevel@tonic-gate 	}
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate 	if ((strlen(pURL) > SLP_MAX_STRINGLEN) ||
377*7c478bd9Sstevel@tonic-gate 	    (strlen(pcAttrs) > SLP_MAX_STRINGLEN)) {
378*7c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if ((err = find_SAscopes(&pcScopeList))
382*7c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
383*7c478bd9Sstevel@tonic-gate 		return (err);
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
387*7c478bd9Sstevel@tonic-gate 		return (err);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	/* format params into msg */
390*7c478bd9Sstevel@tonic-gate 	if ((err = packSrvDereg(hSLP, pURL, pcScopeList, pcAttrs, &msg))
391*7c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
392*7c478bd9Sstevel@tonic-gate 		free(pcScopeList);
393*7c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
394*7c478bd9Sstevel@tonic-gate 		return (err);
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	free(pcScopeList);
398*7c478bd9Sstevel@tonic-gate 	return (reg_common(hSLP, msg, pvUser, callback));
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate static SLPError packSrvDereg(slp_handle_impl_t *hp, const char *url,
402*7c478bd9Sstevel@tonic-gate 				const char *scopes, const char *attrs,
403*7c478bd9Sstevel@tonic-gate 				struct reg_msg  **msg) {
404*7c478bd9Sstevel@tonic-gate 	char *m = NULL;
405*7c478bd9Sstevel@tonic-gate 	SLPError err;
406*7c478bd9Sstevel@tonic-gate 	size_t msgLen, tmplen, len = 0;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	/* create the reg_msg */
409*7c478bd9Sstevel@tonic-gate 	*msg = NULL;
410*7c478bd9Sstevel@tonic-gate 	if (!(*msg = calloc(1, sizeof (**msg)))) {
411*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
412*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/* compute the total message length */
416*7c478bd9Sstevel@tonic-gate 	attrs = (attrs ? attrs : "");
417*7c478bd9Sstevel@tonic-gate 	msgLen =
418*7c478bd9Sstevel@tonic-gate 		slp_hdrlang_length(hp) +
419*7c478bd9Sstevel@tonic-gate 		2 + strlen(scopes) +
420*7c478bd9Sstevel@tonic-gate 		/* URL entry */
421*7c478bd9Sstevel@tonic-gate 		5 + strlen(url) +
422*7c478bd9Sstevel@tonic-gate 		2 + strlen(attrs);
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	if (!(m = calloc(msgLen, 1))) {
425*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvDereg", "out of memory");
426*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	/*
430*7c478bd9Sstevel@tonic-gate 	 * Create iovec for the msg. The iovec components are layed out thus:
431*7c478bd9Sstevel@tonic-gate 	 *   0: header + URL
432*7c478bd9Sstevel@tonic-gate 	 *   1: URL auth block count, URL auth block
433*7c478bd9Sstevel@tonic-gate 	 *   2: attrs
434*7c478bd9Sstevel@tonic-gate 	 */
435*7c478bd9Sstevel@tonic-gate 	if (!((*msg)->msgiov = calloc(3, sizeof (*((*msg)->msgiov))))) {
436*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "packSrvDereg", "out of memory");
437*7c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
438*7c478bd9Sstevel@tonic-gate 		goto error;
439*7c478bd9Sstevel@tonic-gate 	}
440*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov_len = 3;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_header(
443*7c478bd9Sstevel@tonic-gate 		hp->locale, m, msgLen, SRVDEREG, 0, &len)) != SLP_OK)
444*7c478bd9Sstevel@tonic-gate 		goto error;
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	/* scopes */
447*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, scopes, &len)) != SLP_OK)
448*7c478bd9Sstevel@tonic-gate 		goto error;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	/* URL Entry */
451*7c478bd9Sstevel@tonic-gate 	len++;	/* skip reserved byte in URL entry */
452*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_sht(m, msgLen, 0, &len)) != SLP_OK)
453*7c478bd9Sstevel@tonic-gate 		goto error;
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 	/* save pointer to URL for signing */
456*7c478bd9Sstevel@tonic-gate 	tmplen = len;
457*7c478bd9Sstevel@tonic-gate 	(*msg)->urlbytes.iov_base = m + len;
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, url, &len)) != SLP_OK)
460*7c478bd9Sstevel@tonic-gate 		goto error;
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	(*msg)->urlbytes.iov_len = len - tmplen;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[0].iov_base = m;
465*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[0].iov_len = len;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	/* add auth blocks for URL */
468*7c478bd9Sstevel@tonic-gate 	err = slp_sign(&((*msg)->urlbytes), 1, 0,
469*7c478bd9Sstevel@tonic-gate 			(*msg)->msgiov, SLP_URL_AUTH);
470*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
471*7c478bd9Sstevel@tonic-gate 		goto error;
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[2].iov_base = m + len;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	/* tag list */
477*7c478bd9Sstevel@tonic-gate 	if ((err = slp_add_string(m, msgLen, attrs, &len)) != SLP_OK)
478*7c478bd9Sstevel@tonic-gate 		goto error;
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	/* length of 2nd portion is len - length of 1st portion */
481*7c478bd9Sstevel@tonic-gate 	(*msg)->msgiov[2].iov_len = len - (*msg)->msgiov[0].iov_len;
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	/* adjust msgLen with authblocks, and set header length */
484*7c478bd9Sstevel@tonic-gate 	msgLen += (*msg)->msgiov[SLP_URL_AUTH].iov_len;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	/* make sure msgLen is valid */
487*7c478bd9Sstevel@tonic-gate 	if (msgLen > SLP_MAX_MSGLEN) {
488*7c478bd9Sstevel@tonic-gate 		err = SLP_PARAMETER_BAD;
489*7c478bd9Sstevel@tonic-gate 		goto error;
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 	slp_set_length(m, msgLen);
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
494*7c478bd9Sstevel@tonic-gate error:
495*7c478bd9Sstevel@tonic-gate 	if (m) free(m);
496*7c478bd9Sstevel@tonic-gate 	if (*msg) {
497*7c478bd9Sstevel@tonic-gate 		if ((*msg)->msgiov) free_msgiov((*msg)->msgiov, 3);
498*7c478bd9Sstevel@tonic-gate 		free(*msg);
499*7c478bd9Sstevel@tonic-gate 	}
500*7c478bd9Sstevel@tonic-gate 	*msg = NULL;
501*7c478bd9Sstevel@tonic-gate 	return (err);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate /*
505*7c478bd9Sstevel@tonic-gate  * Passes the packed message to the routines which talk to slpd.
506*7c478bd9Sstevel@tonic-gate  */
507*7c478bd9Sstevel@tonic-gate static SLPError reg_common(slp_handle_impl_t *hp, struct reg_msg *msg,
508*7c478bd9Sstevel@tonic-gate 				void *cookie, SLPRegReport callback) {
509*7c478bd9Sstevel@tonic-gate 	SLPError err;
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 	if (!slp_reg_thr_running)
512*7c478bd9Sstevel@tonic-gate 		if ((err = start_reg_thr()) != SLP_OK)
513*7c478bd9Sstevel@tonic-gate 			goto reg_done;
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	if (hp->async)
516*7c478bd9Sstevel@tonic-gate 		err = enqueue_reg(hp, msg, cookie, callback);
517*7c478bd9Sstevel@tonic-gate 	else
518*7c478bd9Sstevel@tonic-gate 		err = reg_impl(hp, msg, cookie, callback);
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate reg_done:
521*7c478bd9Sstevel@tonic-gate 	/* If an error occurred, end_call() will not have happened */
522*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
523*7c478bd9Sstevel@tonic-gate 		slp_end_call(hp);
524*7c478bd9Sstevel@tonic-gate 	return (err);
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate /*
528*7c478bd9Sstevel@tonic-gate  * Put a reg message on the queue. Assumes reg_thread is running.
529*7c478bd9Sstevel@tonic-gate  */
530*7c478bd9Sstevel@tonic-gate static SLPError enqueue_reg(slp_handle_impl_t *hp, struct reg_msg *msg,
531*7c478bd9Sstevel@tonic-gate 			    void *cookie, SLPRegReport cb) {
532*7c478bd9Sstevel@tonic-gate 	struct reg_q_msg *rmsg;
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	if (!(rmsg = malloc(sizeof (*rmsg)))) {
535*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "enqueue_reg", "out of memory");
536*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
537*7c478bd9Sstevel@tonic-gate 	}
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	rmsg->msg = msg;
540*7c478bd9Sstevel@tonic-gate 	rmsg->hp = hp;
541*7c478bd9Sstevel@tonic-gate 	rmsg->cb = cb;
542*7c478bd9Sstevel@tonic-gate 	rmsg->cookie = cookie;
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	return (slp_enqueue(reg_q, rmsg));
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate /*
548*7c478bd9Sstevel@tonic-gate  * Create a new reg_q and start the reg thread.
549*7c478bd9Sstevel@tonic-gate  */
550*7c478bd9Sstevel@tonic-gate static SLPError start_reg_thr() {
551*7c478bd9Sstevel@tonic-gate 	SLPError err = SLP_OK;
552*7c478bd9Sstevel@tonic-gate 	int terr;
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&start_lock);
555*7c478bd9Sstevel@tonic-gate 	/* make sure someone else hasn't already intialized the thread */
556*7c478bd9Sstevel@tonic-gate 	if (slp_reg_thr_running) {
557*7c478bd9Sstevel@tonic-gate 		goto start_done;
558*7c478bd9Sstevel@tonic-gate 	}
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	/* create the reg queue */
561*7c478bd9Sstevel@tonic-gate 	reg_q = slp_new_queue(&err);
562*7c478bd9Sstevel@tonic-gate 	if (err != SLP_OK) {
563*7c478bd9Sstevel@tonic-gate 		goto start_done;
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 	/* start the reg thread */
567*7c478bd9Sstevel@tonic-gate 	if ((terr = thr_create(
568*7c478bd9Sstevel@tonic-gate 		0, NULL, (void *(*)(void *)) reg_thread,
569*7c478bd9Sstevel@tonic-gate 		NULL, 0, NULL)) != 0) {
570*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "start_reg_thr",
571*7c478bd9Sstevel@tonic-gate 			"could not start thread: %s",
572*7c478bd9Sstevel@tonic-gate 			strerror(terr));
573*7c478bd9Sstevel@tonic-gate 		slp_destroy_queue(reg_q);
574*7c478bd9Sstevel@tonic-gate 		err = SLP_INTERNAL_SYSTEM_ERROR;
575*7c478bd9Sstevel@tonic-gate 		goto start_done;
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 	slp_reg_thr_running = 1;
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate start_done:
580*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&start_lock);
581*7c478bd9Sstevel@tonic-gate 	return (err);
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate /*
585*7c478bd9Sstevel@tonic-gate  * This is what the permanent reg thread runs; it just sits in a loop
586*7c478bd9Sstevel@tonic-gate  * monitoring the reg_q for new reg messages.
587*7c478bd9Sstevel@tonic-gate  *
588*7c478bd9Sstevel@tonic-gate  * To conserve resources,
589*7c478bd9Sstevel@tonic-gate  * if there are no more registrations to refresh, it will exit.
590*7c478bd9Sstevel@tonic-gate  */
591*7c478bd9Sstevel@tonic-gate static void reg_thread() {
592*7c478bd9Sstevel@tonic-gate 	timestruc_t timeout;
593*7c478bd9Sstevel@tonic-gate 	timeout.tv_nsec = 0;
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 	for (;;) {
596*7c478bd9Sstevel@tonic-gate 		SLPBoolean etimed;
597*7c478bd9Sstevel@tonic-gate 		struct reg_q_msg *rmsg;
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 		/* get the next message from the queue */
600*7c478bd9Sstevel@tonic-gate 		timeout.tv_sec =
601*7c478bd9Sstevel@tonic-gate 			next_wake_time ? next_wake_time : time(NULL) + 5;
602*7c478bd9Sstevel@tonic-gate 		rmsg = slp_dequeue_timed(reg_q, &timeout, &etimed);
603*7c478bd9Sstevel@tonic-gate 		if (!rmsg && etimed == SLP_TRUE) {
604*7c478bd9Sstevel@tonic-gate 			/* timed out */
605*7c478bd9Sstevel@tonic-gate 			if (!check_reregs()) {
606*7c478bd9Sstevel@tonic-gate 				/* no more reregs; shut down this thread */
607*7c478bd9Sstevel@tonic-gate 				(void) mutex_lock(&start_lock);
608*7c478bd9Sstevel@tonic-gate 				slp_destroy_queue(reg_q);
609*7c478bd9Sstevel@tonic-gate 				slp_reg_thr_running = 0;
610*7c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&start_lock);
611*7c478bd9Sstevel@tonic-gate 				thr_exit(NULL);
612*7c478bd9Sstevel@tonic-gate 			}
613*7c478bd9Sstevel@tonic-gate 			continue;
614*7c478bd9Sstevel@tonic-gate 		}
615*7c478bd9Sstevel@tonic-gate 		if (!rmsg)
616*7c478bd9Sstevel@tonic-gate 			continue;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 		/* got a new message */
619*7c478bd9Sstevel@tonic-gate 		(void) reg_impl(rmsg->hp, rmsg->msg, rmsg->cookie, rmsg->cb);
620*7c478bd9Sstevel@tonic-gate 		free(rmsg);
621*7c478bd9Sstevel@tonic-gate 		(void) check_reregs();
622*7c478bd9Sstevel@tonic-gate 	}
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate /*
626*7c478bd9Sstevel@tonic-gate  * Unpacks a SrvAck.
627*7c478bd9Sstevel@tonic-gate  * 'reply' should point to the beginning of the header.
628*7c478bd9Sstevel@tonic-gate  */
629*7c478bd9Sstevel@tonic-gate static SLPError UnpackSrvAck(char *reply, SLPError *ans) {
630*7c478bd9Sstevel@tonic-gate 	SLPError err;
631*7c478bd9Sstevel@tonic-gate 	unsigned short langlen, call_err;
632*7c478bd9Sstevel@tonic-gate 	char *p = reply + SLP_HDRLEN;
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 	langlen = slp_get_langlen(reply);
635*7c478bd9Sstevel@tonic-gate 	p += langlen;
636*7c478bd9Sstevel@tonic-gate 	if ((err = slp_get_sht(p, 0, NULL, &call_err)) != SLP_OK)
637*7c478bd9Sstevel@tonic-gate 		return (err);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	*ans = slp_map_err(call_err);
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
642*7c478bd9Sstevel@tonic-gate }
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate /*
645*7c478bd9Sstevel@tonic-gate  * The dispatcher for SA messages. Sends a message to slpd, unpacks and
646*7c478bd9Sstevel@tonic-gate  * dispatches the reply to the user callback.
647*7c478bd9Sstevel@tonic-gate  */
648*7c478bd9Sstevel@tonic-gate static SLPError reg_impl(slp_handle_impl_t *hp, struct reg_msg *msg,
649*7c478bd9Sstevel@tonic-gate 				void *cookie, SLPRegReport cb) {
650*7c478bd9Sstevel@tonic-gate 	char *reply = NULL;
651*7c478bd9Sstevel@tonic-gate 	SLPError err, call_err;
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate 	if (hp->cancel)
654*7c478bd9Sstevel@tonic-gate 		goto transaction_complete;
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate 	if ((err = slp_send2slpd_iov(msg->msgiov, msg->msgiov_len, &reply))
657*7c478bd9Sstevel@tonic-gate 	    != SLP_OK)
658*7c478bd9Sstevel@tonic-gate 		goto transaction_complete;
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	/* through with msg, so free it now */
661*7c478bd9Sstevel@tonic-gate 	free_msgiov(msg->msgiov, msg->msgiov_len);
662*7c478bd9Sstevel@tonic-gate 	free(msg);
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	if ((err = UnpackSrvAck(reply, &call_err)) != SLP_OK)
665*7c478bd9Sstevel@tonic-gate 		goto transaction_complete;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	/* the reg thread doubles as the consumer thread for SA calls */
668*7c478bd9Sstevel@tonic-gate 	hp->consumer_tid = thr_self();
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	cb(hp, call_err, cookie);
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate transaction_complete:
673*7c478bd9Sstevel@tonic-gate 	if (reply) {
674*7c478bd9Sstevel@tonic-gate 		free(reply);
675*7c478bd9Sstevel@tonic-gate 	}
676*7c478bd9Sstevel@tonic-gate 	slp_end_call(hp);
677*7c478bd9Sstevel@tonic-gate 	return (err);
678*7c478bd9Sstevel@tonic-gate }
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate /*
681*7c478bd9Sstevel@tonic-gate  * Re-registration routines
682*7c478bd9Sstevel@tonic-gate  */
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate /*
685*7c478bd9Sstevel@tonic-gate  * Adds the registration contained in 'msg' to the refresh registration
686*7c478bd9Sstevel@tonic-gate  * list managed by reg_thread.
687*7c478bd9Sstevel@tonic-gate  * Only registrations which are meant to be permanent are refreshed,
688*7c478bd9Sstevel@tonic-gate  * so we only allow reg's with lifetime == SLP_LIFETIME_PERMANENT into
689*7c478bd9Sstevel@tonic-gate  * the rereg table.
690*7c478bd9Sstevel@tonic-gate  */
691*7c478bd9Sstevel@tonic-gate static SLPError add_rereg(const char *url, struct reg_msg *msg,
692*7c478bd9Sstevel@tonic-gate 				unsigned short lifetime) {
693*7c478bd9Sstevel@tonic-gate 	struct rereg_entry *reg;
694*7c478bd9Sstevel@tonic-gate 	SLPError err = SLP_OK;
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 	if (lifetime != SLP_LIFETIME_MAXIMUM) {
697*7c478bd9Sstevel@tonic-gate 		return (SLP_OK);
698*7c478bd9Sstevel@tonic-gate 	}
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rereg_lock);
701*7c478bd9Sstevel@tonic-gate 	/* alloc a new rereg entry */
702*7c478bd9Sstevel@tonic-gate 	if (!(reg = malloc(sizeof (*reg)))) {
703*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "add_rereg", "out of memory");
704*7c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
705*7c478bd9Sstevel@tonic-gate 		goto done;
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	if (!(reg->url = strdup(url))) {
709*7c478bd9Sstevel@tonic-gate 		free(reg);
710*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "add_rereg", "out of memory");
711*7c478bd9Sstevel@tonic-gate 		err = SLP_MEMORY_ALLOC_FAILED;
712*7c478bd9Sstevel@tonic-gate 		goto done;
713*7c478bd9Sstevel@tonic-gate 	}
714*7c478bd9Sstevel@tonic-gate 
715*7c478bd9Sstevel@tonic-gate 	reg->msg = msg;
716*7c478bd9Sstevel@tonic-gate 	reg->lifetime = lifetime;
717*7c478bd9Sstevel@tonic-gate 	reg->wake_time = (time(NULL) + lifetime) - 60;
718*7c478bd9Sstevel@tonic-gate 	reg->next = NULL;
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	/* adjust the next wake time if necessary */
721*7c478bd9Sstevel@tonic-gate 	next_wake_time =
722*7c478bd9Sstevel@tonic-gate 		reg->wake_time < next_wake_time ?
723*7c478bd9Sstevel@tonic-gate 		reg->wake_time : next_wake_time;
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 	/* add the rereg to the list */
726*7c478bd9Sstevel@tonic-gate 	if (!reregs) {
727*7c478bd9Sstevel@tonic-gate 		/* first one */
728*7c478bd9Sstevel@tonic-gate 		reregs = reg;
729*7c478bd9Sstevel@tonic-gate 		goto done;
730*7c478bd9Sstevel@tonic-gate 	}
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	/* else add it to the beginning of the list */
733*7c478bd9Sstevel@tonic-gate 	reg->next = reregs;
734*7c478bd9Sstevel@tonic-gate 	reregs = reg;
735*7c478bd9Sstevel@tonic-gate 
736*7c478bd9Sstevel@tonic-gate done:
737*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rereg_lock);
738*7c478bd9Sstevel@tonic-gate 	return (err);
739*7c478bd9Sstevel@tonic-gate }
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate /*
742*7c478bd9Sstevel@tonic-gate  * Walks through the rereg list and re-registers any which will expire
743*7c478bd9Sstevel@tonic-gate  * before the reg thread wakes up and checks again.
744*7c478bd9Sstevel@tonic-gate  * Returns true if there are more reregs on the list, false if none.
745*7c478bd9Sstevel@tonic-gate  */
746*7c478bd9Sstevel@tonic-gate static SLPBoolean check_reregs() {
747*7c478bd9Sstevel@tonic-gate 	struct rereg_entry *p;
748*7c478bd9Sstevel@tonic-gate 	time_t now, shortest_wait;
749*7c478bd9Sstevel@tonic-gate 	SLPBoolean more = SLP_TRUE;
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rereg_lock);
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 	if (!reregs) {
754*7c478bd9Sstevel@tonic-gate 		more = SLP_FALSE;
755*7c478bd9Sstevel@tonic-gate 		goto done;
756*7c478bd9Sstevel@tonic-gate 	}
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 	now = time(NULL);
759*7c478bd9Sstevel@tonic-gate 	shortest_wait = now + reregs->lifetime;
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 	for (p = reregs; p; p = p->next) {
762*7c478bd9Sstevel@tonic-gate 		if (now > (p->wake_time - granularity)) {
763*7c478bd9Sstevel@tonic-gate 		    char *reply;
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 		    /* rereg it, first recalculating signature */
766*7c478bd9Sstevel@tonic-gate 		    (void) slp_sign(&(p->msg->urlbytes), 1, now + p->lifetime,
767*7c478bd9Sstevel@tonic-gate 				    p->msg->msgiov, 1);
768*7c478bd9Sstevel@tonic-gate 		    (void) slp_sign(&(p->msg->attrbytes), 1, now + p->lifetime,
769*7c478bd9Sstevel@tonic-gate 				    p->msg->msgiov, 3);
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate 		    (void) slp_send2slpd_iov(
772*7c478bd9Sstevel@tonic-gate 				p->msg->msgiov, p->msg->msgiov_len, &reply);
773*7c478bd9Sstevel@tonic-gate 		    if (reply)
774*7c478bd9Sstevel@tonic-gate 			    free(reply);
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 		    p->wake_time = now + p->lifetime;
777*7c478bd9Sstevel@tonic-gate 		}
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 		if (p->wake_time < shortest_wait)
780*7c478bd9Sstevel@tonic-gate 			shortest_wait = p->wake_time;
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 	next_wake_time = shortest_wait;
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate done:
785*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rereg_lock);
786*7c478bd9Sstevel@tonic-gate 	return (more);
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * Removes the refresh registration for 'url'.
791*7c478bd9Sstevel@tonic-gate  */
792*7c478bd9Sstevel@tonic-gate static unsigned short dereg_rereg(const char *url) {
793*7c478bd9Sstevel@tonic-gate 	struct rereg_entry *p, *q;
794*7c478bd9Sstevel@tonic-gate 	unsigned short lifetime = 0;
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rereg_lock);
797*7c478bd9Sstevel@tonic-gate 	for (p = q = reregs; p; p = p->next) {
798*7c478bd9Sstevel@tonic-gate 		if (slp_strcasecmp(p->url, url) == 0) {
799*7c478bd9Sstevel@tonic-gate 			/* found it; remove it from the list */
800*7c478bd9Sstevel@tonic-gate 			if (p == q) {
801*7c478bd9Sstevel@tonic-gate 				/* first one on list */
802*7c478bd9Sstevel@tonic-gate 				reregs = p->next;
803*7c478bd9Sstevel@tonic-gate 			} else {
804*7c478bd9Sstevel@tonic-gate 				q->next = p->next;
805*7c478bd9Sstevel@tonic-gate 			}
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 			/* free the entry */
808*7c478bd9Sstevel@tonic-gate 			lifetime = p->lifetime;
809*7c478bd9Sstevel@tonic-gate 			free(p->url);
810*7c478bd9Sstevel@tonic-gate 			/* free the message memory */
811*7c478bd9Sstevel@tonic-gate 			free(p->msg->msgiov[0].iov_base);
812*7c478bd9Sstevel@tonic-gate 			/* free the URL auth block */
813*7c478bd9Sstevel@tonic-gate 			free(p->msg->msgiov[SLP_URL_AUTH].iov_base);
814*7c478bd9Sstevel@tonic-gate 			/* free the attr auth block */
815*7c478bd9Sstevel@tonic-gate 			free(p->msg->msgiov[SLP_ATTR_AUTH].iov_base);
816*7c478bd9Sstevel@tonic-gate 			/* free the message iovec */
817*7c478bd9Sstevel@tonic-gate 			free(p->msg->msgiov);
818*7c478bd9Sstevel@tonic-gate 			/* finally, free the message structure */
819*7c478bd9Sstevel@tonic-gate 			free(p->msg);
820*7c478bd9Sstevel@tonic-gate 			free(p);
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 			goto done;
823*7c478bd9Sstevel@tonic-gate 		}
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 		q = p;
826*7c478bd9Sstevel@tonic-gate 	}
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate done:
829*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rereg_lock);
830*7c478bd9Sstevel@tonic-gate 	return (lifetime);
831*7c478bd9Sstevel@tonic-gate }
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate /*
834*7c478bd9Sstevel@tonic-gate  * Returns configured scopes in scopes. Caller should free *scopes
835*7c478bd9Sstevel@tonic-gate  * when done. If the scope string is too long for an SLP string, the
836*7c478bd9Sstevel@tonic-gate  * string is truncated.
837*7c478bd9Sstevel@tonic-gate  */
838*7c478bd9Sstevel@tonic-gate static SLPError find_SAscopes(char **scopes) {
839*7c478bd9Sstevel@tonic-gate 	SLPError err;
840*7c478bd9Sstevel@tonic-gate 
841*7c478bd9Sstevel@tonic-gate 	if ((err = slp_administrative_scopes(scopes, SLP_TRUE))
842*7c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
843*7c478bd9Sstevel@tonic-gate 		return (err);
844*7c478bd9Sstevel@tonic-gate 	}
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate 	/* Ensure string is not too long */
847*7c478bd9Sstevel@tonic-gate 	if (strlen(*scopes) > SLP_MAX_STRINGLEN) {
848*7c478bd9Sstevel@tonic-gate 		/* truncate the string */
849*7c478bd9Sstevel@tonic-gate 		if ((*scopes)[SLP_MAX_STRINGLEN - 1] == ',') {
850*7c478bd9Sstevel@tonic-gate 			/* scopes can't end with ',' */
851*7c478bd9Sstevel@tonic-gate 			(*scopes)[SLP_MAX_STRINGLEN - 1] = 0;
852*7c478bd9Sstevel@tonic-gate 		} else {
853*7c478bd9Sstevel@tonic-gate 			(*scopes)[SLP_MAX_STRINGLEN] = 0;
854*7c478bd9Sstevel@tonic-gate 		}
855*7c478bd9Sstevel@tonic-gate 	}
856*7c478bd9Sstevel@tonic-gate 
857*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
858*7c478bd9Sstevel@tonic-gate }
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate /*
861*7c478bd9Sstevel@tonic-gate  * Does all the dirty work of freeing a msgiov.
862*7c478bd9Sstevel@tonic-gate  */
863*7c478bd9Sstevel@tonic-gate static void free_msgiov(struct iovec *msgiov, int iovlen) {
864*7c478bd9Sstevel@tonic-gate 	/* free the message memory */
865*7c478bd9Sstevel@tonic-gate 	free(msgiov[0].iov_base);
866*7c478bd9Sstevel@tonic-gate 	/* free the URL auth block */
867*7c478bd9Sstevel@tonic-gate 	free(msgiov[SLP_URL_AUTH].iov_base);
868*7c478bd9Sstevel@tonic-gate 	if (iovlen == 4) {
869*7c478bd9Sstevel@tonic-gate 		/* free the attr auth block */
870*7c478bd9Sstevel@tonic-gate 		free(msgiov[SLP_ATTR_AUTH].iov_base);
871*7c478bd9Sstevel@tonic-gate 	}
872*7c478bd9Sstevel@tonic-gate 	/* free the message iovec */
873*7c478bd9Sstevel@tonic-gate 	free(msgiov);
874*7c478bd9Sstevel@tonic-gate }
875