Removed version number from file header.
[metze/samba/wip.git] / source3 / client / testsmbc.c
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
31 void 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
65 int global_id = 0;
66
67 void 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
77 int 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     /* Try to list the print jobs ... */
98
99     if (smbc_list_print_jobs("smb://samba/pclp", print_list_fn) < 0) {
100
101       fprintf(stderr, "Could not list print jobs: %s, %d\n", strerror(errno), errno);
102       exit(1);
103
104     }
105
106     /* Try to delete the last job listed */
107
108     if (global_id > 0) {
109
110       fprintf(stdout, "Trying to delete print job %u\n", global_id);
111
112       if (smbc_unlink_print_job("smb://samba/pclp", global_id) < 0) {
113
114         fprintf(stderr, "Failed to unlink job id %u, %s, %u\n", global_id, 
115                 strerror(errno), errno);
116
117         exit(1);
118
119       }
120
121     }
122
123     /* Try to print a file ... */
124
125     if (smbc_print_file("smb://samba/public/testfile2.txt", "smb://samba/pclp") < 0) {
126
127       fprintf(stderr, "Failed to print job: %s %u\n", strerror(errno), errno);
128       exit(1);
129
130     }
131
132     /* Try to delete argv[1] as a file ... */
133     
134     if (smbc_unlink(argv[1]) < 0) {
135
136       fprintf(stderr, "Could not unlink: %s, %s, %d\n",
137               argv[1], strerror(errno), errno);
138
139       exit(0);
140
141     }
142
143     if ((dh1 = smbc_opendir("smb://"))<1) {
144
145       fprintf(stderr, "Could not open directory: smb://: %s\n",
146               strerror(errno));
147
148       exit(1);
149
150     }
151
152     if ((dh2 = smbc_opendir("smb://sambanet")) < 0) {
153
154       fprintf(stderr, "Could not open directory: smb://sambanet: %s\n",
155               strerror(errno));
156
157       exit(1);
158
159     }
160
161     if ((dh3 = smbc_opendir("smb://samba")) < 0) {
162
163       fprintf(stderr, "Could not open directory: smb://samba: %s\n",
164               strerror(errno));
165
166       exit(1);
167
168     }
169
170     fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
171
172     /* Now, list those directories, but in funny ways ... */
173
174     dirp = (char *)dirbuf;
175
176     if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp, 
177                               sizeof(dirbuf))) < 0) {
178
179       fprintf(stderr, "Problems getting directory entries: %s\n",
180               strerror(errno));
181
182       exit(1);
183
184     }
185
186     /* Now, process the list of names ... */
187
188     fprintf(stdout, "Directory listing, size = %u\n", dirc);
189
190     while (dirc > 0) {
191
192       dsize = ((struct smbc_dirent *)dirp)->dirlen;
193       fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
194               ((struct smbc_dirent *)dirp)->smbc_type, 
195               ((struct smbc_dirent *)dirp)->name, 
196               ((struct smbc_dirent *)dirp)->comment);
197
198       dirp += dsize;
199       (char *)dirc -= dsize;
200
201     }
202
203     dirp = (char *)dirbuf;
204
205     if ((dirc = smbc_getdents(dh2, (struct smbc_dirent *)dirp, 
206                               sizeof(dirbuf))) < 0) {
207
208       fprintf(stderr, "Problems getting directory entries: %s\n",
209               strerror(errno));
210
211       exit(1);
212
213     }
214
215     /* Now, process the list of names ... */
216
217     fprintf(stdout, "\nDirectory listing, size = %u\n", dirc);
218
219     while (dirc > 0) {
220
221       dsize = ((struct smbc_dirent *)dirp)->dirlen;
222       fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
223               ((struct smbc_dirent *)dirp)->smbc_type, 
224               ((struct smbc_dirent *)dirp)->name, 
225               ((struct smbc_dirent *)dirp)->comment);
226
227       dirp += dsize;
228       (char *)dirc -= dsize;
229
230     }
231
232     dirp = (char *)dirbuf;
233
234     if ((dirc = smbc_getdents(dh3, (struct smbc_dirent *)dirp, 
235                               sizeof(dirbuf))) < 0) {
236
237       fprintf(stderr, "Problems getting directory entries: %s\n",
238               strerror(errno));
239
240       exit(1);
241
242     }
243
244     /* Now, process the list of names ... */
245
246     fprintf(stdout, "Directory listing, size = %u\n", dirc);
247
248     while (dirc > 0) {
249
250       dsize = ((struct smbc_dirent *)dirp)->dirlen;
251       fprintf(stdout, "\nDir Ent, Type: %u, Name: %s, Comment: %s\n",
252               ((struct smbc_dirent *)dirp)->smbc_type, 
253               ((struct smbc_dirent *)dirp)->name, 
254               ((struct smbc_dirent *)dirp)->comment);
255
256       (char *)dirp += dsize;
257       (char *)dirc -= dsize;
258
259     }
260
261     exit(1);
262
263   }
264
265   /* For now, open a file on a server that is hard coded ... later will
266    * read from the command line ...
267    */
268
269   fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
270
271   if (fd < 0) {
272
273     fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
274     exit(0);
275
276   }
277
278   fprintf(stdout, "Opened or created file: %s\n", file);
279
280   /* Now, write some date to the file ... */
281
282   bzero(buff, sizeof(buff));
283   strcpy(buff, "Some test data for the moment ...");
284
285   err = smbc_write(fd, buff, sizeof(buff));
286
287   if (err < 0) {
288     
289     fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
290     exit(0);
291
292   }
293
294   fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
295
296   /* Now, seek the file back to offset 0 */
297
298   err = smbc_lseek(fd, SEEK_SET, 0);
299
300   if (err < 0) {
301
302     fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
303     exit(0);
304
305   }
306
307   fprintf(stdout, "Completed lseek on file: %s\n", file);
308
309   /* Now, read the file contents back ... */
310
311   err = smbc_read(fd, buff, sizeof(buff));
312
313   if (err < 0) {
314
315     fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
316     exit(0);
317
318   }
319
320   fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */
321
322   fprintf(stdout, "Now fstat'ing file: %s\n", file);
323
324   err = smbc_fstat(fd, &st1);
325
326   if (err < 0) {
327
328     fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
329     exit(0);
330
331   }
332
333
334   /* Now, close the file ... */
335
336   err = smbc_close(fd);
337
338   if (err < 0) {
339
340     fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
341
342   }
343
344   /* Now, rename the file ... */
345
346   err = smbc_rename(file, file2);
347
348   if (err < 0) {
349
350     fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
351
352   }
353
354   fprintf(stdout, "Renamed file %s to %s\n", file, file2);
355
356   /* Now, create a file and delete it ... */
357
358   fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
359
360   fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
361
362   if (fd < 0) {
363
364     fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
365     exit(0);
366
367   }
368
369   fprintf(stdout, "Opened or created file: %s\n", file);
370
371   err = smbc_close(fd);
372
373   if (err < 0) {
374
375     fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
376     exit(0);
377
378   }
379   
380   /* Now, delete the file ... */
381
382   fprintf(stdout, "File %s created, now deleting ...\n", file);
383
384   err = smbc_unlink(file);
385
386   if (err < 0) {
387
388     fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
389     exit(0);
390
391   }
392
393   /* Now, stat the file, file 2 ... */
394
395   fprintf(stdout, "Now stat'ing file: %s\n", file);
396
397   err = smbc_stat(file2, &st2);
398
399   if (err < 0) {
400
401     fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
402     exit(0);
403
404   }
405
406   fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2, 
407           (int)st2.st_size, st2.st_mode);
408   fprintf(stdout, "    time: %s\n", ctime(&st2.st_atime));
409   fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file, 
410           (int)st1.st_size, st1.st_mode);
411   fprintf(stdout, "    time: %s\n", ctime(&st1.st_atime));
412
413   /* Now, make a directory ... */
414
415   fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
416
417   if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
418
419     fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n", 
420             strerror(errno));
421
422     if (errno == EEXIST) { /* Try to delete the directory */
423
424       fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
425
426       if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
427
428         fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
429
430         exit(0);
431
432       }
433
434       fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
435
436       if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
437
438         fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
439                 strerror(errno));
440
441         fprintf(stderr, "I give up!\n");
442
443         exit(1);
444
445       }
446
447     }
448
449     exit(0);
450     
451   }
452
453   fprintf(stdout, "Made dir: make-dir\n");
454   return 0;
455 }