utils/net_status.c

説明を見る。
00001 /* 
00002    Samba Unix/Linux SMB client library 
00003    net status command -- possible replacement for smbstatus
00004    Copyright (C) 2003 Volker Lendecke (vl@samba.org)
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00019 
00020 #include "includes.h"
00021 #include "utils/net.h"
00022 
00023 static int show_session(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
00024                         void *state)
00025 {
00026         BOOL *parseable = (BOOL *)state;
00027         struct sessionid sessionid;
00028 
00029         if (dbuf.dsize != sizeof(sessionid))
00030                 return 0;
00031 
00032         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
00033 
00034         if (!process_exists_by_pid(sessionid.pid)) {
00035                 return 0;
00036         }
00037 
00038         if (*parseable) {
00039                 d_printf("%d\\%s\\%s\\%s\\%s\n",
00040                          (int)sessionid.pid, uidtoname(sessionid.uid),
00041                          gidtoname(sessionid.gid), 
00042                          sessionid.remote_machine, sessionid.hostname);
00043         } else {
00044                 d_printf("%5d   %-12s  %-12s  %-12s (%s)\n",
00045                          (int)sessionid.pid, uidtoname(sessionid.uid),
00046                          gidtoname(sessionid.gid), 
00047                          sessionid.remote_machine, sessionid.hostname);
00048         }
00049 
00050         return 0;
00051 }
00052 
00053 static int net_status_sessions(int argc, const char **argv)
00054 {
00055         TDB_CONTEXT *tdb;
00056         BOOL parseable;
00057 
00058         if (argc == 0) {
00059                 parseable = False;
00060         } else if ((argc == 1) && strequal(argv[0], "parseable")) {
00061                 parseable = True;
00062         } else {
00063                 return net_help_status(argc, argv);
00064         }
00065 
00066         if (!parseable) {
00067                 d_printf("PID     Username      Group         Machine"
00068                          "                        \n");
00069                 d_printf("-------------------------------------------"
00070                          "------------------------\n");
00071         }
00072 
00073         tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
00074                            TDB_DEFAULT, O_RDONLY, 0);
00075 
00076         if (tdb == NULL) {
00077                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
00078                 return -1;
00079         }
00080 
00081         tdb_traverse(tdb, show_session, &parseable);
00082         tdb_close(tdb);
00083 
00084         return 0;
00085 }
00086 
00087 static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
00088                       void *state)
00089 {
00090         struct connections_data crec;
00091 
00092         if (dbuf.dsize != sizeof(crec))
00093                 return 0;
00094 
00095         memcpy(&crec, dbuf.dptr, sizeof(crec));
00096 
00097         if (crec.cnum == -1)
00098                 return 0;
00099 
00100         if (!process_exists(crec.pid)) {
00101                 return 0;
00102         }
00103 
00104         d_printf("%-10.10s   %s   %-12s  %s",
00105                crec.servicename,procid_str_static(&crec.pid),
00106                crec.machine,
00107                time_to_asc(crec.start));
00108 
00109         return 0;
00110 }
00111 
00112 struct sessionids {
00113         int num_entries;
00114         struct sessionid *entries;
00115 };
00116 
00117 static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
00118                        void *state)
00119 {
00120         struct sessionids *ids = (struct sessionids *)state;
00121         struct sessionid sessionid;
00122 
00123         if (dbuf.dsize != sizeof(sessionid))
00124                 return 0;
00125 
00126         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
00127 
00128         if (!process_exists_by_pid(sessionid.pid)) 
00129                 return 0;
00130 
00131         ids->num_entries += 1;
00132         ids->entries = SMB_REALLOC_ARRAY(ids->entries, struct sessionid, ids->num_entries);
00133         if (!ids->entries) {
00134                 ids->num_entries = 0;
00135                 return 0;
00136         }
00137         ids->entries[ids->num_entries-1] = sessionid;
00138 
00139         return 0;
00140 }
00141 
00142 static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
00143                                 void *state)
00144 {
00145         struct sessionids *ids = (struct sessionids *)state;
00146         struct connections_data crec;
00147         int i;
00148         BOOL guest = True;
00149 
00150         if (dbuf.dsize != sizeof(crec))
00151                 return 0;
00152 
00153         memcpy(&crec, dbuf.dptr, sizeof(crec));
00154 
00155         if (crec.cnum == -1)
00156                 return 0;
00157 
00158         if (!process_exists(crec.pid)) {
00159                 return 0;
00160         }
00161 
00162         for (i=0; i<ids->num_entries; i++) {
00163                 struct process_id id = pid_to_procid(ids->entries[i].pid);
00164                 if (procid_equal(&id, &crec.pid)) {
00165                         guest = False;
00166                         break;
00167                 }
00168         }
00169 
00170         d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
00171                  crec.servicename,procid_str_static(&crec.pid),
00172                  guest ? "" : uidtoname(ids->entries[i].uid),
00173                  guest ? "" : gidtoname(ids->entries[i].gid),
00174                  crec.machine, 
00175                  guest ? "" : ids->entries[i].hostname,
00176                  time_to_asc(crec.start));
00177 
00178         return 0;
00179 }
00180 
00181 static int net_status_shares_parseable(int argc, const char **argv)
00182 {
00183         struct sessionids ids;
00184         TDB_CONTEXT *tdb;
00185 
00186         ids.num_entries = 0;
00187         ids.entries = NULL;
00188 
00189         tdb = tdb_open_log(lock_path("sessionid.tdb"), 0,
00190                            TDB_DEFAULT, O_RDONLY, 0);
00191 
00192         if (tdb == NULL) {
00193                 d_fprintf(stderr, "%s not initialised\n", lock_path("sessionid.tdb"));
00194                 return -1;
00195         }
00196 
00197         tdb_traverse(tdb, collect_pid, &ids);
00198         tdb_close(tdb);
00199 
00200         tdb = tdb_open_log(lock_path("connections.tdb"), 0,
00201                            TDB_DEFAULT, O_RDONLY, 0);
00202 
00203         if (tdb == NULL) {
00204                 d_fprintf(stderr, "%s not initialised\n", lock_path("connections.tdb"));
00205                 d_fprintf(stderr, "This is normal if no SMB client has ever "
00206                          "connected to your server.\n");
00207                 return -1;
00208         }
00209 
00210         tdb_traverse(tdb, show_share_parseable, &ids);
00211         tdb_close(tdb);
00212 
00213         SAFE_FREE(ids.entries);
00214 
00215         return 0;
00216 }
00217 
00218 static int net_status_shares(int argc, const char **argv)
00219 {
00220         TDB_CONTEXT *tdb;
00221 
00222         if (argc == 0) {
00223 
00224                 d_printf("\nService      pid     machine       "
00225                          "Connected at\n");
00226                 d_printf("-------------------------------------"
00227                          "------------------\n");
00228 
00229                 tdb = tdb_open_log(lock_path("connections.tdb"), 0,
00230                                    TDB_DEFAULT, O_RDONLY, 0);
00231 
00232                 if (tdb == NULL) {
00233                         d_fprintf(stderr, "%s not initialised\n",
00234                                  lock_path("connections.tdb"));
00235                         d_fprintf(stderr, "This is normal if no SMB client has "
00236                                  "ever connected to your server.\n");
00237                         return -1;
00238                 }
00239 
00240                 tdb_traverse(tdb, show_share, NULL);
00241                 tdb_close(tdb);
00242 
00243                 return 0;
00244         }
00245 
00246         if ((argc != 1) || !strequal(argv[0], "parseable")) {
00247                 return net_help_status(argc, argv);
00248         }
00249 
00250         return net_status_shares_parseable(argc, argv);
00251 }
00252 
00253 int net_status(int argc, const char **argv)
00254 {
00255         struct functable func[] = {
00256                 {"sessions", net_status_sessions},
00257                 {"shares", net_status_shares},
00258                 {NULL, NULL}
00259         };
00260         return net_run_function(argc, argv, func, net_help_status);
00261 }

Sambaに対してSat Aug 29 21:23:29 2009に生成されました。  doxygen 1.4.7