Imported Upstream version 2.5.1
[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.13 2006/05/25 01:47:11 johnfranks Exp $
28  *
29  * Support routines for Amanda SAMBA support
30  */
31
32 #include "amanda.h"
33 #include "findpass.h"
34 #include "util.h"
35
36 /*
37  * Find the Samba password and optional domain for a given disk.
38  * Returns pointers into an alloc-ed area.  The caller should clear them
39  * as soon as reasonable.
40  */
41
42 char *
43 findpass(
44     char *      disk,
45     char **     domain)
46 {
47   FILE *fp;
48   static char *buffer = NULL;
49   char *s, *d, *pw = NULL;
50   int ch;
51   char *qname;
52
53   *domain = NULL;                               /* just to be sure */
54   if ( (fp = fopen("/etc/amandapass", "r")) ) {
55     amfree(buffer);
56     for (; (buffer = agets(fp)) != NULL; free(buffer)) {
57       if (buffer[0] == '\0')
58         continue;
59       s = buffer;
60       ch = *s++;
61       skip_whitespace(s, ch);                   /* find start of disk name */
62       if (!ch || ch == '#') {
63         continue;
64       }
65       qname = s-1;                              /* start of disk name */
66       skip_quoted_string(s, ch);
67       if (ch && ch != '#') {
68         s[-1] = '\0';                           /* terminate disk name */
69         d = unquote_string(qname);
70         if ((strcmp(d,"*") == 0) || (strcmp(disk, d) == 0)) {
71           skip_whitespace(s, ch);               /* find start of password */
72           if (ch && ch != '#') {
73             pw = s - 1;                         /* start of password */
74             skip_non_whitespace_cs(s, ch);
75             s[-1] = '\0';                       /* terminate password */
76             pw = stralloc(pw);
77             skip_whitespace(s, ch);             /* find start of domain */
78             if (ch && ch != '#') {
79               *domain = s - 1;                  /* start of domain */
80               skip_non_whitespace_cs(s, ch);
81               s[-1] = '\0';                     /* terminate domain */
82               *domain = stralloc(*domain);
83             }
84           }
85           amfree(d);
86           break;
87         }
88         amfree(d);
89       }
90     }
91     afclose(fp);
92   }
93   return pw;
94 }
95
96 /* 
97  * Convert an amanda disk-name into a Samba sharename,
98  * optionally for a shell execution (\'s are escaped).
99  * Returns a new name alloc-d that the caller is responsible
100  * for free-ing.
101  */
102 char *
103 makesharename(
104     char *      disk,
105     int         shell)
106 {
107   char *buffer;
108   size_t buffer_size;
109   char *s;
110   int ch;
111   
112   buffer_size = 2 * strlen(disk) + 1;           /* worst case */
113   buffer = alloc(buffer_size);
114
115   s = buffer;
116   while ((ch = *disk++) != '\0') {
117     if (s >= buffer+buffer_size-2) {            /* room for escape */
118       amfree(buffer);                           /* should never happen */
119       return NULL;                              /* buffer not big enough */
120     }
121     if (ch == '/') {
122       ch = '\\';                                /* convert '/' to '\\' */
123     }
124     if (ch == '\\' && shell) {
125       *s++ = '\\';                              /* add escape for shell */
126     }
127     *s++ = ch;
128   }
129   *s = '\0';                                    /* terminate the share name */
130   return buffer;
131 }
132
133 /*
134  * find out if the samba sharename specifies both a share
135  * and a target subdirectory or just a share
136  *
137  * the caller is expected to release share & subdir
138  */
139 void
140 parsesharename(
141     char *      disk,
142     char **     share,
143     char **     subdir)
144 {
145     char *ch=NULL;
146     int slashcnt=0;
147
148     *share = NULL;
149     *subdir = NULL;
150     if (!disk) {
151         return;
152     }
153     *share = stralloc(disk);
154     ch = *share;
155     *subdir = NULL;
156     while (*ch != '\0') {
157         if (*ch == '/') {slashcnt++;}
158         if (slashcnt == 4) {
159             *ch = '\0';
160             *subdir = stralloc(++ch);
161             return;
162         }
163         ch++;
164     }
165 }
166