#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <pwd.h>
#include <libintl.h>
#include <netdb.h>
#include <ns.h>
#include <list.h>
#define ESCAPE_CHARS "\\\n=: `&;|>^$()<*?["
static char *
freadline(FILE *fp, char *buf, int buflen)
{
char *s = buf;
while (fgets(s, buflen, fp)) {
if ((s == buf) && ((*s == '#') || (*s == '\n'))) {
continue;
} else {
if ((*s == '#') || (*s == '\n')) {
*s = '\0';
break;
}
buflen -= strlen(s);
s += strlen(s);
if (*(s - 2) != '\\')
break;
#ifdef STRIP_CONTINUATION
buflen -= 2;
s -= 2;
#endif
}
}
if (s == buf)
return (NULL);
else
return (buf);
}
static int
_file_put_printer(const char *file, const ns_printer_t *printer)
{
FILE *ifp,
*ofp;
char *tmpfile;
int fd;
int exit_status = 0;
int size;
size = strlen(file) + 1 + 20;
if ((tmpfile = malloc(size)) == NULL)
return (-1);
if (snprintf(tmpfile, size, "%sXXXXXX", file) >= size) {
syslog(LOG_ERR, "_file_put_printer:buffer overflow:tmpfile");
return (-1);
}
while (1) {
fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
if ((fd < 0) && (errno == EEXIST))
fd = open(file, O_RDWR);
if (fd < 0) {
if (errno == EAGAIN)
continue;
free(tmpfile);
return (-1);
}
if (lockf(fd, F_TLOCK, 0) == 0)
break;
(void) close(fd);
}
if ((ifp = fdopen(fd, "r")) == NULL) {
(void) close(fd);
free(tmpfile);
return (-1);
}
if ((fd = mkstemp(tmpfile)) < 0) {
(void) fclose(ifp);
free(tmpfile);
return (-1);
}
(void) fchmod(fd, 0644);
if ((ofp = fdopen(fd, "wb+")) != NULL) {
char buf[4096];
(void) fprintf(ofp,
"#\n#\tIf you hand edit this file, comments and structure may change.\n"
"#\tThe preferred method of modifying this file is through the use of\n"
"#\tlpset(8)\n#\n");
if (!((strcmp(printer->name, "all") == 0) &&
(printer->attributes == NULL))) {
char *t, *entry, *pentry;
(void) _cvt_printer_to_entry((ns_printer_t *)printer,
buf, sizeof (buf));
t = pentry = strdup(buf);
while (freadline(ifp, buf, sizeof (buf)) != NULL) {
ns_printer_t *tmp = (ns_printer_t *)
_cvt_nss_entry_to_printer(buf, "");
if (ns_printer_match_name(tmp, printer->name)
== 0) {
entry = pentry;
pentry = NULL;
} else {
entry = buf;
}
(void) fprintf(ofp, "%s\n", entry);
}
if (pentry != NULL)
(void) fprintf(ofp, "%s\n", pentry);
free(t);
}
(void) fclose(ofp);
(void) rename(tmpfile, file);
} else {
(void) close(fd);
(void) unlink(tmpfile);
exit_status = -1;
}
(void) fclose(ifp);
(void) free(tmpfile);
return (exit_status);
}
int
files_put_printer(const ns_printer_t *printer)
{
static char *file = "/etc/printers.conf";
return (_file_put_printer(file, printer));
}
#include <rpc/rpc.h>
#include <rpcsvc/ypclnt.h>
#include <rpcsvc/yp_prot.h>
static int
remote_command(char *command, char *host)
{
struct passwd *pw;
if ((pw = getpwuid(getuid())) != NULL) {
int fd;
if ((fd = rcmd_af(&host, htons(514), pw->pw_name, "root",
command, NULL, AF_INET6)) < 0)
return (-1);
(void) close(fd);
return (0);
} else {
return (-1);
}
}
#define TMP_PRINTERS_FILE "/tmp/printers.NIS"
#define NIS_MAKEFILE "/var/yp/Makefile"
#define MAKE_EXCERPT "/usr/lib/print/Makefile.yp"
int
nis_put_printer(const ns_printer_t *printer)
{
static char *domain = NULL;
char *map = "printers.conf.byname";
char *tmp = NULL;
char *host = NULL;
char lfile[BUFSIZ];
char rfile[BUFSIZ];
char cmd[BUFSIZ];
if (domain == NULL)
(void) yp_get_default_domain(&domain);
if ((yp_master(domain, (char *)map, &host) != 0) &&
(yp_master(domain, "passwd.byname", &host) != 0))
return (-1);
if (snprintf(lfile, sizeof (lfile), "/tmp/%s", map) >=
sizeof (lfile)) {
syslog(LOG_ERR, "nis_put_printer:lfile buffer overflow");
return (-1);
}
if (snprintf(rfile, sizeof (rfile), "root@%s:/etc/%s", host, map) >=
sizeof (rfile)) {
syslog(LOG_ERR, "nis_put_printer:rfile buffer overflow");
return (-1);
}
if (((tmp = strrchr(rfile, '.')) != NULL) &&
(strcmp(tmp, ".byname") == 0))
*tmp = '\0';
if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1",
rfile, lfile) >= sizeof (cmd)) {
syslog(LOG_ERR,
"nis_put_printer:buffer overflow building cmd");
return (-1);
}
(void) system(cmd);
if (_file_put_printer(lfile, printer) != 0)
return (-1);
if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1",
lfile, rfile) >= sizeof (cmd)) {
syslog(LOG_ERR,
"nis_put_printer:buffer overflow building cmd");
return (-1);
}
if (system(cmd) != 0)
return (-1);
if (snprintf(cmd, sizeof (cmd),
"rcp %s root@%s:%s.print >/dev/null 2>&1",
MAKE_EXCERPT, host, NIS_MAKEFILE) >= sizeof (cmd)) {
syslog(LOG_ERR,
"nis_put_printer:buffer overflow building cmd");
return (-1);
}
if (system(cmd) != 0)
return (-1);
if (snprintf(cmd, sizeof (cmd),
"/bin/sh -c 'PATH=/usr/ccs/bin:/bin:/usr/bin:$PATH "
"make -f %s -f %s.print printers.conf >/dev/null 2>&1'",
NIS_MAKEFILE, NIS_MAKEFILE) >= sizeof (cmd)) {
syslog(LOG_ERR,
"nis_put_printer:buffer overflow on make");
return (-1);
}
return (remote_command(cmd, host));
}