~ubuntu-branches/ubuntu/lucid/rlpr/lucid

« back to all changes in this revision

Viewing changes to src/rlprm.c

  • Committer: Bazaar Package Importer
  • Author(s): Brian Mays
  • Date: 2002-03-20 11:21:15 UTC
  • Revision ID: james.westby@ubuntu.com-20020320112115-whl7m242ig71nu3h
Tags: upstream-2.02
ImportĀ upstreamĀ versionĀ 2.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1998, 1999 peter memishian (meem), meem@gnu.org
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * $Id: rlprm.c,v 1.7 1999/05/04 01:45:24 meem Exp $
 
15
 */
 
16
 
 
17
#include <sys/types.h>
 
18
#include <errno.h>
 
19
#include <pwd.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <stdio.h>
 
23
#include <getopt.h>
 
24
#include <unistd.h>
 
25
#include <lib.h>
 
26
 
 
27
#include "intl.h"
 
28
#include "component.h"
 
29
#include "msg.h"
 
30
#include "util.h"
 
31
#include "client.h"
 
32
#include "rlprrc.h"
 
33
#include "rlprm.h"
 
34
#include "rfc1179.h"
 
35
 
 
36
static struct component *components[] =
 
37
{
 
38
    &comp_msg,
 
39
    &comp_component,
 
40
    &comp_client,
 
41
    &comp_rlprm,
 
42
    0
 
43
};
 
44
 
 
45
const char               *program_name;
 
46
const char               *bsd_program_name = "lprm";
 
47
 
 
48
static struct rlpr_rlprm *rlpr_rlprm;
 
49
 
 
50
static const char        *make_removej_request(const char *, char * const *);
 
51
 
 
52
int
 
53
main(int argc, char *argv[])
 
54
{
 
55
    const char         *req;
 
56
    struct component   *comp;
 
57
    int                 sock_fd;
 
58
    int                 timeout;
 
59
 
 
60
    program_name = argv[0];
 
61
    toggle_root();
 
62
 
 
63
    setlocale(LC_ALL, "");
 
64
    bindtextdomain(PACKAGE, LOCALEDIR);
 
65
    textdomain(PACKAGE);
 
66
 
 
67
    if ((comp = component_init(components, argc, argv)) != 0)
 
68
        msg(R_FATAL, 0, "component `%s': init() failed!", comp->name);
 
69
 
 
70
    if (argv[optind] && strcmp("-", argv[optind]) == 0) {
 
71
        argv[optind] = rlpr_rlprm->user;
 
72
        if (getuid() == 0)
 
73
            msg(R_WARNING, 0, "`-' option as root only removes root's"
 
74
                " jobs (see BUGS)");
 
75
    }
 
76
 
 
77
    /* just to tidy the code later */
 
78
    timeout = rlpr_rlprm->timeout;
 
79
 
 
80
    /*
 
81
     * make the remove job request message (which we need to make as
 
82
     * one message because some lpd's are broken and require that they
 
83
     * reap the message in one read(2)).  then connect to the lpd,
 
84
     * send the request and read the reply.
 
85
     */
 
86
 
 
87
    req = make_removej_request(client_get_printer(), &argv[optind]);
 
88
 
 
89
    sock_fd = client_open(rlpr_rlprm->timeout);
 
90
    if (sock_fd == -1)
 
91
        msg(R_FATAL, 0, "client_open(): cannot connect to lpd");
 
92
 
 
93
    if (client_command_noack(sock_fd, req, timeout, "remove job req") == 0)
 
94
        msg(R_FATAL, 0, "unable to send `remove job' request to lpd");
 
95
 
 
96
    if (!msg_is_quiet())
 
97
        if (copy_file(sock_fd, STDOUT_FILENO, timeout, 0, "stdout") == 0)
 
98
            msg(R_FATAL, 0, "unable to get job removal output from lpd");
 
99
 
 
100
    close(sock_fd);
 
101
 
 
102
    if ((comp = component_fini(components)) != 0)
 
103
        msg(R_FATAL, 0, "component `%s': fini() failed!", comp->name);
 
104
 
 
105
    return EXIT_SUCCESS;
 
106
}
 
107
 
 
108
static const char *
 
109
make_removej_request(const char *printer, char * const *argv)
 
110
{
 
111
    unsigned int        i;
 
112
    unsigned int        request_size = 3; /* REMJ"\n" */
 
113
    char               *request;
 
114
 
 
115
    request_size += strlen(rlpr_rlprm->user) + 1;
 
116
    request_size += strlen(printer);
 
117
    for (i = 0; argv[i] != 0; i++)
 
118
        request_size += strlen(argv[i]) + 1;
 
119
 
 
120
    request = xmalloc(request_size);
 
121
    sprintf(request, "%c%s %s", REMJ, printer, rlpr_rlprm->user);
 
122
 
 
123
    for (i = 0; argv[i] != 0; i++) {
 
124
        strcat(request, " ");
 
125
        strcat(request, argv[i]);
 
126
    }
 
127
 
 
128
    strcat(request, "\n");
 
129
    return request;
 
130
}
 
131
 
 
132
static int
 
133
rlprm_init(void)
 
134
{
 
135
    struct passwd      *pwd;
 
136
 
 
137
    rlpr_rlprm  = xmalloc(sizeof (struct rlpr_rlprm));
 
138
    pwd = getpwuid(getuid());
 
139
 
 
140
    if (pwd == 0 || pwd->pw_name == 0) {
 
141
        msg(R_ERROR, errno, "unable to resolve your username");
 
142
        return 0;
 
143
    }
 
144
 
 
145
    rlpr_rlprm->timeout = R_TIMEOUT_DEFAULT;
 
146
    rlpr_rlprm->user    = xstrdup(pwd->pw_name);
 
147
    return 1;
 
148
}
 
149
 
 
150
static int
 
151
rlprm_parse_args(int opt)
 
152
{
 
153
    switch (opt) {
 
154
 
 
155
    case -600:
 
156
        msg(R_STDOUT, 0, "usage: %s [-Hprinthost] [-Pprinter] [-Xproxy]"
 
157
            " [OPTIONS] [job/user ...]\nplease see the manpage for"
 
158
            " detailed help", program_name);
 
159
        exit(EXIT_SUCCESS);
 
160
        break;
 
161
 
 
162
    case -601:
 
163
        rlpr_rlprm->timeout = strtol(optarg, 0, 0);
 
164
        break;
 
165
 
 
166
    case 'V':
 
167
        msg(R_STDOUT, 0, "version "VERSION" from "__DATE__" "__TIME__
 
168
            " -- meem@gnu.org");
 
169
        exit(EXIT_SUCCESS);
 
170
        break;
 
171
 
 
172
    case EOF:
 
173
        break;
 
174
    }
 
175
 
 
176
    return 1;
 
177
}
 
178
 
 
179
static struct option rlprm_opts[] = {
 
180
    { "help",           0, 0, -600  },
 
181
    { "timeout",        1, 0, -601  },
 
182
    { "version",        0, 0, 'V'   },
 
183
    { 0,                0, 0,  0    }
 
184
};
 
185
 
 
186
static const char rlprm_opt_list[] = "V";
 
187
 
 
188
struct component comp_rlprm = {
 
189
    "rlprm", rlprm_init, 0,
 
190
    { { rlprm_opts, rlprm_opt_list, rlprm_parse_args } }
 
191
};