-
Notifications
You must be signed in to change notification settings - Fork 0
/
cups-custom-auth-command.patch
150 lines (149 loc) · 5.48 KB
/
cups-custom-auth-command.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
diff --git a/scheduler/auth.c b/scheduler/auth.c
index 7f6c3f6d2..ae7d711be 100644
--- a/scheduler/auth.c
+++ b/scheduler/auth.c
@@ -1657,6 +1657,145 @@ cupsdIsAuthorized(cupsd_client_t *con, /* I - Connection */
(type == CUPSD_AUTH_NONE && cupsArrayCount(best->names) == 0))
return (HTTP_OK);
+ /*
+ * Custom authorization...
+ */
+ static char *cups_custom_auth_command = "/usr/local/libexec/cups_custom_auth";
+ if (access(cups_custom_auth_command, X_OK) == 0 &&
+ best->level == CUPSD_AUTH_USER &&
+ best->limit == CUPSD_AUTH_LIMIT_IPP &&
+ !(con->is_browser) && con->request->request.op.version[0] >= 2)
+ {
+ char *argv[10]; /* Command-line arguments */
+ char *envp[MAX_ENV]; /* Environment */
+ char req_username[256]; /* requesting-user-name */
+ char orig_username[256]; /* job-originating-user-name */
+ char accounting_id[256]; /* job-accounting-user-id */
+ int temppid; /* Process ID for custom auth processes */
+ int outfds[2]; /* Pipe file descriptors for stdout */
+ int errfds[2]; /* Pipe file descriptors for stderr output */
+ ipp_attribute_t *attr;
+
+ req_username[0] = '\0';
+ attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
+ if (attr)
+ {
+ strlcpy(req_username, attr->values[0].string.text, sizeof(req_username));
+ }
+
+ orig_username[0] = '\0';
+ attr = ippFindAttribute(con->request, "job-originating-user-name", IPP_TAG_NAME);
+ if (attr)
+ {
+ strlcpy(orig_username, attr->values[0].string.text, sizeof(orig_username));
+ }
+
+ accounting_id[0] = '\0';
+ attr = ippFindAttribute(con->request, "job-accounting-user-id", IPP_TAG_NAME);
+ if (attr)
+ {
+ strlcpy(accounting_id, attr->values[0].string.text, sizeof(accounting_id));
+ }
+
+ argv[0] = cups_custom_auth_command;
+ argv[1] = (char *)ippOpString(best->op); /* IPP operation */
+ argv[2] = con->uri;
+ argv[3] = (char *)httpGetHostname(con->http, NULL, 0); /* client hostname or IP address */
+ argv[4] = con->username;
+ argv[5] = owner ? (char *)owner : "";
+ argv[6] = req_username;
+ argv[7] = orig_username;
+ argv[8] = accounting_id;
+ argv[9] = NULL;
+
+ cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+
+ if (cupsdOpenPipe(outfds) || cupsdOpenPipe(errfds))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create pipe for %s - %s",
+ cups_custom_auth_command, strerror(errno));
+ return (HTTP_UNAUTHORIZED);
+ }
+
+ if(cupsdStartProcess(cups_custom_auth_command, argv, envp, -1, outfds[1], errfds[1], -1, -1, 0, DefaultProfile, NULL, &temppid) < 0)
+ {
+ /*
+ * Error - can't fork!
+ */
+
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s",
+ cups_custom_auth_command, strerror(errno));
+
+ cupsdClosePipe(outfds);
+ cupsdClosePipe(errfds);
+ return (HTTP_UNAUTHORIZED);
+ }
+ else
+ {
+ /*
+ * Fork successful ...
+ */
+ char buf[2048];
+ FILE* outfp = fdopen(outfds[0], "r");
+ FILE* errfp = fdopen(errfds[0], "r");
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Started %s- PID = %d", cups_custom_auth_command, temppid);
+
+ close(outfds[1]);
+ close(errfds[1]);
+
+ buf[0] = '\0';
+ while (fgets(buf, sizeof(buf), errfp) != NULL)
+ {
+ buf[strcspn(buf, "\r\n")] = '\0'; /* chomp any kind of newline */
+ if (strncmp(buf, "DEBUG:", 6) == 0) {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdIsAuthorized custom authorization:%s", &buf[6]);
+ } else if (strncmp(buf, "WARN:", 5) == 0) {
+ cupsdLogMessage(CUPSD_LOG_WARN, "cupsdIsAuthorized custom authorization:%s", &buf[5]);
+ } else if (strncmp(buf, "ERROR:", 6) == 0) {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdIsAuthorized custom authorization:%s", &buf[6]);
+ } else {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdIsAuthorized custom authorization: %s", buf);
+ }
+ }
+
+ username[0] = '\0';
+ while (fgets(buf, sizeof(buf), outfp) != NULL)
+ {
+ buf[strcspn(buf, "\r\n")] = '\0'; /* chomp any kind of newline */
+ if (strncmp(buf, "username=", 9) == 0) {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdIsAuthorized using custom authorization: username=\"%s\"",
+ &buf[9]);
+ strlcpy(username, &buf[9], sizeof(username));
+ strlcpy(con->username, username, sizeof(con->username));
+ /* replace requesting-user-name with custom authorization username */
+ if (strncmp(req_username, username, sizeof(username) != 0))
+ {
+ attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
+ if (attr)
+ {
+ if (attr->values[0].string.text) _cupsStrFree(attr->values[0].string.text);
+ attr->values[0].string.text = _cupsStrAlloc(username);
+ }
+ }
+
+ } else if (strncmp(buf, "auth-type=", 10) == 0) {
+ cupsdLogMessage(CUPSD_LOG_WARN, "cupsdIsAuthorized custom authorization auth-type:%s", &buf[10]);
+ /* TO-DO, overide authentication type with custom authentication type */
+ }
+ }
+
+ fclose(outfp);
+ fclose(errfp);
+
+ if (username[0])
+ {
+
+ return (HTTP_OK);
+ }
+ }
+ }
+
if (!con->username[0] && type == CUPSD_AUTH_NONE &&
best->limit == CUPSD_AUTH_LIMIT_IPP)
{