~ubuntu-branches/debian/lenny/dropbear/lenny

« back to all changes in this revision

Viewing changes to scpmisc.c

  • Committer: Bazaar Package Importer
  • Author(s): Gerrit Pape
  • Date: 2006-04-16 16:16:40 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416161640-ypmemhromnxg3tle
Tags: 0.48.1-1
* new upstream point release.
  * Compile fix for scp
* debian/diff/dbclient.1.diff: new: document -R option to dbclient
  accurately (thx Markus Schaber; closes: #351882).
* debian/dropbear.README.Debian: document a workaround for systems with
  possibly blocking /dev/random device (closes: #355414)..

Show diffs side-by-side

added added

removed removed

Lines of Context:
140
140
addargs(arglist *args, char *fmt, ...)
141
141
{
142
142
        va_list ap;
143
 
        char buf[1024];
144
 
        int nalloc;
 
143
        char *cp;
 
144
        u_int nalloc;
 
145
        int r;
145
146
 
146
147
        va_start(ap, fmt);
147
 
        vsnprintf(buf, sizeof(buf), fmt, ap);
 
148
        r = vasprintf(&cp, fmt, ap);
148
149
        va_end(ap);
 
150
        if (r == -1)
 
151
                fatal("addargs: argument too long");
149
152
 
150
153
        nalloc = args->nalloc;
151
154
        if (args->list == NULL) {
156
159
 
157
160
        args->list = xrealloc(args->list, nalloc * sizeof(char *));
158
161
        args->nalloc = nalloc;
159
 
        args->list[args->num++] = xstrdup(buf);
 
162
        args->list[args->num++] = cp;
160
163
        args->list[args->num] = NULL;
161
164
}
 
165
 
 
166
void
 
167
replacearg(arglist *args, u_int which, char *fmt, ...)
 
168
{
 
169
        va_list ap;
 
170
        char *cp;
 
171
        int r;
 
172
 
 
173
        va_start(ap, fmt);
 
174
        r = vasprintf(&cp, fmt, ap);
 
175
        va_end(ap);
 
176
        if (r == -1)
 
177
                fatal("replacearg: argument too long");
 
178
 
 
179
        if (which >= args->num)
 
180
                fatal("replacearg: tried to replace invalid arg %d >= %d",
 
181
                    which, args->num);
 
182
        xfree(args->list[which]);
 
183
        args->list[which] = cp;
 
184
}
 
185
 
 
186
void
 
187
freeargs(arglist *args)
 
188
{
 
189
        u_int i;
 
190
 
 
191
        if (args->list != NULL) {
 
192
                for (i = 0; i < args->num; i++)
 
193
                        xfree(args->list[i]);
 
194
                xfree(args->list);
 
195
                args->nalloc = args->num = 0;
 
196
                args->list = NULL;
 
197
        }
 
198
}
 
199
 
 
200
/*
 
201
 * NB. duplicate __progname in case it is an alias for argv[0]
 
202
 * Otherwise it may get clobbered by setproctitle()
 
203
 */
 
204
char *ssh_get_progname(char *argv0)
 
205
{
 
206
        char *p;
 
207
 
 
208
        if (argv0 == NULL)
 
209
                return ("unknown");     /* XXX */
 
210
        p = strrchr(argv0, '/');
 
211
        if (p == NULL)
 
212
                p = argv0;
 
213
        else
 
214
                p++;
 
215
 
 
216
        return (xstrdup(p));
 
217
}
 
218
 
 
219
void fatal(char* fmt,...)
 
220
{
 
221
        va_list args;
 
222
        va_start(args, fmt);
 
223
        vfprintf(stderr, fmt, args);
 
224
        va_end(args);
 
225
        exit(255);
 
226
}
 
227
 
 
228
void
 
229
sanitise_stdfd(void)
 
230
{
 
231
        int nullfd, dupfd;
 
232
 
 
233
        if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
 
234
                fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
 
235
                exit(1);
 
236
        }
 
237
        while (++dupfd <= 2) {
 
238
                /* Only clobber closed fds */
 
239
                if (fcntl(dupfd, F_GETFL, 0) >= 0)
 
240
                        continue;
 
241
                if (dup2(nullfd, dupfd) == -1) {
 
242
                        fprintf(stderr, "dup2: %s", strerror(errno));
 
243
                        exit(1);
 
244
                }
 
245
        }
 
246
        if (nullfd > 2)
 
247
                close(nullfd);
 
248
}