summaryrefslogtreecommitdiff
path: root/source3/rpc_server/srv_unixinfo_nt.c
blob: 79756f20a7425f5d8f477c63aaf21e004f694d69 (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
/* 
 *  Unix SMB/CIFS implementation.
 *  RPC Pipe client / server routines for unixinfo-pipe
 *  Copyright (C) Volker Lendecke 2005
 *  
 *  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.
 */

/* This is the interface to the rpcunixinfo pipe. */

#include "includes.h"
#include "nterr.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_SRV

/* Map a sid to a uid */

NTSTATUS _unixinfo_sid_to_uid(pipes_struct *p,
			      UNIXINFO_Q_SID_TO_UID *q_u,
			      UNIXINFO_R_SID_TO_UID *r_u)
{
	uid_t uid;

	r_u->uid.low = 0;
	r_u->uid.high = 0;

	r_u->status = sid_to_uid(&q_u->sid, &uid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
	if (NT_STATUS_IS_OK(r_u->status))
		r_u->uid.low = uid;

	return r_u->status;
}

/* Map a uid to a sid */

NTSTATUS _unixinfo_uid_to_sid(pipes_struct *p,
			      UNIXINFO_Q_UID_TO_SID *q_u,
			      UNIXINFO_R_UID_TO_SID *r_u)
{
	DOM_SID sid;

	r_u->status = NT_STATUS_NO_SUCH_USER;

	if (q_u->uid.high == 0) {
		uid_to_sid(&sid, q_u->uid.low);
		r_u->status = NT_STATUS_OK;
	}

	init_r_unixinfo_uid_to_sid(r_u,
				NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);

	return r_u->status;
}

/* Map a sid to a gid */

NTSTATUS _unixinfo_sid_to_gid(pipes_struct *p,
			      UNIXINFO_Q_SID_TO_GID *q_u,
			      UNIXINFO_R_SID_TO_GID *r_u)
{
	gid_t gid;

	r_u->gid.low = 0;
	r_u->gid.high = 0;

	r_u->status = sid_to_gid(&q_u->sid, &gid) ? NT_STATUS_OK : NT_STATUS_NONE_MAPPED;
	if (NT_STATUS_IS_OK(r_u->status))
		r_u->gid.low = gid;

	return r_u->status;
}

/* Map a gid to a sid */

NTSTATUS _unixinfo_gid_to_sid(pipes_struct *p,
			      UNIXINFO_Q_GID_TO_SID *q_u,
			      UNIXINFO_R_GID_TO_SID *r_u)
{
	DOM_SID sid;

	r_u->status = NT_STATUS_NO_SUCH_USER;

	if (q_u->gid.high == 0) {
		gid_to_sid(&sid, q_u->gid.low);
		r_u->status = NT_STATUS_OK;
	}

	init_r_unixinfo_gid_to_sid(r_u,
				NT_STATUS_IS_OK(r_u->status) ? &sid : NULL);

	return r_u->status;
}

/* Get unix struct passwd information */

NTSTATUS _unixinfo_getpwuid(pipes_struct *p,
			    UNIXINFO_Q_GETPWUID *q_u,
			    UNIXINFO_R_GETPWUID *r_u)
{
	int i;

	if (r_u->count > 1023) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	r_u->info = TALLOC_ARRAY(p->mem_ctx, struct unixinfo_getpwuid,
				 q_u->count);

	if ((r_u->count > 0) && (r_u->info == NULL)) {
		return NT_STATUS_NO_MEMORY;
	}

	r_u->status = NT_STATUS_OK;
	r_u->count = q_u->count;

	for (i=0; i<r_u->count; i++) {
		struct passwd *pw;
		char *homedir, *shell;
		ssize_t len1, len2;

		r_u->info[i].status = NT_STATUS_NO_SUCH_USER;
		r_u->info[i].homedir = "";
		r_u->info[i].shell = "";

		if (q_u->uid[i].high != 0) {
			DEBUG(10, ("64-bit uids not yet supported...\n"));
			continue;
		}

		pw = getpwuid(q_u->uid[i].low);

		if (pw == NULL) {
			DEBUG(10, ("Did not find uid %d\n", q_u->uid[i].low));
			continue;
		}

		len1 = push_utf8_talloc(p->mem_ctx, &homedir, pw->pw_dir);
		len2 = push_utf8_talloc(p->mem_ctx, &shell, pw->pw_shell);

		if ((len1 < 0) || (len2 < 0) || (homedir == NULL) ||
		    (shell == NULL)) {
			DEBUG(3, ("push_utf8_talloc failed\n"));
			r_u->info[i].status = NT_STATUS_NO_MEMORY;
			continue;
		}

		r_u->info[i].status = NT_STATUS_OK;
		r_u->info[i].homedir = homedir;
		r_u->info[i].shell = shell;
	}

	return r_u->status;
}