1// AuthenticationServer.cpp
2
3#include "AuthenticationServer.h"
4
5#include <string.h>
6
7#include <util/KMessage.h>
8
9#include "AuthenticationServerDefs.h"
10#include "Compatibility.h"
11
12// constructor
13AuthenticationServer::AuthenticationServer()
14	: fServerPort(-1)
15{
16	fServerPort = find_port(kAuthenticationServerPortName);
17}
18
19// destructor
20AuthenticationServer::~AuthenticationServer()
21{
22}
23
24// InitCheck
25status_t
26AuthenticationServer::InitCheck() const
27{
28	return (fServerPort >= 0 ? B_OK : fServerPort);
29}
30
31// GetAuthentication
32status_t
33AuthenticationServer::GetAuthentication(const char* context, const char* server,
34	const char* share, uid_t uid, bool badPassword,
35	bool* _cancelled, char* _foundUser, int32 foundUserSize,
36	char* _foundPassword, int32 foundPasswordSize)
37{
38	// check parameters/initialization
39	if (!context || !server || !share || !_foundPassword)
40		return B_BAD_VALUE;
41	if (InitCheck() != B_OK)
42		return InitCheck();
43	// prepare the request
44	KMessage request;
45	status_t error = request.AddInt32("uid", uid);
46	if (error != B_OK)
47		return error;
48	error = request.AddString("context", context);
49	if (error != B_OK)
50		return error;
51	error = request.AddString("server", server);
52	if (error != B_OK)
53		return error;
54	error = request.AddString("share", share);
55	if (error != B_OK)
56		return error;
57	error = request.AddBool("badPassword", badPassword);
58	if (error != B_OK)
59		return error;
60	// send the request
61	KMessage reply;
62	error = request.SendTo(fServerPort, -1, &reply);
63	if (error != B_OK)
64		return error;
65	// process the reply
66	// error
67	if (reply.FindInt32("error", &error) != B_OK)
68		return B_ERROR;
69	if (error != B_OK)
70		return error;
71	// cancelled
72	bool cancelled = false;
73	if (reply.FindBool("cancelled", &cancelled) != B_OK)
74		return B_ERROR;
75	if (_cancelled)
76		*_cancelled = cancelled;
77	if (cancelled)
78		return B_OK;
79	// user/password
80	const char* foundUser = NULL;
81	const char* foundPassword = NULL;
82	if (reply.FindString("user", &foundUser) != B_OK
83		|| reply.FindString("password", &foundPassword) != B_OK) {
84		return B_ERROR;
85	}
86	// set results
87	if (_foundUser) {
88		if (foundUserSize <= (int32)strlen(foundUser))
89			return B_BUFFER_OVERFLOW;
90		strcpy(_foundUser, foundUser);
91	}
92	if (_foundPassword) {
93		if (foundPasswordSize <= (int32)strlen(foundPassword))
94			return B_BUFFER_OVERFLOW;
95		strcpy(_foundPassword, foundPassword);
96	}
97	return B_OK;
98}
99
100