summaryrefslogtreecommitdiffstats
path: root/source/nsswitch/winbindd.c
blob: 6489674cf680798b77afbf28749e2d7a75a95b4b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* 
   Unix SMB/Netbios implementation.
   Version 2.0
   Winbind daemon for ntdom nss module
   Copyright (C) Tim Potter 2000
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "winbindd.h"
#include "sids.h"

/****************************************************************************
exit thy server
****************************************************************************/
void exit_server(char *reason)
{
	static int firsttime=1;

	if (!firsttime) exit(0);
	firsttime = 0;

	unbecome_vuser();
	DEBUG(0,("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% AARGH\n"));

	if (!reason) {   
		DEBUG(0,("====================================\n"));
	}    

	DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
#ifdef MEM_MAN
	{
		extern FILE *dbf;
		smb_mem_write_verbose(dbf);
		dbgflush();
	}
#endif
	exit(0);
}

/* Connect to a domain controller and return domain sid */

int winbind_get_domain_sid(char *system_name, fstring domain_name,
                          DOM_SID *domain_sid)
{
    POLICY_HND lsa_handle;
    DOM_SID level3_sid, level5_sid;
    fstring level3_dom, level5_dom;
    BOOL res = True;

    /* Get SID from domain controller */

    res = res ? lsa_open_policy(system_name, &lsa_handle, False, 
                                0x02000000) : False;

    res = res ? lsa_query_info_pol(&lsa_handle, 0x03, level3_dom, 
                                   &level3_sid) : False;

    res = res ? lsa_query_info_pol(&lsa_handle, 0x05, level5_dom, 
                                   &level5_sid) : False;

    res = res ? lsa_close(&lsa_handle) : False;

    /* Return domain sid if successful */

    if (res && (domain_sid != NULL)) {
        memcpy(domain_sid, &level5_sid, sizeof(level5_sid));
        fstrcpy(domain_name, level5_dom);
    }

    return res;
}

/* Return a sid and type within a domain given a username */

int winbind_lookup_by_name(char *system_name, DOM_SID *level5_sid,
                           fstring name, DOM_SID *sid,
                           enum SID_NAME_USE *type)
{
    POLICY_HND lsa_handle;
    BOOL res = True;
    DOM_SID *sids = NULL;
    int num_sids = 0, num_names = 1;
    uint32 *types = NULL;

    if (name == NULL) {
        return 0;
    }

    res = res ? lsa_open_policy(system_name, &lsa_handle, True,
                                0x02000000) : False;
    
    res = res ? lsa_lookup_names(&lsa_handle, num_names, (char **)&name,
                                 &sids, &types, &num_sids) : False;

    res = res ? lsa_close(&lsa_handle) : False;

    /* Return rid and type if lookup successful */

    if (res) {

        if ((sid != NULL) && (sids != NULL)) {
            sid_copy(sid, &sids[0]);
        }

        if ((type != NULL) && (types != NULL)) {
            *type = types[0];
        }
    }
    
    return res;
}

/* Return a name and type within a domain given a rid */
int winbind_lookup_by_sid(char *system_name, DOM_SID *level5_sid,
                          DOM_SID *sid, char *name,
                          enum SID_NAME_USE *type)
{
    POLICY_HND lsa_handle;
    int num_sids = 1, num_names = 0;
    uint32 *types = NULL;
    char **names;
    BOOL res = True;


    res = res ? lsa_open_policy(system_name, &lsa_handle, True,
                                0x02000000) : False;

    res = res ? lsa_lookup_sids(&lsa_handle, num_sids, (DOM_SID **)&sid,
                                &names, &types, &num_names) : False;

    res = res ? lsa_close(&lsa_handle) : False;

    /* Return name and type if successful */

    if (res) {
        if ((names != NULL) && (name != NULL)) {
            fstrcpy(name, names[0]);
        }

        if ((type != NULL) && (types != NULL)) {
            *type = types[0];
        }
    }

    return res;
}

/* Lookup user information from rid */

int winbind_lookup_userinfo(char *system_name, DOM_SID *level5_sid,
                            uint32 user_rid, SAM_USERINFO_CTR *info)
{
    POLICY_HND sam_handle, sam_dom_handle;
    BOOL res = True;

    /* Open connection to SAM pipe and SAM domain */

    res = res ? samr_connect(system_name, 0x02000000, &sam_handle) : False;

    res = res ? samr_open_domain(&sam_handle, 0x02000000, level5_sid, 
                                 &sam_dom_handle) : False;
    /* Query user info */

    res = res ? get_samr_query_userinfo(&sam_dom_handle, 0x15, user_rid,
                                        info) : False;
    /* Close up shop */

    res = res ? samr_close(&sam_dom_handle) : False;

    res = res ? samr_close(&sam_handle) : False;

    return res;
}                                   

int winbind_lookup_groupinfo(char *system_name, DOM_SID *level5_sid,
                             uint32 group_rid, GROUP_INFO_CTR *info)
{
    POLICY_HND sam_handle, sam_dom_handle;
    BOOL res = True;

    /* Open connection to SAM pipe and SAM domain */

    res = res ? samr_connect(system_name, 0x02000000, &sam_handle) : False;

    res = res ? samr_open_domain(&sam_handle, 0x02000000, level5_sid, 
                                 &sam_dom_handle) : False;
    /* Query group info */

    res = res ? get_samr_query_groupinfo(&sam_dom_handle, 1,
                                         group_rid, info) : False;

    /* Close up shop */

    res = res ? samr_close(&sam_dom_handle) : False;

    res = res ? samr_close(&sam_handle) : False;

    return res;
}

/* Create ipc socket */

int create_winbind_socket(void)
{
    struct sockaddr_un sunaddr;
    struct stat st;
    int ret, sock;

    ret = stat(SOCKET_NAME, &st);
    if (ret == -1 && errno != ENOENT) {
        perror("stat");
        return -1;
    }

    if (ret == 0) {
        fprintf(stderr, "socket exists!\n");
        return -1;
    }

    sock = socket(AF_UNIX, SOCK_STREAM, 0);
    
    if (sock < 0) {
        perror("socket");
        return -1;
    }
    
    memset(&sunaddr, 0, sizeof(sunaddr));
    sunaddr.sun_family = AF_UNIX;
    strncpy(sunaddr.sun_path, SOCKET_NAME, sizeof(sunaddr.sun_path));
    
    if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
        perror("bind");
        close(sock);
        return -1;
    }
    
    if (chmod(SOCKET_NAME, 0700) < 0) {
        perror("chmod");
        close(sock);
      return -1;
    }
    
    if (listen(sock, 5) < 0) {
        perror("listen");
        close(sock);
        return -1;
    }
    
    /* Success! */
    
    return sock;
}

/*
 * Main function 
 */

int main(int argc, char **argv)
{
    DOM_SID domain_sid;
    fstring domain_name, sid;
    int sock;
    
    /* Initialise samba/rpc client stuff */

    lp_load(CONFIGFILE, True, False, False);
    fstrcpy(debugf, "/tmp/winbindd.log");
    setup_logging(debugf, 1);
    reopen_logs();

    charset_initialise();
    codepage_initialise(lp_client_code_page());

    /* Get the domain sid */

    if (!winbind_get_domain_sid(SERVER, domain_name, &domain_sid)) {
        DEBUG(0, ("Cannot get domain sid from %s\n", domain_name));
        return 1;
    }

    sid_to_string(sid, &domain_sid);
    DEBUG(3, ("Domain controller for domain %s has sid %s\n",
              domain_name, sid));

    sid_copy(&global_sam_sid, &domain_sid); /* ??? */
    generate_wellknown_sids(); /* ??? */

    /* Loop waiting for requests */

    if ((sock = create_winbind_socket()) == -1) {
        DEBUG(0, ("failed to create socket\n"));
        return 1;
    }

    while (1) {
        int len, sock2;
        struct sockaddr_un sunaddr;
        struct winbindd_request request;
        struct winbindd_response response;

        /* Accept connection */

        len = sizeof(sunaddr);
        sock2 = accept(sock, (struct sockaddr *)&sunaddr, &len);

        /* Read command */

        if ((len = read(sock2, &request, sizeof(request))) < 0) {
            close(sock2);
            continue;
        }

        response.result = WINBINDD_ERROR;

        /* Process command */

        switch(request.cmd) {
            
            /* User functions */

        case WINBINDD_GETPWNAM_FROM_USER: 
            DEBUG(1, ("getpwnam from user '%s'\n", request.data.username));
            winbindd_getpwnam_from_user(&domain_sid, &request, &response);
            break;

        case WINBINDD_GETPWNAM_FROM_UID:
            DEBUG(1, ("getpwnam from uid %d\n", request.data.uid));
            winbindd_getpwnam_from_uid(&domain_sid, &request, &response);
            break;

            /* Group functions */

        case WINBINDD_GETGRNAM_FROM_GROUP:
            DEBUG(1, ("getgrnam from group '%s'\n", request.data.groupname));
            winbindd_getgrnam_from_group(&domain_sid, &request, &response);
            break;

        case WINBINDD_GETGRNAM_FROM_GID:
            DEBUG(1, ("getgrnam from gid %d\n", request.data.gid));
            winbindd_getgrnam_from_gid(&domain_sid, &request, &response);
            break;

            /* Oops */

        default:
            DEBUG(0, ("oops - unknown command %d\n", request.cmd));
            break;
        }

        /* Send response */

        write(sock2, &response, sizeof(response));
        close(sock2);
    }

    return 0;
}