BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / file_systems / nfs4 / RPCAuth.cpp
blob9164c87b5d09b9bcf095c8c5d987591b4a9a72a9
1 /*
2 * Copyright 2012 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Paweł Dziepak, pdziepak@quarnos.org
7 */
10 #include "RPCAuth.h"
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
16 #include <AutoDeleter.h>
17 #include <SupportDefs.h>
18 #include <util/kernel_cpp.h>
20 #include "RPCDefs.h"
23 using namespace RPC;
26 Auth::Auth()
31 const Auth*
32 Auth::CreateNone()
34 Auth* auth = new(std::nothrow) Auth;
35 if (auth == NULL)
36 return NULL;
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;
45 return auth;
49 const Auth*
50 Auth::CreateSys()
52 Auth* auth = new(std::nothrow) Auth;
53 if (auth == NULL)
54 return NULL;
55 ObjectDeleter<Auth> authDeleter(auth);
57 XDR::WriteStream xdr;
58 xdr.AddUInt(time(NULL));
60 char hostname[255];
61 if (gethostname(hostname, 255) < 0)
62 strcpy(hostname, "unknown");
63 xdr.AddString(hostname, 255);
65 xdr.AddUInt(getuid());
66 xdr.AddUInt(getgid());
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;
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;
87 auth->fStream.AddInt(AUTH_SYS);
88 auth->fStream.AddOpaque(xdr);
89 if (auth->fStream.Error() != B_OK)
90 return NULL;
92 authDeleter.Detach();
93 return auth;