1 | /* $Id: localipc.cpp 64289 2016-10-17 09:42:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Implements RTLocalIpcMakeNameUniqueUser
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*********************************************************************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *********************************************************************************************************************************/
|
---|
30 | #include <iprt/localipc.h>
|
---|
31 |
|
---|
32 | #include <iprt/err.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/crc.h>
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Make the IPC pipe name unique for user
|
---|
38 | * in a form like 'VBoxTrayIPC-6a4500cb7c726949'
|
---|
39 | *
|
---|
40 | * @returns IPRT status code.
|
---|
41 | * @retval VINF_SUCCESS and *pcbDst if pipe name created successfully.
|
---|
42 | * @retval VERR_INVALID_PARAMETER in case of invalid parameter value.
|
---|
43 | * @retval VERR_BUFFER_OVERFLOW if the pszDst is too small.
|
---|
44 | *
|
---|
45 | * @param pszPrefix Pipe name prefix, for example 'VBoxTrayIPC-'
|
---|
46 | * @param pszUsername Username.
|
---|
47 | * @param pszDst Destination buffer to store created pipe name
|
---|
48 | * @param pcbDst IN - size of destination buffer in bytes,
|
---|
49 | * OUT - length of created pipe name in bytes without trailing zero
|
---|
50 | *
|
---|
51 | * @todo r=bird: 'Unique' is a misnomer here. Doing CRC64 on a string does
|
---|
52 | * not guarentee uniqueness, not even using cryptographic hashing will
|
---|
53 | * achive this. There can be two @a pszUsername strings with the same
|
---|
54 | * CRC64 value, while still being different.
|
---|
55 | *
|
---|
56 | * Rename the function to something like RTLocalIpcMakeAsciiName and
|
---|
57 | * use a safe escape-like recoding of it.
|
---|
58 | *
|
---|
59 | * Also, pcbDst should be two parameters.
|
---|
60 | */
|
---|
61 | RTDECL(int) RTLocalIpcMakeNameUniqueUser(const char *pszPrefix, const char *pszUsername, char *pszDst, size_t *pcbDst)
|
---|
62 | {
|
---|
63 | AssertPtrReturn(pszPrefix, VERR_INVALID_PARAMETER);
|
---|
64 | AssertPtrReturn(pszUsername, VERR_INVALID_PARAMETER);
|
---|
65 | AssertPtrReturn(pcbDst, VERR_INVALID_PARAMETER);
|
---|
66 | AssertReturn(*pcbDst, VERR_BUFFER_OVERFLOW);
|
---|
67 | AssertPtrReturn(pszDst, VERR_INVALID_PARAMETER);
|
---|
68 |
|
---|
69 | const size_t cchPrefix = strlen(pszPrefix);
|
---|
70 | AssertReturn(*pcbDst > cchPrefix + 17, VERR_BUFFER_OVERFLOW);
|
---|
71 |
|
---|
72 | const size_t cchSrc = strlen(pszUsername);
|
---|
73 | AssertReturn(cchSrc, VERR_INVALID_PARAMETER);
|
---|
74 |
|
---|
75 | /* Use the crc of user name instead of user name to avoid localized pipe problem */
|
---|
76 | uint64_t uCrc = RTCrc64(pszUsername, cchSrc);
|
---|
77 | AssertReturn(uCrc > 0, VERR_INVALID_PARAMETER);
|
---|
78 |
|
---|
79 | RT_BZERO(pszDst, *pcbDst);
|
---|
80 | size_t cbRes = RTStrPrintf(pszDst, *pcbDst, "%s%016RX64", pszPrefix, uCrc);
|
---|
81 | AssertReturn(cbRes, VERR_BUFFER_OVERFLOW); /** @todo r=bird: WRONG. RTStrPrintf is a very very stupid API which is almost impossible to check for overflows */
|
---|
82 |
|
---|
83 | *pcbDst = cbRes;
|
---|
84 | return VINF_SUCCESS;
|
---|
85 | }
|
---|
86 |
|
---|