summaryrefslogtreecommitdiff
path: root/source3/client/testsmbc.c
blob: 0dbd5b3c2d630b13c385076bf8971482a8def837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* 
   Unix SMB/Netbios implementation.
   Version 2.0
   SMB client library test program
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Richard Sharpe 2000
   Copyright (C) John Terpsra 2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <errno.h>
#include <libsmbclient.h>

void auth_fn(char *server, char *share,
	     char **workgroup, char **username, char **password)
{
  static char wg[128], un[128], pw[128];
  /* DO nothing for now ... change later */

  fprintf(stdout, "Enter workgroup: ");
  fgets(wg, sizeof(wg), stdin);

  if (wg[strlen(wg) - 1] == 0x0a) /* A new line? */
    wg[strlen(wg) - 1] = 0x00;

  fprintf(stdout, "Enter username: ");
  fgets(un, sizeof(un), stdin);

  if (un[strlen(un) - 1] == 0x0a) /* A new line? */
    un[strlen(un) - 1] = 0x00;

  fprintf(stdout, "Enter password: ");
  fgets(pw, sizeof(pw), stdin);

  if (pw[strlen(pw) - 1] == 0x0a) /* A new line? */
    pw[strlen(pw) - 1] = 0x00;

  *workgroup = wg; *password = pw; *username = un;

}

int main(int argc, char *argv[])
{
  int err, fd, dh1, dh2, dh3, dsize, dirc;
  const char *file = "smb://samba/public/testfile.txt";
  const char *file2 = "smb://samba/public/testfile2.txt";
  const char *workgroup = "sambanet";
  char buff[256];
  char dirbuf[512];
  struct smbc_dirent *dirp;
  struct stat st1, st2;

  err = smbc_init(auth_fn, workgroup,  10); /* Initialize things */

  if (err < 0) {

    fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));

  }

  if (argc > 1) {

    if ((dh1 = smbc_opendir("smb://"))<1) {

      fprintf(stderr, "Could not open directory: smb://: %s\n",
	      strerror(errno));

      exit(1);

    }

    if ((dh2 = smbc_opendir("smb://sambanet")) < 0) {

      fprintf(stderr, "Could not open directory: smb://sambanet: %s\n",
	      strerror(errno));

      exit(1);

    }

    if ((dh3 = smbc_opendir("smb://samba")) < 0) {

      fprintf(stderr, "Could not open directory: smb://samba: %s\n",
	      strerror(errno));

      exit(1);

    }

    fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);

    /* Now, list those directories, but in funny ways ... */

    dirp = (struct smbc_dirent *)dirbuf;

    if ((dirc = smbc_getdents(dh1, dirp, sizeof(dirbuf))) < 0) {

      fprintf(stderr, "Problems getting directory entries: %s\n",
	      strerror(errno));

      exit(1);

    }

    /* Now, process the list of names ... */

    fprintf(stdout, "Directory listing, size = %u\n", dirc);

    while (dirc > 0) {

      dsize = sizeof(struct smbc_dirent) + dirp->namelen + dirp->commentlen + 1;
      fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
	      dirp->smbc_type, dirp->name, dirp->comment);

      (char *)dirp += dsize;
      (char *)dirc -= dsize;

    }

    dirp = (struct smbc_dirent *)dirbuf;

    if ((dirc = smbc_getdents(dh2, dirp, sizeof(dirbuf))) < 0) {

      fprintf(stderr, "Problems getting directory entries: %s\n",
	      strerror(errno));

      exit(1);

    }

    /* Now, process the list of names ... */

    fprintf(stdout, "\nDirectory listing, size = %u\n", dirc);

    while (dirc > 0) {

      dsize = sizeof(struct smbc_dirent) + dirp->namelen + dirp->commentlen + 1;
      fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
	      dirp->smbc_type, dirp->name, dirp->comment);

      (char *)dirp += dsize;
      (char *)dirc -= dsize;

    }

    dirp = (struct smbc_dirent *)dirbuf;

    if ((dirc = smbc_getdents(dh3, dirp, sizeof(dirbuf))) < 0) {

      fprintf(stderr, "Problems getting directory entries: %s\n",
	      strerror(errno));

      exit(1);

    }

    /* Now, process the list of names ... */

    fprintf(stdout, "Directory listing, size = %u\n", dirc);

    while (dirc > 0) {

      dsize = sizeof(struct smbc_dirent) + dirp->namelen + dirp->commentlen + 1;
      fprintf(stdout, "\nDir Ent, Type: %u, Name: %s, Comment: %s\n",
	      dirp->smbc_type, dirp->name, dirp->comment);

      (char *)dirp += dsize;
      (char *)dirc -= dsize;

    }

    exit(1);

  }

  /* For now, open a file on a server that is hard coded ... later will
   * read from the command line ...
   */

  fd = smbc_open(file, O_RDWR | O_CREAT, 0666);

  if (fd < 0) {

    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Opened or created file: %s\n", file);

  /* Now, write some date to the file ... */

  bzero(buff, sizeof(buff));
  strcpy(buff, "Some test data for the moment ...");

  err = smbc_write(fd, buff, sizeof(buff));

  if (err < 0) {
    
    fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);

  /* Now, seek the file back to offset 0 */

  err = smbc_lseek(fd, SEEK_SET, 0);

  if (err < 0) {

    fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Completed lseek on file: %s\n", file);

  /* Now, read the file contents back ... */

  err = smbc_read(fd, buff, sizeof(buff));

  if (err < 0) {

    fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */

  fprintf(stdout, "Now fstat'ing file: %s\n", file);

  err = smbc_fstat(fd, &st1);

  if (err < 0) {

    fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }


  /* Now, close the file ... */

  err = smbc_close(fd);

  if (err < 0) {

    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));

  }

  /* Now, rename the file ... */

  err = smbc_rename(file, file2);

  if (err < 0) {

    fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));

  }

  fprintf(stdout, "Renamed file %s to %s\n", file, file2);

  /* Now, create a file and delete it ... */

  fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);

  fd = smbc_open(file, O_RDWR | O_CREAT, 0666);

  if (fd < 0) {

    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Opened or created file: %s\n", file);

  err = smbc_close(fd);

  if (err < 0) {

    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }
  
  /* Now, delete the file ... */

  fprintf(stdout, "File %s created, now deleting ...\n", file);

  err = smbc_unlink(file);

  if (err < 0) {

    fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  /* Now, stat the file, file 2 ... */

  fprintf(stdout, "Now stat'ing file: %s\n", file);

  err = smbc_stat(file2, &st2);

  if (err < 0) {

    fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
    exit(0);

  }

  fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2, 
	  st2.st_size, st2.st_mode);
  fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file, 
	  st1.st_size, st1.st_mode);

}