1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba utility functions
|
---|
4 | Copyright (C) Andrew Bartlett 2011
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "librpc/gen_ndr/server_id.h"
|
---|
22 |
|
---|
23 | bool server_id_same_process(const struct server_id *p1,
|
---|
24 | const struct server_id *p2)
|
---|
25 | {
|
---|
26 | return ((p1->pid == p2->pid) && (p1->vnn == p2->vnn));
|
---|
27 | }
|
---|
28 |
|
---|
29 | bool server_id_equal(const struct server_id *p1, const struct server_id *p2)
|
---|
30 | {
|
---|
31 | if (!server_id_same_process(p1, p2)) {
|
---|
32 | return false;
|
---|
33 | }
|
---|
34 |
|
---|
35 | if (p1->task_id != p2->task_id) {
|
---|
36 | return false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (p1->unique_id != p2->unique_id) {
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 |
|
---|
43 | return true;
|
---|
44 | }
|
---|
45 |
|
---|
46 | char *server_id_str_buf(struct server_id id, struct server_id_buf *dst)
|
---|
47 | {
|
---|
48 | if (server_id_is_disconnected(&id)) {
|
---|
49 | strlcpy(dst->buf, "disconnected", sizeof(dst->buf));
|
---|
50 | } else if ((id.vnn == NONCLUSTER_VNN) && (id.task_id == 0)) {
|
---|
51 | snprintf(dst->buf, sizeof(dst->buf), "%llu",
|
---|
52 | (unsigned long long)id.pid);
|
---|
53 | } else if (id.vnn == NONCLUSTER_VNN) {
|
---|
54 | snprintf(dst->buf, sizeof(dst->buf), "%llu.%u",
|
---|
55 | (unsigned long long)id.pid, (unsigned)id.task_id);
|
---|
56 | } else if (id.task_id == 0) {
|
---|
57 | snprintf(dst->buf, sizeof(dst->buf), "%u:%llu",
|
---|
58 | (unsigned)id.vnn, (unsigned long long)id.pid);
|
---|
59 | } else {
|
---|
60 | snprintf(dst->buf, sizeof(dst->buf), "%u:%llu.%u",
|
---|
61 | (unsigned)id.vnn,
|
---|
62 | (unsigned long long)id.pid,
|
---|
63 | (unsigned)id.task_id);
|
---|
64 | }
|
---|
65 | return dst->buf;
|
---|
66 | }
|
---|
67 |
|
---|
68 | size_t server_id_str_buf_unique(struct server_id id, char *buf, size_t buflen)
|
---|
69 | {
|
---|
70 | struct server_id_buf idbuf;
|
---|
71 | char unique_buf[21]; /* 2^64 is 18446744073709551616, 20 chars */
|
---|
72 | size_t idlen, unique_len, needed;
|
---|
73 |
|
---|
74 | server_id_str_buf(id, &idbuf);
|
---|
75 |
|
---|
76 | idlen = strlen(idbuf.buf);
|
---|
77 | unique_len = snprintf(unique_buf, sizeof(unique_buf), "%"PRIu64,
|
---|
78 | id.unique_id);
|
---|
79 | needed = idlen + unique_len + 2;
|
---|
80 |
|
---|
81 | if (buflen >= needed) {
|
---|
82 | memcpy(buf, idbuf.buf, idlen);
|
---|
83 | buf[idlen] = '/';
|
---|
84 | memcpy(buf + idlen + 1, unique_buf, unique_len+1);
|
---|
85 | }
|
---|
86 |
|
---|
87 | return needed;
|
---|
88 | }
|
---|
89 |
|
---|
90 | struct server_id server_id_from_string(uint32_t local_vnn,
|
---|
91 | const char *pid_string)
|
---|
92 | {
|
---|
93 | struct server_id templ = {
|
---|
94 | .vnn = NONCLUSTER_VNN, .pid = UINT64_MAX
|
---|
95 | };
|
---|
96 | struct server_id result;
|
---|
97 | int ret;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * We accept various forms with 1, 2 or 3 component forms
|
---|
101 | * because the server_id_str_buf() can print different forms, and
|
---|
102 | * we want backwards compatibility for scripts that may call
|
---|
103 | * smbclient.
|
---|
104 | */
|
---|
105 |
|
---|
106 | result = templ;
|
---|
107 | ret = sscanf(pid_string, "%"SCNu32":%"SCNu64".%"SCNu32"/%"SCNu64,
|
---|
108 | &result.vnn, &result.pid, &result.task_id,
|
---|
109 | &result.unique_id);
|
---|
110 | if (ret == 4) {
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 | result = templ;
|
---|
115 | ret = sscanf(pid_string, "%"SCNu32":%"SCNu64".%"SCNu32,
|
---|
116 | &result.vnn, &result.pid, &result.task_id);
|
---|
117 | if (ret == 3) {
|
---|
118 | return result;
|
---|
119 | }
|
---|
120 |
|
---|
121 | result = templ;
|
---|
122 | ret = sscanf(pid_string, "%"SCNu32":%"SCNu64"/%"SCNu64,
|
---|
123 | &result.vnn, &result.pid, &result.unique_id);
|
---|
124 | if (ret == 3) {
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | result = templ;
|
---|
129 | ret = sscanf(pid_string, "%"SCNu32":%"SCNu64,
|
---|
130 | &result.vnn, &result.pid);
|
---|
131 | if (ret == 2) {
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | result = templ;
|
---|
136 | ret = sscanf(pid_string, "%"SCNu64".%"SCNu32"/%"SCNu64,
|
---|
137 | &result.pid, &result.task_id, &result.unique_id);
|
---|
138 | if (ret == 3) {
|
---|
139 | result.vnn = local_vnn;
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 |
|
---|
143 | result = templ;
|
---|
144 | ret = sscanf(pid_string, "%"SCNu64".%"SCNu32,
|
---|
145 | &result.pid, &result.task_id);
|
---|
146 | if (ret == 2) {
|
---|
147 | result.vnn = local_vnn;
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | result = templ;
|
---|
152 | ret = sscanf(pid_string, "%"SCNu64"/%"SCNu64,
|
---|
153 | &result.pid, &result.unique_id);
|
---|
154 | if (ret == 2) {
|
---|
155 | result.vnn = local_vnn;
|
---|
156 | return result;
|
---|
157 | }
|
---|
158 |
|
---|
159 | result = templ;
|
---|
160 | ret = sscanf(pid_string, "%"SCNu64, &result.pid);
|
---|
161 | if (ret == 1) {
|
---|
162 | result.vnn = local_vnn;
|
---|
163 | return result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (strcmp(pid_string, "disconnected") == 0) {
|
---|
167 | server_id_set_disconnected(&result);
|
---|
168 | return result;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return templ;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Set the serverid to the special value that represents a disconnected
|
---|
176 | * client for (e.g.) durable handles.
|
---|
177 | */
|
---|
178 | void server_id_set_disconnected(struct server_id *id)
|
---|
179 | {
|
---|
180 | SMB_ASSERT(id != NULL);
|
---|
181 |
|
---|
182 | id->pid = UINT64_MAX;
|
---|
183 | id->task_id = UINT32_MAX;
|
---|
184 | id->vnn = NONCLUSTER_VNN;
|
---|
185 | id->unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
|
---|
186 |
|
---|
187 | return;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * check whether a serverid is the special placeholder for
|
---|
192 | * a disconnected client
|
---|
193 | */
|
---|
194 | bool server_id_is_disconnected(const struct server_id *id)
|
---|
195 | {
|
---|
196 | struct server_id dis;
|
---|
197 |
|
---|
198 | SMB_ASSERT(id != NULL);
|
---|
199 |
|
---|
200 | server_id_set_disconnected(&dis);
|
---|
201 |
|
---|
202 | return server_id_equal(id, &dis);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void server_id_put(uint8_t buf[24], const struct server_id id)
|
---|
206 | {
|
---|
207 | SBVAL(buf, 0, id.pid);
|
---|
208 | SIVAL(buf, 8, id.task_id);
|
---|
209 | SIVAL(buf, 12, id.vnn);
|
---|
210 | SBVAL(buf, 16, id.unique_id);
|
---|
211 | }
|
---|
212 |
|
---|
213 | void server_id_get(struct server_id *id, const uint8_t buf[24])
|
---|
214 | {
|
---|
215 | id->pid = BVAL(buf, 0);
|
---|
216 | id->task_id = IVAL(buf, 8);
|
---|
217 | id->vnn = IVAL(buf, 12);
|
---|
218 | id->unique_id = BVAL(buf, 16);
|
---|
219 | }
|
---|