290af67fabec5de85250e8e630e66a02b966ff6e
[debian/amanda] / client-src / findpass.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: findpass.c,v 1.10.4.1.4.1 2001/08/01 22:36:24 jrjackson Exp $
28  *
29  * Support routines for Amanda SAMBA support
30  */
31
32 #include "findpass.h"
33
34 /*
35  * Find the Samba password and optional domain for a given disk.
36  * Returns pointers into an alloc-ed area.  The caller should clear them
37  * as soon as reasonable.
38  */
39
40 char *findpass(disk, domain)
41 char *disk, **domain;
42 {
43   FILE *fp;
44   static char *buffer = NULL;
45   char *s, *d, *pw = NULL;
46   int ch;
47
48   *domain = NULL;                               /* just to be sure */
49   if ( (fp = fopen("/etc/amandapass", "r")) ) {
50     amfree(buffer);
51     for (; (buffer = agets(fp)) != NULL; free(buffer)) {
52       s = buffer;
53       ch = *s++;
54       skip_whitespace(s, ch);                   /* find start of disk name */
55       if (!ch || ch == '#') {
56         continue;
57       }
58       d = s-1;                                  /* start of disk name */
59       skip_non_whitespace_cs(s, ch);
60       if (ch && ch != '#') {
61         s[-1] = '\0';                           /* terminate disk name */
62         if ((strcmp(d,"*") == 0) || (strcmp(disk, d) == 0)) {
63           skip_whitespace(s, ch);               /* find start of password */
64           if (ch && ch != '#') {
65             pw = s - 1;                         /* start of password */
66             skip_non_whitespace_cs(s, ch);
67             s[-1] = '\0';                       /* terminate password */
68             pw = stralloc(pw);
69             skip_whitespace(s, ch);             /* find start of domain */
70             if (ch && ch != '#') {
71               *domain = s - 1;                  /* start of domain */
72               skip_non_whitespace_cs(s, ch);
73               s[-1] = '\0';                     /* terminate domain */
74               *domain = stralloc(*domain);
75             }
76           }
77           break;
78         }
79       }
80     }
81     afclose(fp);
82   }
83   return pw;
84 }
85
86 /* 
87  * Convert an amanda disk-name into a Samba sharename,
88  * optionally for a shell execution (\'s are escaped).
89  * Returns a new name alloc-d that the caller is responsible
90  * for free-ing.
91  */
92 char *makesharename(disk, shell)
93 char *disk;
94 int shell;
95 {
96   char *buffer;
97   int buffer_size;
98   char *s;
99   int ch;
100   
101   buffer_size = 2 * strlen(disk) + 1;           /* worst case */
102   buffer = alloc(buffer_size);
103
104   s = buffer;
105   while ((ch = *disk++) != '\0') {
106     if (s >= buffer+buffer_size-2) {            /* room for escape */
107       amfree(buffer);                           /* should never happen */
108       return NULL;                              /* buffer not big enough */
109     }
110     if (ch == '/') {
111       ch = '\\';                                /* convert '/' to '\\' */
112     }
113     if (ch == '\\' && shell) {
114       *s++ = '\\';                              /* add escape for shell */
115     }
116     *s++ = ch;
117   }
118   *s = '\0';                                    /* terminate the share name */
119   return buffer;
120 }
121
122 /*
123  * find out if the samba sharename specifies both a share
124  * and a target subdirectory or just a share
125  *
126  * the caller is expected to release share & subdir
127  */
128 void parsesharename (disk, share, subdir)
129 char *disk;
130 char **share;
131 char **subdir;
132 {
133     char *ch=NULL;
134     int slashcnt=0;
135
136     *share = NULL;
137     *subdir = NULL;
138     if (!disk) {
139         return;
140     }
141     *share = stralloc(disk);
142     ch = *share;
143     *subdir = NULL;
144     while (*ch != '\0') {
145         if (*ch == '/') {slashcnt++;}
146         if (slashcnt == 4) {
147             *ch = '\0';
148             *subdir = stralloc(++ch);
149             return;
150         }
151         ch++;
152     }
153 }
154