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