backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / lib / util / server_id.c
blob308ee2a492e450e04438a66429368838db498388
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Bartlett 2011
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.
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.
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/>.
20 #include "includes.h"
21 #include "librpc/gen_ndr/server_id.h"
23 bool server_id_same_process(const struct server_id *p1,
24 const struct server_id *p2)
26 return ((p1->pid == p2->pid) && (p1->vnn == p2->vnn));
29 bool server_id_equal(const struct server_id *p1, const struct server_id *p2)
31 if (!server_id_same_process(p1, p2)) {
32 return false;
35 if (p1->task_id != p2->task_id) {
36 return false;
39 if (p1->unique_id != p2->unique_id) {
40 return false;
43 return true;
46 char *server_id_str_buf(struct server_id id, struct server_id_buf *dst)
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);
65 return dst->buf;
68 char *server_id_str(TALLOC_CTX *mem_ctx, const struct server_id *id)
70 struct server_id_buf tmp;
71 char *result;
73 result = talloc_strdup(mem_ctx, server_id_str_buf(*id, &tmp));
74 if (result == NULL) {
75 return NULL;
79 * beautify the talloc_report output
81 talloc_set_name_const(result, result);
82 return result;
85 struct server_id server_id_from_string(uint32_t local_vnn,
86 const char *pid_string)
88 struct server_id result;
89 unsigned long long pid;
90 unsigned int vnn, task_id = 0;
92 ZERO_STRUCT(result);
95 * We accept various forms with 1, 2 or 3 component forms
96 * because the server_id_str() can print different forms, and
97 * we want backwards compatibility for scripts that may call
98 * smbclient.
100 if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
101 result.vnn = vnn;
102 result.pid = pid;
103 result.task_id = task_id;
104 } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
105 result.vnn = vnn;
106 result.pid = pid;
107 } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
108 result.vnn = local_vnn;
109 result.pid = pid;
110 result.task_id = task_id;
111 } else if (sscanf(pid_string, "%llu", &pid) == 1) {
112 result.vnn = local_vnn;
113 result.pid = pid;
114 } else if (strcmp(pid_string, "disconnected") ==0) {
115 server_id_set_disconnected(&result);
116 } else {
117 result.vnn = NONCLUSTER_VNN;
118 result.pid = UINT64_MAX;
120 return result;
124 * Set the serverid to the special value that represents a disconnected
125 * client for (e.g.) durable handles.
127 void server_id_set_disconnected(struct server_id *id)
129 SMB_ASSERT(id != NULL);
131 id->pid = UINT64_MAX;
132 id->task_id = UINT32_MAX;
133 id->vnn = NONCLUSTER_VNN;
134 id->unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
136 return;
140 * check whether a serverid is the special placeholder for
141 * a disconnected client
143 bool server_id_is_disconnected(const struct server_id *id)
145 struct server_id dis;
147 SMB_ASSERT(id != NULL);
149 server_id_set_disconnected(&dis);
151 return server_id_equal(id, &dis);
154 void server_id_put(uint8_t buf[24], const struct server_id id)
156 SBVAL(buf, 0, id.pid);
157 SIVAL(buf, 8, id.task_id);
158 SIVAL(buf, 12, id.vnn);
159 SBVAL(buf, 16, id.unique_id);
162 void server_id_get(struct server_id *id, const uint8_t buf[24])
164 id->pid = BVAL(buf, 0);
165 id->task_id = IVAL(buf, 8);
166 id->vnn = IVAL(buf, 12);
167 id->unique_id = BVAL(buf, 16);