summaryrefslogtreecommitdiff
path: root/source3/winbindd/winbindd_sockinit.c
blob: 886b67fb470df9dd54c114d76574c5a6d555356d (plain)
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
/* 
   Unix SMB/CIFS implementation.
   Copyright (C) Tim Potter 2000-2001
   Copyright (C) 2001 by Martin Pool <mbp@samba.org>
   Copyright (C) James Peach 2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "winbindd.h"
#include "smb_launchd.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

/* Open the winbindd socket */

static int _winbindd_socket = -1;
static int _winbindd_priv_socket = -1;
static BOOL unlink_winbindd_socket = True;

static int open_winbindd_socket(void)
{
	if (_winbindd_socket == -1) {
		_winbindd_socket = create_pipe_sock(
			get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME, 0755);
		DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
			   _winbindd_socket));
	}

	return _winbindd_socket;
}

static int open_winbindd_priv_socket(void)
{
	if (_winbindd_priv_socket == -1) {
		_winbindd_priv_socket = create_pipe_sock(
			get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
		DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
			   _winbindd_priv_socket));
	}

	return _winbindd_priv_socket;
}

/* Close the winbindd socket */

static void close_winbindd_socket(void)
{
	if (_winbindd_socket != -1) {
		DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
			   _winbindd_socket));
		close(_winbindd_socket);
		_winbindd_socket = -1;
	}
	if (_winbindd_priv_socket != -1) {
		DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
			   _winbindd_priv_socket));
		close(_winbindd_priv_socket);
		_winbindd_priv_socket = -1;
	}
}

BOOL winbindd_init_sockets(int *public_sock, int *priv_sock,
				int *idle_timeout_sec)
{
	struct smb_launch_info linfo;

	if (smb_launchd_checkin_names(&linfo, "WinbindPublicPipe",
		    "WinbindPrivilegedPipe", NULL)) {
		if (linfo.num_sockets != 2) {
			DEBUG(0, ("invalid launchd configuration, "
				"expected 2 sockets but got %d\n",
				linfo.num_sockets));
			return False;
		}

		*public_sock = _winbindd_socket = linfo.socket_list[0];
		*priv_sock = _winbindd_priv_socket = linfo.socket_list[1];
		*idle_timeout_sec = linfo.idle_timeout_secs;

		unlink_winbindd_socket = False;

		smb_launchd_checkout(&linfo);
		return True;
	} else {
		*public_sock = open_winbindd_socket();
		*priv_sock = open_winbindd_priv_socket();
		*idle_timeout_sec = -1;

		if (*public_sock == -1 || *priv_sock == -1) {
			DEBUG(0, ("failed to open winbindd pipes: %s\n",
			    errno ? strerror(errno) : "unknown error"));
			return False;
		}

		return True;
	}
}

void winbindd_release_sockets(void)
{
	pstring path;

	close_winbindd_socket();

	/* Remove socket file */
	if (unlink_winbindd_socket) {
		pstr_sprintf(path, "%s/%s",
			 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME);
		unlink(path);
	}
}