/source3/utils/net_status.c

https://github.com/theresahalloran/samba-tool-group-doc · C · 243 lines · 186 code · 40 blank · 17 comment · 28 complexity · cd48b4246b49a081dffc08ca2059722d MD5 · raw file

  1. /*
  2. Samba Unix/Linux SMB client library
  3. net status command -- possible replacement for smbstatus
  4. Copyright (C) 2003 Volker Lendecke (vl@samba.org)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "includes.h"
  16. #include "utils/net.h"
  17. #include "session.h"
  18. #include "messages.h"
  19. int net_status_usage(struct net_context *c, int argc, const char **argv)
  20. {
  21. d_printf(_(" net status sessions [parseable] "
  22. "Show list of open sessions\n"));
  23. d_printf(_(" net status shares [parseable] "
  24. "Show list of open shares\n"));
  25. return -1;
  26. }
  27. static int show_session(const char *key, struct sessionid *session,
  28. void *private_data)
  29. {
  30. bool *parseable = (bool *)private_data;
  31. if (!process_exists(session->pid)) {
  32. return 0;
  33. }
  34. if (*parseable) {
  35. d_printf("%s\\%s\\%s\\%s\\%s\n",
  36. procid_str_static(&session->pid),
  37. uidtoname(session->uid),
  38. gidtoname(session->gid),
  39. session->remote_machine, session->hostname);
  40. } else {
  41. d_printf("%7s %-12s %-12s %-12s (%s)\n",
  42. procid_str_static(&session->pid),
  43. uidtoname(session->uid),
  44. gidtoname(session->gid),
  45. session->remote_machine, session->hostname);
  46. }
  47. return 0;
  48. }
  49. static int net_status_sessions(struct net_context *c, int argc, const char **argv)
  50. {
  51. bool parseable;
  52. if (c->display_usage) {
  53. d_printf( "%s\n"
  54. "net status sessions [parseable]\n"
  55. " %s\n",
  56. _("Usage:"),
  57. _("Display open user sessions.\n"
  58. " If parseable is specified, output is machine-"
  59. "readable."));
  60. return 0;
  61. }
  62. if (argc == 0) {
  63. parseable = false;
  64. } else if ((argc == 1) && strequal(argv[0], "parseable")) {
  65. parseable = true;
  66. } else {
  67. return net_status_usage(c, argc, argv);
  68. }
  69. if (!parseable) {
  70. d_printf(_("PID Username Group Machine"
  71. " \n"
  72. "-------------------------------------------"
  73. "------------------------\n"));
  74. }
  75. sessionid_traverse_read(show_session, &parseable);
  76. return 0;
  77. }
  78. static int show_share(struct db_record *rec,
  79. const struct connections_key *key,
  80. const struct connections_data *crec,
  81. void *state)
  82. {
  83. if (crec->cnum == -1)
  84. return 0;
  85. if (!process_exists(crec->pid)) {
  86. return 0;
  87. }
  88. d_printf("%-10.10s %s %-12s %s",
  89. crec->servicename, procid_str_static(&crec->pid),
  90. crec->machine,
  91. time_to_asc(crec->start));
  92. return 0;
  93. }
  94. struct sessionids {
  95. int num_entries;
  96. struct sessionid *entries;
  97. };
  98. static int collect_pids(const char *key, struct sessionid *session,
  99. void *private_data)
  100. {
  101. struct sessionids *ids = (struct sessionids *)private_data;
  102. if (!process_exists(session->pid))
  103. return 0;
  104. ids->num_entries += 1;
  105. ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
  106. if (!ids->entries) {
  107. ids->num_entries = 0;
  108. return 0;
  109. }
  110. ids->entries[ids->num_entries-1] = *session;
  111. return 0;
  112. }
  113. static int show_share_parseable(const struct connections_key *key,
  114. const struct connections_data *crec,
  115. void *state)
  116. {
  117. struct sessionids *ids = (struct sessionids *)state;
  118. int i;
  119. bool guest = true;
  120. if (crec->cnum == -1)
  121. return 0;
  122. if (!process_exists(crec->pid)) {
  123. return 0;
  124. }
  125. for (i=0; i<ids->num_entries; i++) {
  126. struct server_id id = ids->entries[i].pid;
  127. if (procid_equal(&id, &crec->pid)) {
  128. guest = false;
  129. break;
  130. }
  131. }
  132. d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
  133. crec->servicename,procid_str_static(&crec->pid),
  134. guest ? "" : uidtoname(ids->entries[i].uid),
  135. guest ? "" : gidtoname(ids->entries[i].gid),
  136. crec->machine,
  137. guest ? "" : ids->entries[i].hostname,
  138. time_to_asc(crec->start));
  139. return 0;
  140. }
  141. static int net_status_shares_parseable(struct net_context *c, int argc, const char **argv)
  142. {
  143. struct sessionids ids;
  144. ids.num_entries = 0;
  145. ids.entries = NULL;
  146. sessionid_traverse_read(collect_pids, &ids);
  147. connections_forall_read(show_share_parseable, &ids);
  148. SAFE_FREE(ids.entries);
  149. return 0;
  150. }
  151. static int net_status_shares(struct net_context *c, int argc, const char **argv)
  152. {
  153. if (c->display_usage) {
  154. d_printf( "%s\n"
  155. "net status shares [parseable]\n"
  156. " %s\n",
  157. _("Usage:"),
  158. _("Display open user shares.\n"
  159. " If parseable is specified, output is machine-"
  160. "readable."));
  161. return 0;
  162. }
  163. if (argc == 0) {
  164. d_printf(_("\nService pid machine "
  165. "Connected at\n"
  166. "-------------------------------------"
  167. "------------------\n"));
  168. connections_forall(show_share, NULL);
  169. return 0;
  170. }
  171. if ((argc != 1) || !strequal(argv[0], "parseable")) {
  172. return net_status_usage(c, argc, argv);
  173. }
  174. return net_status_shares_parseable(c, argc, argv);
  175. }
  176. int net_status(struct net_context *c, int argc, const char **argv)
  177. {
  178. struct functable func[] = {
  179. {
  180. "sessions",
  181. net_status_sessions,
  182. NET_TRANSPORT_LOCAL,
  183. N_("Show list of open sessions"),
  184. N_("net status sessions [parseable]\n"
  185. " If parseable is specified, output is presented "
  186. "in a machine-parseable fashion.")
  187. },
  188. {
  189. "shares",
  190. net_status_shares,
  191. NET_TRANSPORT_LOCAL,
  192. N_("Show list of open shares"),
  193. N_("net status shares [parseable]\n"
  194. " If parseable is specified, output is presented "
  195. "in a machine-parseable fashion.")
  196. },
  197. {NULL, NULL, 0, NULL, NULL}
  198. };
  199. return net_run_function(c, argc, argv, "net status", func);
  200. }