util: Use new samba_getpass() function for passwd util.
[Samba/vl.git] / lib / util / server_id.c
bloba06891d80cceed563ba86097e950e112cb1789bb
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_equal(const struct server_id *p1, const struct server_id *p2)
25 if (p1->pid != p2->pid) {
26 return false;
29 if (p1->task_id != p2->task_id) {
30 return false;
33 if (p1->vnn != p2->vnn) {
34 return false;
37 if (p1->unique_id != p2->unique_id) {
38 return false;
41 return true;
44 char *server_id_str(TALLOC_CTX *mem_ctx, const struct server_id *id)
46 if (server_id_is_disconnected(id)) {
47 return talloc_strdup(mem_ctx, "disconnected");
48 } else if (id->vnn == NONCLUSTER_VNN && id->task_id == 0) {
49 return talloc_asprintf(mem_ctx,
50 "%llu",
51 (unsigned long long)id->pid);
52 } else if (id->vnn == NONCLUSTER_VNN) {
53 return talloc_asprintf(mem_ctx,
54 "%llu.%u",
55 (unsigned long long)id->pid,
56 (unsigned)id->task_id);
57 } else if (id->task_id == 0) {
58 return talloc_asprintf(mem_ctx,
59 "%u:%llu",
60 (unsigned)id->vnn,
61 (unsigned long long)id->pid);
62 } else {
63 return talloc_asprintf(mem_ctx,
64 "%u:%llu.%u",
65 (unsigned)id->vnn,
66 (unsigned long long)id->pid,
67 (unsigned)id->task_id);
71 struct server_id server_id_from_string(uint32_t local_vnn,
72 const char *pid_string)
74 struct server_id result;
75 unsigned long long pid;
76 unsigned int vnn, task_id = 0;
78 ZERO_STRUCT(result);
81 * We accept various forms with 1, 2 or 3 component forms
82 * because the server_id_str() can print different forms, and
83 * we want backwards compatibility for scripts that may call
84 * smbclient.
86 if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
87 result.vnn = vnn;
88 result.pid = pid;
89 result.task_id = task_id;
90 } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
91 result.vnn = vnn;
92 result.pid = pid;
93 } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
94 result.vnn = local_vnn;
95 result.pid = pid;
96 result.task_id = task_id;
97 } else if (sscanf(pid_string, "%llu", &pid) == 1) {
98 result.vnn = local_vnn;
99 result.pid = pid;
100 } else if (strcmp(pid_string, "disconnected") ==0) {
101 server_id_set_disconnected(&result);
102 } else {
103 result.vnn = NONCLUSTER_VNN;
104 result.pid = UINT64_MAX;
106 return result;
110 * Set the serverid to the special value that represents a disconnected
111 * client for (e.g.) durable handles.
113 void server_id_set_disconnected(struct server_id *id)
115 SMB_ASSERT(id != NULL);
117 id->pid = UINT64_MAX;
118 id->task_id = UINT32_MAX;
119 id->vnn = NONCLUSTER_VNN;
120 id->unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
122 return;
126 * check whether a serverid is the special placeholder for
127 * a disconnected client
129 bool server_id_is_disconnected(const struct server_id *id)
131 struct server_id dis;
133 SMB_ASSERT(id != NULL);
135 server_id_set_disconnected(&dis);
137 return server_id_equal(id, &dis);