1/*
2   Unix SMB/CIFS implementation.
3   SMB client library test program
4   Copyright (C) Andrew Tridgell 1998
5   Copyright (C) Richard Sharpe 2000
6   Copyright (C) John Terpsra 2000
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include <stdio.h>
24#include <errno.h>
25#include <sys/time.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <libsmbclient.h>
30
31void auth_fn(const char *server, const char *share,
32	     char *workgroup, int wgmaxlen, char *username, int unmaxlen,
33	     char *password, int pwmaxlen)
34{
35  char temp[128];
36
37  fprintf(stdout, "Need password for //%s/%s\n", server, share);
38
39  fprintf(stdout, "Enter workgroup: [%s] ", workgroup);
40  fgets(temp, sizeof(temp), stdin);
41
42  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
43    temp[strlen(temp) - 1] = 0x00;
44
45  if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
46
47  fprintf(stdout, "Enter username: [%s] ", username);
48  fgets(temp, sizeof(temp), stdin);
49
50  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
51    temp[strlen(temp) - 1] = 0x00;
52
53  if (temp[0]) strncpy(username, temp, unmaxlen - 1);
54
55  fprintf(stdout, "Enter password: [%s] ", password);
56  fgets(temp, sizeof(temp), stdin);
57
58  if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
59    temp[strlen(temp) - 1] = 0x00;
60
61  if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
62
63}
64
65int global_id = 0;
66
67void print_list_fn(struct print_job_info *pji)
68{
69
70  fprintf(stdout, "Print job: ID: %u, Prio: %u, Size: %u, User: %s, Name: %s\n",
71	  pji->id, pji->priority, pji->size, pji->user, pji->name);
72
73  global_id = pji->id;
74
75}
76
77int main(int argc, char *argv[])
78{
79  int err, fd, dh1, dh2, dh3, dsize, dirc;
80  const char *file = "smb://samba/public/testfile.txt";
81  const char *file2 = "smb://samba/public/testfile2.txt";
82  char buff[256];
83  char dirbuf[512];
84  char *dirp;
85  struct stat st1, st2;
86
87  err = smbc_init(auth_fn,  10); /* Initialize things */
88
89  if (err < 0) {
90
91    fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
92
93  }
94
95  if (argc > 1) {
96
97    if ((dh1 = smbc_opendir(argv[1]))<1) {
98
99      fprintf(stderr, "Could not open directory: %s: %s\n",
100	      argv[1], strerror(errno));
101
102      exit(1);
103
104    }
105
106    fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
107
108    /* Now, list those directories, but in funny ways ... */
109
110    dirp = (char *)dirbuf;
111
112    if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
113			      sizeof(dirbuf))) < 0) {
114
115      fprintf(stderr, "Problems getting directory entries: %s\n",
116	      strerror(errno));
117
118      exit(1);
119
120    }
121
122    /* Now, process the list of names ... */
123
124    fprintf(stdout, "Directory listing, size = %u\n", dirc);
125
126    while (dirc > 0) {
127
128      dsize = ((struct smbc_dirent *)dirp)->dirlen;
129      fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
130	      ((struct smbc_dirent *)dirp)->smbc_type,
131	      ((struct smbc_dirent *)dirp)->name,
132	      ((struct smbc_dirent *)dirp)->comment);
133
134      dirp += dsize;
135      (char *)dirc -= dsize;
136
137    }
138
139    dirp = (char *)dirbuf;
140
141    exit(1);
142
143  }
144
145  /* For now, open a file on a server that is hard coded ... later will
146   * read from the command line ...
147   */
148
149  fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
150
151  if (fd < 0) {
152
153    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
154    exit(0);
155
156  }
157
158  fprintf(stdout, "Opened or created file: %s\n", file);
159
160  /* Now, write some date to the file ... */
161
162  bzero(buff, sizeof(buff));
163  strcpy(buff, "Some test data for the moment ...");
164
165  err = smbc_write(fd, buff, sizeof(buff));
166
167  if (err < 0) {
168
169    fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
170    exit(0);
171
172  }
173
174  fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
175
176  /* Now, seek the file back to offset 0 */
177
178  err = smbc_lseek(fd, SEEK_SET, 0);
179
180  if (err < 0) {
181
182    fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
183    exit(0);
184
185  }
186
187  fprintf(stdout, "Completed lseek on file: %s\n", file);
188
189  /* Now, read the file contents back ... */
190
191  err = smbc_read(fd, buff, sizeof(buff));
192
193  if (err < 0) {
194
195    fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
196    exit(0);
197
198  }
199
200  fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */
201
202  fprintf(stdout, "Now fstat'ing file: %s\n", file);
203
204  err = smbc_fstat(fd, &st1);
205
206  if (err < 0) {
207
208    fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
209    exit(0);
210
211  }
212
213
214  /* Now, close the file ... */
215
216  err = smbc_close(fd);
217
218  if (err < 0) {
219
220    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
221
222  }
223
224  /* Now, rename the file ... */
225
226  err = smbc_rename(file, file2);
227
228  if (err < 0) {
229
230    fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
231
232  }
233
234  fprintf(stdout, "Renamed file %s to %s\n", file, file2);
235
236  /* Now, create a file and delete it ... */
237
238  fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
239
240  fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
241
242  if (fd < 0) {
243
244    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
245    exit(0);
246
247  }
248
249  fprintf(stdout, "Opened or created file: %s\n", file);
250
251  err = smbc_close(fd);
252
253  if (err < 0) {
254
255    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
256    exit(0);
257
258  }
259
260  /* Now, delete the file ... */
261
262  fprintf(stdout, "File %s created, now deleting ...\n", file);
263
264  err = smbc_unlink(file);
265
266  if (err < 0) {
267
268    fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
269    exit(0);
270
271  }
272
273  /* Now, stat the file, file 2 ... */
274
275  fprintf(stdout, "Now stat'ing file: %s\n", file);
276
277  err = smbc_stat(file2, &st2);
278
279  if (err < 0) {
280
281    fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
282    exit(0);
283
284  }
285
286  fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2,
287	  (int)st2.st_size, st2.st_mode);
288  fprintf(stdout, "    time: %s\n", ctime(&st2.st_atime));
289  fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file,
290	  (int)st1.st_size, st1.st_mode);
291  fprintf(stdout, "    time: %s\n", ctime(&st1.st_atime));
292
293  /* Now, make a directory ... */
294
295  fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
296
297  if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
298
299    fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
300	    strerror(errno));
301
302    if (errno == EEXIST) { /* Try to delete the directory */
303
304      fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
305
306      if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
307
308	fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
309
310	exit(0);
311
312      }
313
314      fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
315
316      if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
317
318	fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
319		strerror(errno));
320
321	fprintf(stderr, "I give up!\n");
322
323	exit(1);
324
325      }
326
327    }
328
329    exit(0);
330
331  }
332
333  fprintf(stdout, "Made dir: make-dir\n");
334  return 0;
335}
336