[PATCH] New smbclient commands: users and stat
Gregor N. Purdy
gregor at focusresearch.com
Mon Nov 5 12:23:04 GMT 2001
All --
This is my first work with the Samba code base. I want to have access
to complete stat info of a file via libsmbclient, including the
*string* user name of the owner. To test out my ability to do this,
I've tinkered with source/client/client.c (patch attached) to add
a 'users' command that lists all the users on the remote machine
(enlightening on XP professional), and a 'stat' command that gathers
all the timestamps and (supposedly) the user name.
This is just a first cut at this functionality, which I'd like to
bury in the libsmbclient api somewhere so I can expose it via a
Perl module and Inline::C to any old Perl program I want in addition
to having the possiblity of commands in smbclient proper.
Anyway, its not completely working yet (I've not managed to get the
user name to print out, though I've written code that I *thought*
would do that), and I'm not properly accounting for the current
working directory when you run 'stat'. But, the basics are there.
I'd appreciate comments of the form:
(a) this is/is not a good thing to be doing
(b) this might/won't make it into the distribution once complete
(c) you are going about it correctly/incorrectly
(d) /you get the idea/ :)
Any pointers re: better ways of doing things appreciated. I'll keep
tinkering with this and post again when I think I've got it integrated
properly into smbclient, and again when (if?) I move the guts to an
API point in libsmbclient.
Regards,
-- Gregor
_____________________________________________________________________
/ perl -e 'srand(-2091643526); print chr rand 90 for (0..4)' \
Gregor N. Purdy gregor at focusresearch.com
Focus Research, Inc. http://www.focusresearch.com/
8080 Beckett Center Drive #203 513-860-3570 vox
West Chester, OH 45069 513-860-3579 fax
\_____________________________________________________________________/
-------------- next part --------------
Index: client.c
===================================================================
RCS file: /cvsroot/samba/source/client/client.c,v
retrieving revision 1.189
diff -a -u -r1.189 client.c
--- client.c 2 Nov 2001 11:31:49 -0000 1.189
+++ client.c 5 Nov 2001 20:12:09 -0000
@@ -51,6 +51,8 @@
static int process_tok(fstring tok);
static int cmd_help(void);
+static int cmd_stat(void);
+static int cmd_users(void);
/* 30 second timeout on most commands */
#define CLIENT_TIMEOUT (30*1000)
@@ -1855,9 +1857,11 @@
{"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
{"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
{"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
+ {"stat",cmd_stat,"detailed info on files",{COMPL_REMOTE,COMPL_NONE}},
{"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
{"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
{"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
+ {"users",cmd_users,"give info on users",{COMPL_NONE,COMPL_NONE}},
{"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
{"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
{"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
@@ -1894,6 +1898,106 @@
else
return(-2);
}
+
+/****************************************************************************
+print_stat_info
+****************************************************************************/
+void print_stat_info(const char * filename, const char * username, uint16 x, uint16 y, uint32 z)
+{
+ d_printf(" Owner: %s\n", username);
+}
+
+
+/****************************************************************************
+stat - print detailed info about a file
+****************************************************************************/
+static int do_stat(char * filename)
+{
+ int fnum;
+ uint16 mode;
+ size_t size;
+ time_t c_time;
+ time_t a_time;
+ time_t m_time;
+ time_t w_time;
+ SMB_INO_T ino;
+
+ d_printf("Getting status for file '%s'...\n",filename);
+
+ d_printf(" Name: %s\n", filename);
+
+ fnum = cli_open(cli, filename, O_RDONLY, DENY_NONE);
+
+ if (fnum == -1) {
+ d_printf("%s opening remote file %s\n",cli_errstr(cli),filename);
+ return 1;
+ }
+
+ if (!cli_qfileinfo(cli, fnum,
+ &mode, &size, &c_time, &a_time, &m_time, &w_time, &ino) &&
+ !cli_getattrE(cli, fnum,
+ &mode, &size, &c_time, &a_time, &m_time)) {
+ d_printf("getattrib: %s\n",cli_errstr(cli));
+ return 1;
+ }
+
+ if (!cli_NetFileGetInfo(cli, fnum, print_stat_info)) {
+ d_printf("NetFileGetInfo: %s\n",cli_errstr(cli));
+ return 1;
+ }
+
+ if (!cli_close(cli, fnum)) {
+ d_printf("Error %s closing remote file\n",cli_errstr(cli));
+ return 1;
+ }
+
+ d_printf(" Size: %ld\n", size);
+ d_printf(" Mode: %s\n", attrib_string(mode));
+ d_printf(" Created: %s", asctime(LocalTime(&c_time)));
+ d_printf(" Modified: %s", asctime(LocalTime(&m_time)));
+ d_printf(" Accessed: %s", asctime(LocalTime(&a_time)));
+ d_printf(" Written: %s", asctime(LocalTime(&w_time)));
+
+ return 0;
+}
+
+
+/****************************************************************************
+cmd_stat
+****************************************************************************/
+static int cmd_stat(void)
+{
+ fstring buf;
+ int rc = 0;
+
+ if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+ rc = do_stat(buf);
+ } else {
+ d_printf("stat: file name required!");
+ rc = 1;
+ }
+
+ return rc;
+}
+
+/****************************************************************************
+print_user_info
+****************************************************************************/
+void print_user_info(const char * username, const char * comment, const char * homedir, const char * logonscript, void * cli)
+{
+ d_printf(" %s\n", username);
+}
+
+/****************************************************************************
+users - list the users
+****************************************************************************/
+static int cmd_users(void)
+{
+ void * state;
+
+ return cli_RNetUserEnum(cli, print_user_info, state);
+}
+
/****************************************************************************
help
More information about the samba-technical
mailing list