1/*
2 * Copyright 2012 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Paweł Dziepak, pdziepak@quarnos.org
7 */
8
9
10#include "RPCAuth.h"
11
12#include <string.h>
13#include <stdlib.h>
14#include <unistd.h>
15
16#include <AutoDeleter.h>
17#include <SupportDefs.h>
18#include <util/kernel_cpp.h>
19
20#include "RPCDefs.h"
21
22
23using namespace RPC;
24
25
26Auth::Auth()
27{
28}
29
30
31const Auth*
32Auth::CreateNone()
33{
34	Auth* auth = new(std::nothrow) Auth;
35	if (auth == NULL)
36		return NULL;
37
38	auth->fStream.AddInt(AUTH_NONE);
39	auth->fStream.AddOpaque(NULL, 0);
40	if (auth->fStream.Error() != B_OK) {
41		delete auth;
42		return NULL;
43	}
44
45	return auth;
46}
47
48
49const Auth*
50Auth::CreateSys()
51{
52	Auth* auth = new(std::nothrow) Auth;
53	if (auth == NULL)
54		return NULL;
55	ObjectDeleter<Auth> authDeleter(auth);
56
57	XDR::WriteStream xdr;
58	xdr.AddUInt(time(NULL));
59
60	char hostname[255];
61	if (gethostname(hostname, 255) < 0)
62		strcpy(hostname, "unknown");
63	xdr.AddString(hostname, 255);
64
65	xdr.AddUInt(getuid());
66	xdr.AddUInt(getgid());
67
68	int count = getgroups(0, NULL);
69	if (count < B_OK)
70		return NULL;
71	gid_t* groups = (gid_t*)malloc(count * sizeof(gid_t));
72	if (groups == NULL)
73		return NULL;
74
75	int len = getgroups(count, groups);
76	if (len > 0) {
77		len = min_c(len, 16);
78		xdr.AddUInt(len);
79		for (int i = 0; i < len; i++)
80			xdr.AddUInt((uint32)groups[i]);
81	} else
82		xdr.AddUInt(0);
83	free(groups);
84	if (xdr.Error() != B_OK)
85		return NULL;
86
87	auth->fStream.AddInt(AUTH_SYS);
88	auth->fStream.AddOpaque(xdr);
89	if (auth->fStream.Error() != B_OK)
90		return NULL;
91
92	authDeleter.Detach();
93	return auth;
94}
95
96